aboutsummaryrefslogtreecommitdiff
path: root/coreutils/date.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-25 02:43:34 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-25 02:43:34 +0000
commit9ce4ae8142b191ab6c2135165d5904351b48f665 (patch)
tree0064cf06ce484a14cacfecff0299b3f32c8ec1dd /coreutils/date.c
parent92e13c2a11381eec06a382de6917369ded240804 (diff)
downloadbusybox-w32-9ce4ae8142b191ab6c2135165d5904351b48f665.tar.gz
busybox-w32-9ce4ae8142b191ab6c2135165d5904351b48f665.tar.bz2
busybox-w32-9ce4ae8142b191ab6c2135165d5904351b48f665.zip
date: make help text more understandable; small shrink
text data bss dec hex filename 799025 641 7380 807046 c5086 busybox_old 799009 641 7380 807030 c5076 busybox_unstripped
Diffstat (limited to 'coreutils/date.c')
-rw-r--r--coreutils/date.c104
1 files changed, 51 insertions, 53 deletions
diff --git a/coreutils/date.c b/coreutils/date.c
index a8e339333..064f758e2 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -39,70 +39,70 @@ static void maybe_set_utc(int opt)
39} 39}
40 40
41int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 41int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int date_main(int argc, char **argv) 42int date_main(int argc ATTRIBUTE_UNUSED, char **argv)
43{ 43{
44 time_t tm;
45 struct tm tm_time; 44 struct tm tm_time;
45 time_t tm;
46 unsigned opt; 46 unsigned opt;
47 int ifmt = -1; 47 int ifmt = -1;
48 char *date_str = NULL; 48 char *date_str;
49 char *date_fmt = NULL; 49 char *fmt_dt2str;
50 char *filename = NULL; 50 char *fmt_str2dt;
51 char *isofmt_arg; 51 char *filename;
52 char *hintfmt_arg; 52 char *isofmt_arg = NULL;
53 53
54 opt_complementary = "d--s:s--d" 54 opt_complementary = "d--s:s--d"
55 USE_FEATURE_DATE_ISOFMT(":R--I:I--R"); 55 USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
56 opt = getopt32(argv, "Rs:ud:r:" 56 opt = getopt32(argv, "Rs:ud:r:"
57 USE_FEATURE_DATE_ISOFMT("I::D:"), 57 USE_FEATURE_DATE_ISOFMT("I::D:"),
58 &date_str, &date_str, &filename 58 &date_str, &date_str, &filename
59 USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg)); 59 USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt));
60 argv += optind;
60 maybe_set_utc(opt); 61 maybe_set_utc(opt);
61 62
62 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) { 63 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
63 if (!isofmt_arg) { 64 ifmt = 0; /* default is date */
64 ifmt = 0; /* default is date */ 65 if (isofmt_arg) {
65 } else { 66 static const char isoformats[] ALIGN1 =
66 static const char *const isoformats[] = { 67 "date\0""hours\0""minutes\0""seconds\0";
67 "date", "hours", "minutes", "seconds" 68 ifmt = index_in_strings(isoformats, isofmt_arg);
68 }; 69 if (ifmt < 0)
69 70 bb_show_usage();
70 for (ifmt = 0; ifmt < 4; ifmt++)
71 if (!strcmp(isofmt_arg, isoformats[ifmt]))
72 goto found;
73 /* parse error */
74 bb_show_usage();
75 found: ;
76 } 71 }
77 } 72 }
78 73
79 /* XXX, date_fmt == NULL from this always */ 74 fmt_dt2str = NULL;
80 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) { 75 if (argv[0] && argv[0][0] == '+') {
81 date_fmt = &argv[optind][1]; /* Skip over the '+' */ 76 fmt_dt2str = &argv[0][1]; /* Skip over the '+' */
82 } else if (date_str == NULL) { 77 argv++;
78 }
79 if (!(opt & (DATE_OPT_SET | DATE_OPT_DATE))) {
83 opt |= DATE_OPT_SET; 80 opt |= DATE_OPT_SET;
84 date_str = argv[optind]; 81 date_str = argv[0]; /* can be NULL */
85 } 82 }
86 83
87 /* Now we have parsed all the information except the date format 84 /* Now we have parsed all the information except the date format
88 which depends on whether the clock is being set or read */ 85 which depends on whether the clock is being set or read */
89 86
90 if (filename) { 87 if (opt & DATE_OPT_REFERENCE) {
91 struct stat statbuf; 88 struct stat statbuf;
92 xstat(filename, &statbuf); 89 xstat(filename, &statbuf);
93 tm = statbuf.st_mtime; 90 tm = statbuf.st_mtime;
94 } else 91 } else
95 time(&tm); 92 time(&tm);
96 memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); 93 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
97 /* Zero out fields - take her back to midnight! */ 94
95 /* If date string is given, update tm_time, and maybe set date */
98 if (date_str != NULL) { 96 if (date_str != NULL) {
97 /* Zero out fields - take her back to midnight! */
99 tm_time.tm_sec = 0; 98 tm_time.tm_sec = 0;
100 tm_time.tm_min = 0; 99 tm_time.tm_min = 0;
101 tm_time.tm_hour = 0; 100 tm_time.tm_hour = 0;
102 101
103 /* Process any date input to UNIX time since 1 Jan 1970 */ 102 /* Process any date input to UNIX time since 1 Jan 1970 */
104 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) { 103 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
105 strptime(date_str, hintfmt_arg, &tm_time); 104 if (strptime(date_str, fmt_str2dt, &tm_time) == NULL)
105 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
106 } else if (strchr(date_str, ':') != NULL) { 106 } else if (strchr(date_str, ':') != NULL) {
107 /* Parse input and assign appropriately to tm_time */ 107 /* Parse input and assign appropriately to tm_time */
108 108
@@ -181,55 +181,53 @@ int date_main(int argc, char **argv)
181 /* Display output */ 181 /* Display output */
182 182
183 /* Deal with format string */ 183 /* Deal with format string */
184 184 if (fmt_dt2str == NULL) {
185 if (date_fmt == NULL) {
186 int i; 185 int i;
187 date_fmt = xzalloc(32); 186 fmt_dt2str = xzalloc(32);
188 if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) { 187 if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
189 strcpy(date_fmt, "%Y-%m-%d"); 188 strcpy(fmt_dt2str, "%Y-%m-%d");
190 if (ifmt > 0) { 189 if (ifmt > 0) {
191 i = 8; 190 i = 8;
192 date_fmt[i++] = 'T'; 191 fmt_dt2str[i++] = 'T';
193 date_fmt[i++] = '%'; 192 fmt_dt2str[i++] = '%';
194 date_fmt[i++] = 'H'; 193 fmt_dt2str[i++] = 'H';
195 if (ifmt > 1) { 194 if (ifmt > 1) {
196 date_fmt[i++] = ':'; 195 fmt_dt2str[i++] = ':';
197 date_fmt[i++] = '%'; 196 fmt_dt2str[i++] = '%';
198 date_fmt[i++] = 'M'; 197 fmt_dt2str[i++] = 'M';
199 } 198 if (ifmt > 2) {
200 if (ifmt > 2) { 199 fmt_dt2str[i++] = ':';
201 date_fmt[i++] = ':'; 200 fmt_dt2str[i++] = '%';
202 date_fmt[i++] = '%'; 201 fmt_dt2str[i++] = 'S';
203 date_fmt[i++] = 'S'; 202 }
204 } 203 }
205 format_utc: 204 format_utc:
206 date_fmt[i++] = '%'; 205 fmt_dt2str[i++] = '%';
207 date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z'; 206 fmt_dt2str[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
208 } 207 }
209 } else if (opt & DATE_OPT_RFC2822) { 208 } else if (opt & DATE_OPT_RFC2822) {
210 /* Undo busybox.c for date -R */ 209 /* Undo busybox.c for date -R */
211 if (ENABLE_LOCALE_SUPPORT) 210 if (ENABLE_LOCALE_SUPPORT)
212 setlocale(LC_TIME, "C"); 211 setlocale(LC_TIME, "C");
213 strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S "); 212 strcpy(fmt_dt2str, "%a, %d %b %Y %H:%M:%S ");
214 i = 22; 213 i = 22;
215 goto format_utc; 214 goto format_utc;
216 } else /* default case */ 215 } else /* default case */
217 date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y"; 216 fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y";
218 } 217 }
219 218
220#define date_buf bb_common_bufsiz1 219#define date_buf bb_common_bufsiz1
221 if (*date_fmt == '\0') { 220 if (*fmt_dt2str == '\0') {
222 /* With no format string, just print a blank line */ 221 /* With no format string, just print a blank line */
223 date_buf[0] = '\0'; 222 date_buf[0] = '\0';
224 } else { 223 } else {
225 /* Handle special conversions */ 224 /* Handle special conversions */
226 225 if (strncmp(fmt_dt2str, "%f", 2) == 0) {
227 if (strncmp(date_fmt, "%f", 2) == 0) { 226 fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
228 date_fmt = (char*)"%Y.%m.%d-%H:%M:%S";
229 } 227 }
230 228
231 /* Generate output string */ 229 /* Generate output string */
232 strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time); 230 strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time);
233 } 231 }
234 puts(date_buf); 232 puts(date_buf);
235 233