+/*
+ * Mini date implementation for busybox
+ *
+ * by Matthew Grant <grantma@anathoth.gen.nz>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+*/
+
#include "internal.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
-#include <getopt.h>
/* This 'date' command supports only 2 time setting formats,
an RFC 822 complient date output for shell scripting
mail commands */
-const char date_usage[] = "date [-uR] [+FORMAT|+%f] [ [-s|-d] MMDDhhmm[[CC]YY]\n | [[[[CCYY.]MM.DD-]hh:mm[:ss]]]] ]";
-
-static struct option const long_options[] =
-{
- {"date", required_argument, NULL, 'd'},
- /* {"rfc-822", no_argument, NULL, 'R'},
- {"set", required_argument, NULL, 's'},
- {"uct", no_argument, NULL, 'u'},
- {"utc", no_argument, NULL, 'u'},
- {"universal", no_argument, NULL, 'u'}, */
- {NULL, 0, NULL, 0}
-};
-
+static const char date_usage[] = "date [OPTION]... [+FORMAT]\n"
+" or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\n"
+"Display the current time in the given FORMAT, or set the system date.\n"
+"\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n"
+"\t-s\t\tset time described by STRING\n"
+"\t-u\t\tprint or set Coordinated Universal Time\n";
/* Input parsing code is always bulky - used heavy duty libc stuff as
if(nr < 4 || nr > 5) {
fprintf(stderr, "date: invalid date `%s'\n", t_string);
- exit(1);
+ exit( FALSE);
}
/* correct for century - minor Y2K problem here? */
fprintf(stderr, "date: invalid date `%s'\n", t_string);
- exit(1);
+ exit( FALSE);
}
-void
-date_err(void) {
- fprintf(stderr, "date: only one date argument can be given at a time.\n");
- exit(1);
-}
-
int
-date_main(struct FileInfo * i, int argc, char * * argv)
+date_main(int argc, char * * argv)
{
char *date_str = NULL;
char *date_fmt = NULL;
char *t_buff;
+ int i;
int set_time = 0;
int rfc822 = 0;
int utc = 0;
int use_arg = 0;
- int n_args;
time_t tm;
struct tm tm_time;
- char optc;
/* Interpret command line args */
-
-
- while ((optc = getopt_long (argc, argv, "d:Rs:u", long_options, NULL))
- != EOF) {
- switch (optc) {
- case 0:
- break;
-
- case 'R':
- rfc822 = 1;
- break;
-
- case 's':
- set_time = 1;
- if(date_str != NULL) date_err();
- date_str = optarg;
- break;
-
- case 'u':
- utc = 1;
- if (putenv ("TZ=UTC0") != 0) {
- fprintf(stderr,"date: memory exhausted\n");
- return(1);
- }
-#if LOCALTIME_CACHE
- tzset ();
-#endif break;
-
- case 'd':
- use_arg = 1;
- if(date_str != NULL) date_err();
- date_str = optarg;
- break;
-
- default:
- usage(date_usage);
- break;
+ i = --argc;
+ argv++;
+ while (i > 0 && **argv) {
+ if (**argv == '-') {
+ while (i>0 && *++(*argv)) switch (**argv) {
+ case 'R':
+ rfc822 = 1;
+ break;
+ case 's':
+ set_time = 1;
+ if(date_str != NULL) usage ( date_usage);
+ date_str = optarg;
+ break;
+ case 'u':
+ utc = 1;
+ if (putenv ("TZ=UTC0") != 0) {
+ fprintf(stderr,"date: memory exhausted\n");
+ exit( FALSE);
+ }
+ /* Look ma, no break. Don't fix it either. */
+ case 'd':
+ use_arg = 1;
+ if(date_str != NULL) usage ( date_usage);
+ date_str = optarg;
+ break;
+ case '-':
+ usage ( date_usage);
+ }
+ } else {
+ if ( (date_fmt == NULL) && (strcmp(*argv, "+")==0) )
+ date_fmt=*argv;
+ else if (date_str == NULL) {
+ set_time = 1;
+ date_str=*argv;
+ } else {
+ usage ( date_usage);
+ }
+ }
+ i--;
+ argv++;
}
- }
-
-
- n_args = argc - optind;
-
- while (n_args--){
- switch(argv[optind][0]) {
- case '+':
- /* Date format strings */
- if(date_fmt != NULL) {
- fprintf(stderr, "date: only one date format can be given.\n");
- return(1);
- }
- date_fmt = &argv[optind][1];
- break;
-
- case '\0':
- break;
-
- default:
- /* Anything left over must be a date string to set the time */
- set_time = 1;
- if(date_str != NULL) date_err();
- date_str = argv[optind];
- break;
- }
- optind++;
- }
/* Now we have parsed all the information except the date format
tm = mktime(&tm_time);
if (tm < 0 ) {
fprintf(stderr, "date: invalid date `%s'\n", date_str);
- exit(1);
+ exit( FALSE);
}
/* if setting time, set it */
if(set_time) {
if( stime(&tm) < 0) {
fprintf(stderr, "date: can't set date.\n");
- exit(1);
+ exit( FALSE);
}
}
}
} else if ( *date_fmt == '\0' ) {
/* Imitate what GNU 'date' does with NO format string! */
printf ("\n");
- return(0);
+ exit( TRUE);
}
/* Handle special conversions */
strftime(t_buff, 200, date_fmt, &tm_time);
printf("%s\n", t_buff);
- return(0);
+ exit( TRUE);
}
+