X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fdate.c;h=70484e2cd3484f89bd02ebaf52dbe734a634c832;hb=4905434b8aead249d6bdd134d2fbd8c06dfcc059;hp=1aa3e22d23d413e49196f04dbede0421a5715ce9;hpb=c2266bd51990883ff6f7de4a4ca22a4bef2050c9;p=oweals%2Fbusybox.git diff --git a/coreutils/date.c b/coreutils/date.c index 1aa3e22d2..70484e2cd 100644 --- a/coreutils/date.c +++ b/coreutils/date.c @@ -3,7 +3,7 @@ * Mini date implementation for busybox * * by Matthew Grant - * + * * iso-format handling added by Robert Griebl * * This program is free software; you can redistribute it and/or modify @@ -33,7 +33,7 @@ #include "busybox.h" -/* This 'date' command supports only 2 time setting formats, +/* 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 @@ -42,11 +42,12 @@ /* Input parsing code is always bulky - used heavy duty libc stuff as much as possible, missed out a lot of bounds checking */ -/* Default input handling to save suprising some people */ +/* 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), @@ -56,6 +57,14 @@ static struct tm *date_conv_time(struct tm *tm_time, const char *t_string) 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; @@ -118,21 +127,22 @@ static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string) #define DATE_OPT_SET 0x02 #define DATE_OPT_UTC 0x04 #define DATE_OPT_DATE 0x08 +#define DATE_OPT_REFERENCE 0x10 #ifdef CONFIG_FEATURE_DATE_ISOFMT -# define DATE_OPT_TIMESPEC 0x10 +# define DATE_OPT_TIMESPEC 0x20 #endif int date_main(int argc, char **argv) { char *date_str = NULL; char *date_fmt = NULL; - char *t_buff; int set_time; int utc; int use_arg = 0; time_t tm; unsigned long opt; struct tm tm_time; + char *filename = NULL; #ifdef CONFIG_FEATURE_DATE_ISOFMT int ifmt = 0; @@ -143,8 +153,8 @@ int date_main(int argc, char **argv) # define GETOPT_ISOFMT #endif bb_opt_complementaly = "d~ds:s~ds"; - opt = bb_getopt_ulflags(argc, argv, "Rs:ud:" GETOPT_ISOFMT, - &date_str, &date_str + opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT, + &date_str, &date_str, &filename #ifdef CONFIG_FEATURE_DATE_ISOFMT , &isofmt_arg #endif @@ -155,7 +165,7 @@ int date_main(int argc, char **argv) bb_error_msg_and_die(bb_msg_memory_exhausted); } use_arg = opt & DATE_OPT_DATE; - if(opt & 0x80000000UL) + if(opt & BB_GETOPT_ERROR) bb_show_usage(); #ifdef CONFIG_FEATURE_DATE_ISOFMT if(opt & DATE_OPT_TIMESPEC) { @@ -194,7 +204,12 @@ int date_main(int argc, char **argv) /* Now we have parsed all the information except the date format which depends on whether the clock is being set or read */ - time(&tm); + if(filename) { + struct stat statbuf; + if(stat(filename,&statbuf)) + bb_perror_msg_and_die("File '%s' not found.\n",filename); + 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_str != NULL) { @@ -246,10 +261,10 @@ int date_main(int argc, char **argv) case 0: default: #endif - date_fmt = - (opt & DATE_OPT_RFC2822 - ? (utc ? "%a, %e %b %Y %H:%M:%S GMT" : - "%a, %e %b %Y %H:%M:%S %z") : "%a %b %e %H:%M:%S %Z %Y"); + 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"); #ifdef CONFIG_FEATURE_DATE_ISOFMT break; @@ -267,10 +282,13 @@ int date_main(int argc, char **argv) date_fmt = "%Y.%m.%d-%H:%M:%S"; } - /* Print OUTPUT (after ALL that!) */ - t_buff = xmalloc(201); - strftime(t_buff, 200, date_fmt, &tm_time); - puts(t_buff); + { + /* Print OUTPUT (after ALL that!) */ + RESERVE_CONFIG_BUFFER(t_buff, 201); + strftime(t_buff, 200, date_fmt, &tm_time); + puts(t_buff); + RELEASE_CONFIG_BUFFER(t_buff); + } return EXIT_SUCCESS; }