From 18b14e52a577754db66fe46a6ece5f2a06a7a83a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 17 Jul 2012 23:11:06 -0500 Subject: Largely cosmetic code cleanups. --- toys/date.c | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) (limited to 'toys/date.c') diff --git a/toys/date.c b/toys/date.c index ebcdeb1f..7881f464 100644 --- a/toys/date.c +++ b/toys/date.c @@ -6,7 +6,7 @@ * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html -USE_DATE(NEWTOY(date, "u", TOYFLAG_BIN)) +USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN)) config DATE bool "date" @@ -23,8 +23,7 @@ config DATE static int fromdec(const char *buf, int len) { int result = 0; - while (len--) - result=result * 10 + (*buf++ - '0'); + while (len--) result=result * 10 + (*buf++ - '0'); return result; } @@ -36,16 +35,13 @@ void date_main(void) if (!toys.optargs[0] || toys.optargs[0][0] == '+') { time_t now = time(NULL); struct tm *tm; - if (toys.optargs[0]) - format_string = &toys.optargs[0][1]; - if (toys.optflags) - tm = gmtime(&now); - else - tm = localtime(&now); - if (!tm) - perror_msg("Unable to retrieve current time"); + + if (toys.optargs[0]) format_string = toys.optargs[0]+1; + if (toys.optflags) tm = gmtime(&now); + else tm = localtime(&now); + if (!tm) perror_msg("Unable to retrieve current time"); if (!strftime(toybuf, sizeof(toybuf), format_string, tm)) - perror_msg("invalid format string `%s'", format_string); + perror_msg("bad format `%s'", format_string); puts(toybuf); } else { int len = strlen(toys.optargs[0]); @@ -53,30 +49,26 @@ void date_main(void) struct timeval tv; if (len < 8 || len > 12 || len & 1) - error_msg("invalid date `%s'", toys.optargs[0]); + error_msg("bad date `%s'", toys.optargs[0]); memset(&tm, 0, sizeof(tm)); /* Date format: mmddhhmm[[cc]yy] */ tm.tm_mon = fromdec(toys.optargs[0], 2) - 1; tm.tm_mday = fromdec(&toys.optargs[0][2], 2); tm.tm_hour = fromdec(&toys.optargs[0][4], 2); tm.tm_min = fromdec(&toys.optargs[0][6], 2); - if (len == 12) - tm.tm_year = fromdec(&toys.optargs[0][8], 4) - 1900; + if (len == 12) tm.tm_year = fromdec(&toys.optargs[0][8], 4) - 1900; else if (len == 10) { tm.tm_year = fromdec(&toys.optargs[0][8], 2); /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */ - if (tm.tm_year < 69) - tm.tm_year += 100; + if (tm.tm_year < 69) tm.tm_year += 100; } else { /* Year not specified, so retrieve current year */ time_t now = time(NULL); struct tm *now_tm = localtime(&now); - if (!now_tm) - perror_msg("Unable to retrieve current time"); + if (!now_tm) perror_msg("Unable to retrieve current time"); tm.tm_year = now_tm->tm_year; } - if (!toys.optflags) - tv.tv_sec = mktime(&tm); + if (!toys.optflags) tv.tv_sec = mktime(&tm); else { /* Get the UTC version of a struct tm */ char *tz = NULL; @@ -84,20 +76,17 @@ void date_main(void) setenv("TZ", "", 1); tzset(); tv.tv_sec = mktime(&tm); - if (tz) - setenv("TZ", tz, 1); - else - unsetenv("TZ"); + if (tz) setenv("TZ", tz, 1); + else unsetenv("TZ"); tzset(); } if (tv.tv_sec == (time_t)-1) - error_msg("invalid date `%s'", toys.optargs[0]); + error_msg("bad `%s'", toys.optargs[0]); tv.tv_usec = 0; if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) - perror_msg("invalid format string `%s'", format_string); + perror_msg("bad format `%s'", format_string); puts(toybuf); - if (settimeofday(&tv, NULL) < 0) - perror_msg("cannot set date"); + if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date"); } } -- cgit v1.2.3