diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /coreutils/date.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'coreutils/date.c')
-rw-r--r-- | coreutils/date.c | 451 |
1 files changed, 223 insertions, 228 deletions
diff --git a/coreutils/date.c b/coreutils/date.c index a3528921d..b4c3e7153 100644 --- a/coreutils/date.c +++ b/coreutils/date.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini date implementation for busybox | 3 | * Mini date implementation for busybox |
3 | * | 4 | * |
@@ -38,12 +39,13 @@ | |||
38 | an RFC 822 complient date output for shell scripting | 39 | an RFC 822 complient date output for shell scripting |
39 | mail commands */ | 40 | mail commands */ |
40 | 41 | ||
41 | static const char date_usage[] = "date [OPTION]... [+FORMAT]\n" | 42 | static const char date_usage[] = "date [OPTION]... [+FORMAT]\n" |
42 | " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\n" | 43 | " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\n" |
43 | "Display the current time in the given FORMAT, or set the system date.\n" | 44 | "Display the current time in the given FORMAT, or set the system date.\n" |
44 | "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n" | 45 | "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n" |
45 | "\t-s\t\tset time described by STRING\n" | 46 | "\t-s\t\tset time described by STRING\n" |
46 | "\t-u\t\tprint or set Coordinated Universal Time\n"; | 47 | |
48 | "\t-u\t\tprint or set Coordinated Universal Time\n"; | ||
47 | 49 | ||
48 | 50 | ||
49 | /* Input parsing code is always bulky - used heavy duty libc stuff as | 51 | /* Input parsing code is always bulky - used heavy duty libc stuff as |
@@ -51,240 +53,233 @@ static const char date_usage[] = "date [OPTION]... [+FORMAT]\n" | |||
51 | 53 | ||
52 | /* Default input handling to save suprising some people */ | 54 | /* Default input handling to save suprising some people */ |
53 | 55 | ||
54 | struct tm * | 56 | struct tm *date_conv_time(struct tm *tm_time, const char *t_string) |
55 | date_conv_time(struct tm *tm_time, const char *t_string) { | 57 | { |
56 | int nr; | 58 | int nr; |
57 | 59 | ||
58 | nr = sscanf(t_string, "%2d%2d%2d%2d%d", | 60 | nr = sscanf(t_string, "%2d%2d%2d%2d%d", |
59 | &(tm_time->tm_mon), | 61 | &(tm_time->tm_mon), |
60 | &(tm_time->tm_mday), | 62 | &(tm_time->tm_mday), |
61 | &(tm_time->tm_hour), | 63 | &(tm_time->tm_hour), |
62 | &(tm_time->tm_min), | 64 | &(tm_time->tm_min), &(tm_time->tm_year)); |
63 | &(tm_time->tm_year)); | 65 | |
64 | 66 | if (nr < 4 || nr > 5) { | |
65 | if(nr < 4 || nr > 5) { | 67 | fprintf(stderr, invalid_date, "date", t_string); |
66 | fprintf(stderr, invalid_date, "date", t_string); | 68 | exit(FALSE); |
67 | exit( FALSE); | 69 | } |
68 | } | 70 | |
69 | 71 | /* correct for century - minor Y2K problem here? */ | |
70 | /* correct for century - minor Y2K problem here? */ | 72 | if (tm_time->tm_year >= 1900) |
71 | if(tm_time->tm_year >= 1900) | 73 | tm_time->tm_year -= 1900; |
72 | tm_time->tm_year -= 1900; | 74 | /* adjust date */ |
73 | /* adjust date */ | 75 | tm_time->tm_mon -= 1; |
74 | tm_time->tm_mon -= 1; | 76 | |
75 | 77 | return (tm_time); | |
76 | return(tm_time); | ||
77 | 78 | ||
78 | } | 79 | } |
79 | 80 | ||
80 | 81 | ||
81 | /* The new stuff for LRP */ | 82 | /* The new stuff for LRP */ |
82 | 83 | ||
83 | struct tm * | 84 | struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string) |
84 | date_conv_ftime(struct tm *tm_time, const char *t_string) { | 85 | { |
85 | struct tm itm_time, jtm_time, ktm_time, \ | 86 | struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time; |
86 | ltm_time, mtm_time, ntm_time; | 87 | |
87 | 88 | itm_time = *tm_time; | |
88 | itm_time = *tm_time; | 89 | jtm_time = *tm_time; |
89 | jtm_time = *tm_time; | 90 | ktm_time = *tm_time; |
90 | ktm_time = *tm_time; | 91 | ltm_time = *tm_time; |
91 | ltm_time = *tm_time; | 92 | mtm_time = *tm_time; |
92 | mtm_time = *tm_time; | 93 | ntm_time = *tm_time; |
93 | ntm_time = *tm_time; | 94 | |
94 | 95 | /* Parse input and assign appropriately to tm_time */ | |
95 | /* Parse input and assign appropriately to tm_time */ | 96 | |
96 | 97 | if (sscanf(t_string, "%d:%d:%d", | |
97 | if(sscanf(t_string, "%d:%d:%d", | 98 | &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) { |
98 | &itm_time.tm_hour, | 99 | |
99 | &itm_time.tm_min, | 100 | *tm_time = itm_time; |
100 | &itm_time.tm_sec) == 3 ) { | 101 | return (tm_time); |
101 | 102 | ||
102 | *tm_time = itm_time; | 103 | } else if (sscanf(t_string, "%d:%d", |
103 | return(tm_time); | 104 | &jtm_time.tm_hour, &jtm_time.tm_min) == 2) { |
104 | 105 | ||
105 | } else if (sscanf(t_string, "%d:%d", | 106 | *tm_time = jtm_time; |
106 | &jtm_time.tm_hour, | 107 | return (tm_time); |
107 | &jtm_time.tm_min) == 2) { | 108 | |
108 | 109 | } else if (sscanf(t_string, "%d.%d-%d:%d:%d", | |
109 | *tm_time = jtm_time; | 110 | &ktm_time.tm_mon, |
110 | return(tm_time); | 111 | &ktm_time.tm_mday, |
111 | 112 | &ktm_time.tm_hour, | |
112 | } else if (sscanf(t_string, "%d.%d-%d:%d:%d", | 113 | &ktm_time.tm_min, &ktm_time.tm_sec) == 5) { |
113 | &ktm_time.tm_mon, | 114 | |
114 | &ktm_time.tm_mday, | 115 | ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
115 | &ktm_time.tm_hour, | 116 | *tm_time = ktm_time; |
116 | &ktm_time.tm_min, | 117 | return (tm_time); |
117 | &ktm_time.tm_sec) == 5) { | 118 | |
118 | 119 | } else if (sscanf(t_string, "%d.%d-%d:%d", | |
119 | ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | 120 | <m_time.tm_mon, |
120 | *tm_time = ktm_time; | 121 | <m_time.tm_mday, |
121 | return(tm_time); | 122 | <m_time.tm_hour, <m_time.tm_min) == 4) { |
122 | 123 | ||
123 | } else if (sscanf(t_string, "%d.%d-%d:%d", | 124 | ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
124 | <m_time.tm_mon, | 125 | *tm_time = ltm_time; |
125 | <m_time.tm_mday, | 126 | return (tm_time); |
126 | <m_time.tm_hour, | 127 | |
127 | <m_time.tm_min) == 4) { | 128 | } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d", |
128 | 129 | &mtm_time.tm_year, | |
129 | ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | 130 | &mtm_time.tm_mon, |
130 | *tm_time = ltm_time; | 131 | &mtm_time.tm_mday, |
131 | return(tm_time); | 132 | &mtm_time.tm_hour, |
132 | 133 | &mtm_time.tm_min, &mtm_time.tm_sec) == 6) { | |
133 | } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d", | 134 | |
134 | &mtm_time.tm_year, | 135 | mtm_time.tm_year -= 1900; /* Adjust years */ |
135 | &mtm_time.tm_mon, | 136 | mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
136 | &mtm_time.tm_mday, | 137 | *tm_time = mtm_time; |
137 | &mtm_time.tm_hour, | 138 | return (tm_time); |
138 | &mtm_time.tm_min, | 139 | |
139 | &mtm_time.tm_sec) == 6) { | 140 | } else if (sscanf(t_string, "%d.%d.%d-%d:%d", |
140 | 141 | &ntm_time.tm_year, | |
141 | mtm_time.tm_year -= 1900; /* Adjust years */ | 142 | &ntm_time.tm_mon, |
142 | mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | 143 | &ntm_time.tm_mday, |
143 | *tm_time = mtm_time; | 144 | &ntm_time.tm_hour, &ntm_time.tm_min) == 5) { |
144 | return(tm_time); | 145 | ntm_time.tm_year -= 1900; /* Adjust years */ |
145 | 146 | ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | |
146 | } else if (sscanf(t_string, "%d.%d.%d-%d:%d", | 147 | *tm_time = ntm_time; |
147 | &ntm_time.tm_year, | 148 | return (tm_time); |
148 | &ntm_time.tm_mon, | 149 | |
149 | &ntm_time.tm_mday, | 150 | } |
150 | &ntm_time.tm_hour, | 151 | |
151 | &ntm_time.tm_min) == 5) { | 152 | fprintf(stderr, invalid_date, "date", t_string); |
152 | ntm_time.tm_year -= 1900; /* Adjust years */ | 153 | |
153 | ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | 154 | exit(FALSE); |
154 | *tm_time = ntm_time; | ||
155 | return(tm_time); | ||
156 | |||
157 | } | ||
158 | |||
159 | fprintf(stderr, invalid_date, "date", t_string); | ||
160 | |||
161 | exit( FALSE); | ||
162 | 155 | ||
163 | } | 156 | } |
164 | 157 | ||
165 | 158 | ||
166 | int | 159 | int date_main(int argc, char **argv) |
167 | date_main(int argc, char * * argv) | ||
168 | { | 160 | { |
169 | char *date_str = NULL; | 161 | char *date_str = NULL; |
170 | char *date_fmt = NULL; | 162 | char *date_fmt = NULL; |
171 | char *t_buff; | 163 | char *t_buff; |
172 | int i; | 164 | int i; |
173 | int set_time = 0; | 165 | int set_time = 0; |
174 | int rfc822 = 0; | 166 | int rfc822 = 0; |
175 | int utc = 0; | 167 | int utc = 0; |
176 | int use_arg = 0; | 168 | int use_arg = 0; |
177 | time_t tm; | 169 | time_t tm; |
178 | struct tm tm_time; | 170 | struct tm tm_time; |
179 | 171 | ||
180 | /* Interpret command line args */ | 172 | /* Interpret command line args */ |
181 | i = --argc; | 173 | i = --argc; |
182 | argv++; | 174 | argv++; |
183 | while (i > 0 && **argv) { | 175 | while (i > 0 && **argv) { |
184 | if (**argv == '-') { | 176 | if (**argv == '-') { |
185 | while (i>0 && *++(*argv)) switch (**argv) { | 177 | while (i > 0 && *++(*argv)) |
186 | case 'R': | 178 | switch (**argv) { |
187 | rfc822 = 1; | 179 | case 'R': |
188 | break; | 180 | rfc822 = 1; |
189 | case 's': | 181 | break; |
190 | set_time = 1; | 182 | case 's': |
191 | if(date_str != NULL) usage ( date_usage); | 183 | set_time = 1; |
192 | date_str = optarg; | 184 | if (date_str != NULL) |
193 | break; | 185 | usage(date_usage); |
194 | case 'u': | 186 | date_str = optarg; |
195 | utc = 1; | 187 | break; |
196 | if (putenv ("TZ=UTC0") != 0) { | 188 | case 'u': |
197 | fprintf(stderr, memory_exhausted, "date"); | 189 | utc = 1; |
198 | exit( FALSE); | 190 | if (putenv("TZ=UTC0") != 0) { |
191 | fprintf(stderr, memory_exhausted, "date"); | ||
192 | exit(FALSE); | ||
193 | } | ||
194 | /* Look ma, no break. Don't fix it either. */ | ||
195 | case 'd': | ||
196 | use_arg = 1; | ||
197 | if (date_str != NULL) | ||
198 | usage(date_usage); | ||
199 | date_str = optarg; | ||
200 | break; | ||
201 | case '-': | ||
202 | usage(date_usage); | ||
203 | } | ||
204 | } else { | ||
205 | if ((date_fmt == NULL) && (strcmp(*argv, "+") == 0)) | ||
206 | date_fmt = *argv; | ||
207 | else if (date_str == NULL) { | ||
208 | set_time = 1; | ||
209 | date_str = *argv; | ||
210 | } else { | ||
211 | usage(date_usage); | ||
212 | } | ||
199 | } | 213 | } |
200 | /* Look ma, no break. Don't fix it either. */ | 214 | i--; |
201 | case 'd': | 215 | argv++; |
202 | use_arg = 1; | ||
203 | if(date_str != NULL) usage ( date_usage); | ||
204 | date_str = optarg; | ||
205 | break; | ||
206 | case '-': | ||
207 | usage ( date_usage); | ||
208 | } | ||
209 | } else { | ||
210 | if ( (date_fmt == NULL) && (strcmp(*argv, "+")==0) ) | ||
211 | date_fmt=*argv; | ||
212 | else if (date_str == NULL) { | ||
213 | set_time = 1; | ||
214 | date_str=*argv; | ||
215 | } else { | ||
216 | usage ( date_usage); | ||
217 | } | ||
218 | } | 216 | } |
219 | i--; | 217 | |
220 | argv++; | 218 | |
221 | } | 219 | /* Now we have parsed all the information except the date format |
222 | 220 | which depends on whether the clock is being set or read */ | |
223 | 221 | ||
224 | /* Now we have parsed all the information except the date format | 222 | time(&tm); |
225 | which depends on whether the clock is being set or read */ | 223 | memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); |
226 | 224 | /* Zero out fields - take her back to midnight! */ | |
227 | time(&tm); | 225 | if (date_str != NULL) { |
228 | memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); | 226 | tm_time.tm_sec = 0; |
229 | /* Zero out fields - take her back to midnight!*/ | 227 | tm_time.tm_min = 0; |
230 | if(date_str != NULL) { | 228 | tm_time.tm_hour = 0; |
231 | tm_time.tm_sec = 0; | 229 | } |
232 | tm_time.tm_min = 0; | 230 | |
233 | tm_time.tm_hour = 0; | 231 | /* Process any date input to UNIX time since 1 Jan 1970 */ |
234 | } | 232 | if (date_str != NULL) { |
235 | 233 | ||
236 | /* Process any date input to UNIX time since 1 Jan 1970 */ | 234 | if (strchr(date_str, ':') != NULL) { |
237 | if(date_str != NULL) { | 235 | date_conv_ftime(&tm_time, date_str); |
238 | 236 | } else { | |
239 | if(strchr(date_str, ':') != NULL) { | 237 | date_conv_time(&tm_time, date_str); |
240 | date_conv_ftime(&tm_time, date_str); | 238 | } |
241 | } else { | 239 | |
242 | date_conv_time(&tm_time, date_str); | 240 | /* Correct any day of week and day of year etc fields */ |
243 | } | 241 | tm = mktime(&tm_time); |
244 | 242 | if (tm < 0) { | |
245 | /* Correct any day of week and day of year etc fields */ | 243 | fprintf(stderr, invalid_date, "date", date_str); |
246 | tm = mktime(&tm_time); | 244 | exit(FALSE); |
247 | if (tm < 0 ) { | 245 | } |
248 | fprintf(stderr, invalid_date, "date", date_str); | 246 | |
249 | exit( FALSE); | 247 | /* if setting time, set it */ |
250 | } | 248 | if (set_time) { |
251 | 249 | if (stime(&tm) < 0) { | |
252 | /* if setting time, set it */ | 250 | fprintf(stderr, "date: can't set date.\n"); |
253 | if(set_time) { | 251 | exit(FALSE); |
254 | if( stime(&tm) < 0) { | 252 | } |
255 | fprintf(stderr, "date: can't set date.\n"); | 253 | } |
256 | exit( FALSE); | 254 | } |
257 | } | 255 | |
258 | } | 256 | /* Display output */ |
259 | } | 257 | |
260 | 258 | /* Deal with format string */ | |
261 | /* Display output */ | 259 | if (date_fmt == NULL) { |
262 | 260 | date_fmt = (rfc822 | |
263 | /* Deal with format string */ | 261 | ? (utc |
264 | if(date_fmt == NULL) { | 262 | ? "%a, %_d %b %Y %H:%M:%S GMT" |
265 | date_fmt = (rfc822 | 263 | : "%a, %_d %b %Y %H:%M:%S %z") |
266 | ? (utc | 264 | : "%a %b %e %H:%M:%S %Z %Y"); |
267 | ? "%a, %_d %b %Y %H:%M:%S GMT" | 265 | |
268 | : "%a, %_d %b %Y %H:%M:%S %z") | 266 | } else if (*date_fmt == '\0') { |
269 | : "%a %b %e %H:%M:%S %Z %Y"); | 267 | /* Imitate what GNU 'date' does with NO format string! */ |
270 | 268 | printf("\n"); | |
271 | } else if ( *date_fmt == '\0' ) { | 269 | exit(TRUE); |
272 | /* Imitate what GNU 'date' does with NO format string! */ | 270 | } |
273 | printf ("\n"); | 271 | |
274 | exit( TRUE); | 272 | /* Handle special conversions */ |
275 | } | 273 | |
276 | 274 | if (strncmp(date_fmt, "%f", 2) == 0) { | |
277 | /* Handle special conversions */ | 275 | date_fmt = "%Y.%m.%d-%H:%M:%S"; |
278 | 276 | } | |
279 | if( strncmp( date_fmt, "%f", 2) == 0 ) { | 277 | |
280 | date_fmt = "%Y.%m.%d-%H:%M:%S"; | 278 | /* Print OUTPUT (after ALL that!) */ |
281 | } | 279 | t_buff = malloc(201); |
282 | 280 | strftime(t_buff, 200, date_fmt, &tm_time); | |
283 | /* Print OUTPUT (after ALL that!) */ | 281 | printf("%s\n", t_buff); |
284 | t_buff = malloc(201); | 282 | |
285 | strftime(t_buff, 200, date_fmt, &tm_time); | 283 | exit(TRUE); |
286 | printf("%s\n", t_buff); | ||
287 | |||
288 | exit( TRUE); | ||
289 | 284 | ||
290 | } | 285 | } |