svn commit: branches/busybox_scratch/coreutils

aldot at busybox.net aldot at busybox.net
Sun Aug 20 12:06:41 UTC 2006


Author: aldot
Date: 2006-08-20 05:06:41 -0700 (Sun, 20 Aug 2006)
New Revision: 15846

Log:
- fix, cleanup and shrink date. -I was completely broken, use bitmasks for flags, introduce maybe_set_utc to cut size down, redo output handling to reduce size
   text	   data	    bss	    dec	    hex	filename
   1783	      0	      0	   1783	    6f7	coreutils/date.o.orig
   1622	      0	      0	   1622	    656	coreutils/date.o.01k



Modified:
   branches/busybox_scratch/coreutils/date.c


Changeset:
Modified: branches/busybox_scratch/coreutils/date.c
===================================================================
--- branches/busybox_scratch/coreutils/date.c	2006-08-20 11:57:47 UTC (rev 15845)
+++ branches/busybox_scratch/coreutils/date.c	2006-08-20 12:06:41 UTC (rev 15846)
@@ -5,22 +5,19 @@
  * by Matthew Grant <grantma at anathoth.gen.nz>
  *
  * iso-format handling added by Robert Griebl <griebl at gmx.de>
+ * bugfixes and cleanup by Bernhard Fischer
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 */
 
-#include <stdlib.h>
+#include "busybox.h"
 #include <errno.h>
-#include <unistd.h>
 #include <time.h>
-#include <stdio.h>
-#include <string.h>
-#include "busybox.h"
 
 /* This 'date' command supports only 2 time setting formats,
    all the GNU strftime stuff (its in libc, lets use it),
-   setting time using UTC and displaying int, as well as
-   an RFC 822 complient date output for shell scripting
+   setting time using UTC and displaying it, as well as
+   an RFC 2822 compliant date output for shell scripting
    mail commands */
 
 /* Input parsing code is always bulky - used heavy duty libc stuff as
@@ -111,62 +108,67 @@
 #define DATE_OPT_TIMESPEC	0x20
 #define DATE_OPT_HINT		0x40
 
+static void maybe_set_utc(int utc)
+{
+	if ((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 set_time;
 	int utc;
 	time_t tm;
 	unsigned long opt;
 	struct tm tm_time;
 	char *filename = NULL;
-
-	int ifmt = 0;
+	int ifmt = -1;
 	char *isofmt_arg;
 	char *hintfmt_arg;
 
-	bb_opt_complementally = "?:d--s:s--d";
+	bb_opt_complementally = "?:d--s:s--d"
+		USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
 	opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:"
-		  		 	USE_FEATURE_DATE_ISOFMT("I::D:"),
+					USE_FEATURE_DATE_ISOFMT("I::D:"),
 					&date_str, &date_str, &filename
 					USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
-	set_time = opt & DATE_OPT_SET;
 	utc = opt & DATE_OPT_UTC;
-	if (utc && putenv("TZ=UTC0") != 0) {
-		bb_error_msg_and_die(bb_msg_memory_exhausted);
-	}
+	maybe_set_utc(utc);
 
-	if(ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
+	if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
 		if (!isofmt_arg) {
-			ifmt = 1;
+			ifmt = 0; /* default is date */
 		} else {
-			char *isoformats[]={"date","hours","minutes","seconds"};
-			for(ifmt = 4; ifmt;)
-				if(!strcmp(isofmt_arg,isoformats[--ifmt]))
+			const char * const isoformats[] =
+				{"date", "hours", "minutes", "seconds"};
+
+			for (ifmt = 0; ifmt < 4; ifmt++)
+				if (!strcmp(isofmt_arg, isoformats[ifmt])) {
 					break;
+				}
+			if (ifmt == 4) /* parse error */
+				bb_show_usage();
 		}
-		if (!ifmt) {
-			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) {
-		set_time = 1;
+		opt |= DATE_OPT_SET;
 		date_str = argv[optind];
 	}
 
 	/* 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 (filename) {
 		struct stat statbuf;
 		xstat(filename,&statbuf);
 		tm=statbuf.st_mtime;
-	} else time(&tm);
+	} else
+		time(&tm);
 	memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
 	/* Zero out fields - take her back to midnight! */
 	if (date_str != NULL) {
@@ -189,12 +191,10 @@
 		if (tm < 0) {
 			bb_error_msg_and_die(bb_msg_invalid_date, date_str);
 		}
-		if (utc && putenv("TZ=UTC0") != 0) {
-			bb_error_msg_and_die(bb_msg_memory_exhausted);
-		}
+		maybe_set_utc(utc);
 
 		/* if setting time, set it */
-		if (set_time && stime(&tm) < 0) {
+		if ((opt & DATE_OPT_SET) && stime(&tm) < 0) {
 			bb_perror_msg("cannot set date");
 		}
 	}
@@ -202,33 +202,43 @@
 	/* Display output */
 
 	/* Deal with format string */
+
 	if (date_fmt == NULL) {
-		/* Start with the default case */
-		
-		date_fmt = (opt & DATE_OPT_RFC2822 ?
-					(utc ? "%a, %d %b %Y %H:%M:%S GMT" :
-					"%a, %d %b %Y %H:%M:%S %z") :
-					"%a %b %e %H:%M:%S %Z %Y");
+		int i;
+		date_fmt = xzalloc(32);
+		if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
+			strcpy(date_fmt, "%Y-%m-%d");
+			if (ifmt > 0) {
+				i = 8;
+				date_fmt[i++] = 'T';
+				date_fmt[i++] = '%';
+				date_fmt[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';
+				}
+format_utc:
+				date_fmt[i++] = '%';
+				date_fmt[i] = utc ? 'Z' : 'z';
+			}
+		} else if (opt & DATE_OPT_RFC2822) {
+			strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
+			i = 22;
+			goto format_utc;
+		} else /* default case */
+			date_fmt = "%a %b %e %H:%M:%S %Z %Y";
+	}
 
-		if (ENABLE_FEATURE_DATE_ISOFMT) {
-			if (ifmt == 4)
-				date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
-			else if (ifmt == 3)
-				date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
-			else if (ifmt == 2) 
-				date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
-			else if (ifmt == 1)
-				date_fmt = "%Y-%m-%d";
-		}
-	}
-	
 	if (*date_fmt == '\0') {
-
 		/* With no format string, just print a blank line */
-		
-		*bb_common_bufsiz1=0;
+		*bb_common_bufsiz1 = 0;
 	} else {
-
 		/* Handle special conversions */
 
 		if (strncmp(date_fmt, "%f", 2) == 0) {




More information about the busybox-cvs mailing list