aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-25 02:43:34 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-25 02:43:34 +0000
commit9ce4ae8142b191ab6c2135165d5904351b48f665 (patch)
tree0064cf06ce484a14cacfecff0299b3f32c8ec1dd /coreutils
parent92e13c2a11381eec06a382de6917369ded240804 (diff)
downloadbusybox-9ce4ae8142b191ab6c2135165d5904351b48f665.tar.gz
date: make help text more understandable; small shrink
text data bss dec hex filename 799025 641 7380 807046 c5086 busybox_old 799009 641 7380 807030 c5076 busybox_unstripped
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/date.c104
1 files changed, 51 insertions, 53 deletions
diff --git a/coreutils/date.c b/coreutils/date.c
index a8e339333..064f758e2 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -39,70 +39,70 @@ static void maybe_set_utc(int opt)
}
int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int date_main(int argc, char **argv)
+int date_main(int argc ATTRIBUTE_UNUSED, char **argv)
{
- time_t tm;
struct tm tm_time;
+ time_t tm;
unsigned opt;
int ifmt = -1;
- char *date_str = NULL;
- char *date_fmt = NULL;
- char *filename = NULL;
- char *isofmt_arg;
- char *hintfmt_arg;
+ char *date_str;
+ char *fmt_dt2str;
+ char *fmt_str2dt;
+ char *filename;
+ char *isofmt_arg = NULL;
opt_complementary = "d--s:s--d"
USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
opt = getopt32(argv, "Rs:ud:r:"
USE_FEATURE_DATE_ISOFMT("I::D:"),
&date_str, &date_str, &filename
- USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
+ USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt));
+ argv += optind;
maybe_set_utc(opt);
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
- if (!isofmt_arg) {
- ifmt = 0; /* default is date */
- } else {
- static const char *const isoformats[] = {
- "date", "hours", "minutes", "seconds"
- };
-
- for (ifmt = 0; ifmt < 4; ifmt++)
- if (!strcmp(isofmt_arg, isoformats[ifmt]))
- goto found;
- /* parse error */
- bb_show_usage();
- found: ;
+ ifmt = 0; /* default is date */
+ if (isofmt_arg) {
+ static const char isoformats[] ALIGN1 =
+ "date\0""hours\0""minutes\0""seconds\0";
+ ifmt = index_in_strings(isoformats, isofmt_arg);
+ if (ifmt < 0)
+ bb_show_usage();
}
}
- /* XXX, date_fmt == NULL from this always */
- if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
- date_fmt = &argv[optind][1]; /* Skip over the '+' */
- } else if (date_str == NULL) {
+ fmt_dt2str = NULL;
+ if (argv[0] && argv[0][0] == '+') {
+ fmt_dt2str = &argv[0][1]; /* Skip over the '+' */
+ argv++;
+ }
+ if (!(opt & (DATE_OPT_SET | DATE_OPT_DATE))) {
opt |= DATE_OPT_SET;
- date_str = argv[optind];
+ date_str = argv[0]; /* can be NULL */
}
/* Now we have parsed all the information except the date format
which depends on whether the clock is being set or read */
- if (filename) {
+ if (opt & DATE_OPT_REFERENCE) {
struct stat statbuf;
xstat(filename, &statbuf);
tm = statbuf.st_mtime;
} else
time(&tm);
memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
- /* Zero out fields - take her back to midnight! */
+
+ /* If date string is given, update tm_time, and maybe set date */
if (date_str != NULL) {
+ /* Zero out fields - take her back to midnight! */
tm_time.tm_sec = 0;
tm_time.tm_min = 0;
tm_time.tm_hour = 0;
/* Process any date input to UNIX time since 1 Jan 1970 */
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
- strptime(date_str, hintfmt_arg, &tm_time);
+ if (strptime(date_str, fmt_str2dt, &tm_time) == NULL)
+ bb_error_msg_and_die(bb_msg_invalid_date, date_str);
} else if (strchr(date_str, ':') != NULL) {
/* Parse input and assign appropriately to tm_time */
@@ -181,55 +181,53 @@ int date_main(int argc, char **argv)
/* Display output */
/* Deal with format string */
-
- if (date_fmt == NULL) {
+ if (fmt_dt2str == NULL) {
int i;
- date_fmt = xzalloc(32);
+ fmt_dt2str = xzalloc(32);
if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
- strcpy(date_fmt, "%Y-%m-%d");
+ strcpy(fmt_dt2str, "%Y-%m-%d");
if (ifmt > 0) {
i = 8;
- date_fmt[i++] = 'T';
- date_fmt[i++] = '%';
- date_fmt[i++] = 'H';
+ fmt_dt2str[i++] = 'T';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i++] = 'H';
if (ifmt > 1) {
- date_fmt[i++] = ':';
- date_fmt[i++] = '%';
- date_fmt[i++] = 'M';
- }
- if (ifmt > 2) {
- date_fmt[i++] = ':';
- date_fmt[i++] = '%';
- date_fmt[i++] = 'S';
+ fmt_dt2str[i++] = ':';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i++] = 'M';
+ if (ifmt > 2) {
+ fmt_dt2str[i++] = ':';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i++] = 'S';
+ }
}
format_utc:
- date_fmt[i++] = '%';
- date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
}
} else if (opt & DATE_OPT_RFC2822) {
/* Undo busybox.c for date -R */
if (ENABLE_LOCALE_SUPPORT)
setlocale(LC_TIME, "C");
- strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
+ strcpy(fmt_dt2str, "%a, %d %b %Y %H:%M:%S ");
i = 22;
goto format_utc;
} else /* default case */
- date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y";
+ fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y";
}
#define date_buf bb_common_bufsiz1
- if (*date_fmt == '\0') {
+ if (*fmt_dt2str == '\0') {
/* With no format string, just print a blank line */
date_buf[0] = '\0';
} else {
/* Handle special conversions */
-
- if (strncmp(date_fmt, "%f", 2) == 0) {
- date_fmt = (char*)"%Y.%m.%d-%H:%M:%S";
+ if (strncmp(fmt_dt2str, "%f", 2) == 0) {
+ fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
}
/* Generate output string */
- strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time);
+ strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time);
}
puts(date_buf);