svn commit: branches/busybox_scratch/coreutils
aldot at busybox.net
aldot at busybox.net
Sun Aug 20 12:10:00 UTC 2006
Author: aldot
Date: 2006-08-20 05:10:00 -0700 (Sun, 20 Aug 2006)
New Revision: 15847
Log:
- shrink date: fold date_conv_time and date_conv_ftime into single caller to help optimizers.
text data bss dec hex filename
1622 0 0 1622 656 coreutils/date.o.01k
1441 0 0 1441 5a1 coreutils/date.o
Modified:
branches/busybox_scratch/coreutils/date.c
Changeset:
Modified: branches/busybox_scratch/coreutils/date.c
===================================================================
--- branches/busybox_scratch/coreutils/date.c 2006-08-20 12:06:41 UTC (rev 15846)
+++ branches/busybox_scratch/coreutils/date.c 2006-08-20 12:10:00 UTC (rev 15847)
@@ -25,81 +25,7 @@
/* Default input handling to save surprising some people */
-static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
-{
- int nr;
- char *cp;
- nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
- &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
- &(tm_time->tm_year));
-
- if (nr < 4 || nr > 5) {
- bb_error_msg_and_die(bb_msg_invalid_date, t_string);
- }
-
- cp = strchr(t_string, '.');
- if (cp) {
- nr = sscanf(cp + 1, "%2d", &(tm_time->tm_sec));
- if (nr != 1) {
- bb_error_msg_and_die(bb_msg_invalid_date, t_string);
- }
- }
-
- /* correct for century - minor Y2K problem here? */
- if (tm_time->tm_year >= 1900) {
- tm_time->tm_year -= 1900;
- }
- /* adjust date */
- tm_time->tm_mon -= 1;
-
- return (tm_time);
-
-}
-
-
-/* The new stuff for LRP */
-
-static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
-{
- struct tm t;
-
- /* Parse input and assign appropriately to tm_time */
-
- if (t = *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
- &t.tm_sec) == 3) {
- /* no adjustments needed */
- } else if (t = *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
- &t.tm_min) == 2) {
- /* no adjustments needed */
- } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
- &t.tm_mday, &t.tm_hour,
- &t.tm_min, &t.tm_sec) == 5) {
- /* Adjust dates from 1-12 to 0-11 */
- t.tm_mon -= 1;
- } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
- &t.tm_mday,
- &t.tm_hour, &t.tm_min) == 4) {
- /* Adjust dates from 1-12 to 0-11 */
- t.tm_mon -= 1;
- } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
- &t.tm_mon, &t.tm_mday,
- &t.tm_hour, &t.tm_min,
- &t.tm_sec) == 6) {
- t.tm_year -= 1900; /* Adjust years */
- t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
- } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
- &t.tm_mon, &t.tm_mday,
- &t.tm_hour, &t.tm_min) == 5) {
- t.tm_year -= 1900; /* Adjust years */
- t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
- } else {
- bb_error_msg_and_die(bb_msg_invalid_date, t_string);
- }
- *tm_time = t;
- return (tm_time);
-}
-
#define DATE_OPT_RFC2822 0x01
#define DATE_OPT_SET 0x02
#define DATE_OPT_UTC 0x04
@@ -108,22 +34,21 @@
#define DATE_OPT_TIMESPEC 0x20
#define DATE_OPT_HINT 0x40
-static void maybe_set_utc(int utc)
+static void maybe_set_utc(int opt)
{
- if ((utc ) && putenv("TZ=UTC0") != 0)
+ if ((opt & DATE_OPT_UTC) && putenv("TZ=UTC0") != 0)
bb_error_msg_and_die(bb_msg_memory_exhausted);
}
int date_main(int argc, char **argv)
{
- char *date_str = NULL;
- char *date_fmt = NULL;
- int utc;
time_t tm;
- unsigned long opt;
struct tm tm_time;
- char *filename = NULL;
+ unsigned long opt;
int ifmt = -1;
+ char *date_str = NULL;
+ char *date_fmt = NULL;
+ char *filename = NULL;
char *isofmt_arg;
char *hintfmt_arg;
@@ -133,8 +58,7 @@
USE_FEATURE_DATE_ISOFMT("I::D:"),
&date_str, &date_str, &filename
USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
- utc = opt & DATE_OPT_UTC;
- maybe_set_utc(utc);
+ maybe_set_utc(opt);
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
if (!isofmt_arg) {
@@ -165,8 +89,8 @@
if (filename) {
struct stat statbuf;
- xstat(filename,&statbuf);
- tm=statbuf.st_mtime;
+ xstat(filename, &statbuf);
+ tm = statbuf.st_mtime;
} else
time(&tm);
memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
@@ -180,9 +104,64 @@
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
strptime(date_str, hintfmt_arg, &tm_time);
} else if (strchr(date_str, ':') != NULL) {
- date_conv_ftime(&tm_time, date_str);
+ /* Parse input and assign appropriately to tm_time */
+
+ if (sscanf(date_str, "%d:%d:%d", &tm_time.tm_hour, &tm_time.tm_min,
+ &tm_time.tm_sec) == 3) {
+ /* no adjustments needed */
+ } else if (sscanf(date_str, "%d:%d", &tm_time.tm_hour,
+ &tm_time.tm_min) == 2) {
+ /* no adjustments needed */
+ } else if (sscanf(date_str, "%d.%d-%d:%d:%d", &tm_time.tm_mon,
+ &tm_time.tm_mday, &tm_time.tm_hour,
+ &tm_time.tm_min, &tm_time.tm_sec) == 5) {
+ /* Adjust dates from 1-12 to 0-11 */
+ tm_time.tm_mon -= 1;
+ } else if (sscanf(date_str, "%d.%d-%d:%d", &tm_time.tm_mon,
+ &tm_time.tm_mday,
+ &tm_time.tm_hour, &tm_time.tm_min) == 4) {
+ /* Adjust dates from 1-12 to 0-11 */
+ tm_time.tm_mon -= 1;
+ } else if (sscanf(date_str, "%d.%d.%d-%d:%d:%d", &tm_time.tm_year,
+ &tm_time.tm_mon, &tm_time.tm_mday,
+ &tm_time.tm_hour, &tm_time.tm_min,
+ &tm_time.tm_sec) == 6) {
+ tm_time.tm_year -= 1900; /* Adjust years */
+ tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
+ } else if (sscanf(date_str, "%d.%d.%d-%d:%d", &tm_time.tm_year,
+ &tm_time.tm_mon, &tm_time.tm_mday,
+ &tm_time.tm_hour, &tm_time.tm_min) == 5) {
+ tm_time.tm_year -= 1900; /* Adjust years */
+ tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
+ } else {
+ bb_error_msg_and_die(bb_msg_invalid_date, date_str);
+ }
} else {
- date_conv_time(&tm_time, date_str);
+ int nr;
+ char *cp;
+
+ nr = sscanf(date_str, "%2d%2d%2d%2d%d", &tm_time.tm_mon,
+ &tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,
+ &tm_time.tm_year);
+
+ if (nr < 4 || nr > 5) {
+ bb_error_msg_and_die(bb_msg_invalid_date, date_str);
+ }
+
+ cp = strchr(date_str, '.');
+ if (cp) {
+ nr = sscanf(cp + 1, "%2d", &tm_time.tm_sec);
+ if (nr != 1) {
+ bb_error_msg_and_die(bb_msg_invalid_date, date_str);
+ }
+ }
+
+ /* correct for century - minor Y2K problem here? */
+ if (tm_time.tm_year >= 1900) {
+ tm_time.tm_year -= 1900;
+ }
+ /* adjust date */
+ tm_time.tm_mon -= 1;
}
/* Correct any day of week and day of year etc. fields */
@@ -191,7 +170,7 @@
if (tm < 0) {
bb_error_msg_and_die(bb_msg_invalid_date, date_str);
}
- maybe_set_utc(utc);
+ maybe_set_utc(opt);
/* if setting time, set it */
if ((opt & DATE_OPT_SET) && stime(&tm) < 0) {
@@ -225,7 +204,7 @@
}
format_utc:
date_fmt[i++] = '%';
- date_fmt[i] = utc ? 'Z' : 'z';
+ date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
}
} else if (opt & DATE_OPT_RFC2822) {
strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
More information about the busybox-cvs
mailing list