diff options
Diffstat (limited to 'date.c')
-rw-r--r-- | date.c | 172 |
1 files changed, 77 insertions, 95 deletions
@@ -1,3 +1,24 @@ | |||
1 | /* | ||
2 | * Mini date implementation for busybox | ||
3 | * | ||
4 | * Copyright (C) 1999 by Erik Andersen <andersee@debian.org> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
1 | #include "internal.h" | 22 | #include "internal.h" |
2 | #include <stdlib.h> | 23 | #include <stdlib.h> |
3 | #include <errno.h> | 24 | #include <errno.h> |
@@ -5,7 +26,6 @@ | |||
5 | #include <unistd.h> | 26 | #include <unistd.h> |
6 | #include <time.h> | 27 | #include <time.h> |
7 | #include <stdio.h> | 28 | #include <stdio.h> |
8 | #include <getopt.h> | ||
9 | 29 | ||
10 | 30 | ||
11 | /* This 'date' command supports only 2 time setting formats, | 31 | /* This 'date' command supports only 2 time setting formats, |
@@ -14,25 +34,12 @@ | |||
14 | an RFC 822 complient date output for shell scripting | 34 | an RFC 822 complient date output for shell scripting |
15 | mail commands */ | 35 | mail commands */ |
16 | 36 | ||
17 | const char date_usage[] = "date [-uR] [+FORMAT|+%f] [ [-s|-d] MMDDhhmm[[CC]YY]\n" | 37 | const char date_usage[] = "Usage: date [OPTION]... [+FORMAT]\n" |
18 | "| [[[[CCYY.]MM.DD-]hh:mm[:ss]]]] ]"; | 38 | " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n" |
19 | 39 | "Display the current time in the given FORMAT, or set the system date.\n" | |
20 | //static const char date_usage[] = "Usage: date [OPTION]... [+FORMAT]\n" | 40 | "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n" |
21 | //"or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n" | 41 | "\t-s\t\tset time described by STRING\n" |
22 | //"Display the current time in the given FORMAT, or set the system date.\n"; | 42 | "\t-u\t\tprint or set Coordinated Universal Time\n"; |
23 | |||
24 | |||
25 | static struct option const long_options[] = | ||
26 | { | ||
27 | {"date", required_argument, NULL, 'd'}, | ||
28 | /* {"rfc-822", no_argument, NULL, 'R'}, | ||
29 | {"set", required_argument, NULL, 's'}, | ||
30 | {"uct", no_argument, NULL, 'u'}, | ||
31 | {"utc", no_argument, NULL, 'u'}, | ||
32 | {"universal", no_argument, NULL, 'u'}, */ | ||
33 | {NULL, 0, NULL, 0} | ||
34 | }; | ||
35 | |||
36 | 43 | ||
37 | 44 | ||
38 | /* Input parsing code is always bulky - used heavy duty libc stuff as | 45 | /* Input parsing code is always bulky - used heavy duty libc stuff as |
@@ -53,7 +60,7 @@ date_conv_time(struct tm *tm_time, const char *t_string) { | |||
53 | 60 | ||
54 | if(nr < 4 || nr > 5) { | 61 | if(nr < 4 || nr > 5) { |
55 | fprintf(stderr, "date: invalid date `%s'\n", t_string); | 62 | fprintf(stderr, "date: invalid date `%s'\n", t_string); |
56 | exit(1); | 63 | exit( FALSE); |
57 | } | 64 | } |
58 | 65 | ||
59 | /* correct for century - minor Y2K problem here? */ | 66 | /* correct for century - minor Y2K problem here? */ |
@@ -147,15 +154,15 @@ date_conv_ftime(struct tm *tm_time, const char *t_string) { | |||
147 | 154 | ||
148 | fprintf(stderr, "date: invalid date `%s'\n", t_string); | 155 | fprintf(stderr, "date: invalid date `%s'\n", t_string); |
149 | 156 | ||
150 | exit(1); | 157 | exit( FALSE); |
151 | 158 | ||
152 | } | 159 | } |
153 | 160 | ||
154 | 161 | ||
155 | void | 162 | void |
156 | date_err(void) { | 163 | date_err(void) { |
157 | fprintf(stderr, "date: only one date argument can be given at a time.\n"); | 164 | fprintf (stderr, "%s\n", date_usage); |
158 | exit(1); | 165 | exit( FALSE); |
159 | } | 166 | } |
160 | 167 | ||
161 | int | 168 | int |
@@ -164,82 +171,56 @@ date_main(int argc, char * * argv) | |||
164 | char *date_str = NULL; | 171 | char *date_str = NULL; |
165 | char *date_fmt = NULL; | 172 | char *date_fmt = NULL; |
166 | char *t_buff; | 173 | char *t_buff; |
174 | int i; | ||
167 | int set_time = 0; | 175 | int set_time = 0; |
168 | int rfc822 = 0; | 176 | int rfc822 = 0; |
169 | int utc = 0; | 177 | int utc = 0; |
170 | int use_arg = 0; | 178 | int use_arg = 0; |
171 | int n_args; | ||
172 | time_t tm; | 179 | time_t tm; |
173 | struct tm tm_time; | 180 | struct tm tm_time; |
174 | char optc; | ||
175 | 181 | ||
176 | /* Interpret command line args */ | 182 | /* Interpret command line args */ |
177 | 183 | i = --argc; | |
178 | 184 | argv++; | |
179 | while ((optc = getopt_long (argc, argv, "d:Rs:u", long_options, NULL)) | 185 | while (i > 0 && **argv) { |
180 | != EOF) { | 186 | if (**argv == '-') { |
181 | switch (optc) { | 187 | while (i>0 && *++(*argv)) switch (**argv) { |
182 | case 0: | 188 | case 'R': |
183 | break; | 189 | rfc822 = 1; |
184 | 190 | break; | |
185 | case 'R': | 191 | case 's': |
186 | rfc822 = 1; | 192 | set_time = 1; |
187 | break; | 193 | if(date_str != NULL) date_err(); |
188 | 194 | date_str = optarg; | |
189 | case 's': | 195 | break; |
190 | set_time = 1; | 196 | case 'u': |
191 | if(date_str != NULL) date_err(); | 197 | utc = 1; |
192 | date_str = optarg; | 198 | if (putenv ("TZ=UTC0") != 0) { |
193 | break; | 199 | fprintf(stderr,"date: memory exhausted\n"); |
194 | 200 | exit( FALSE); | |
195 | case 'u': | 201 | } |
196 | utc = 1; | 202 | /* Look ma, no break. Don't fix it either. */ |
197 | if (putenv ("TZ=UTC0") != 0) { | 203 | case 'd': |
198 | fprintf(stderr,"date: memory exhausted\n"); | 204 | use_arg = 1; |
199 | return(1); | 205 | if(date_str != NULL) date_err(); |
200 | } | 206 | date_str = optarg; |
201 | #if LOCALTIME_CACHE | 207 | break; |
202 | tzset (); | 208 | case '-': |
203 | #endif break; | 209 | date_err(); |
204 | 210 | } | |
205 | case 'd': | 211 | } else { |
206 | use_arg = 1; | 212 | if ( (date_fmt == NULL) && (strcmp(*argv, "+")==0) ) |
207 | if(date_str != NULL) date_err(); | 213 | date_fmt=*argv; |
208 | date_str = optarg; | 214 | else if (date_str == NULL) { |
209 | break; | 215 | set_time = 1; |
210 | 216 | date_str=*argv; | |
211 | default: | 217 | } else { |
212 | fprintf(stderr, "Usage: %s", date_usage); | 218 | date_err(); |
213 | break; | 219 | } |
220 | } | ||
221 | i--; | ||
222 | argv++; | ||
214 | } | 223 | } |
215 | } | ||
216 | |||
217 | |||
218 | n_args = argc - optind; | ||
219 | |||
220 | while (n_args--){ | ||
221 | switch(argv[optind][0]) { | ||
222 | case '+': | ||
223 | /* Date format strings */ | ||
224 | if(date_fmt != NULL) { | ||
225 | fprintf(stderr, "date: only one date format can be given.\n"); | ||
226 | return(1); | ||
227 | } | ||
228 | date_fmt = &argv[optind][1]; | ||
229 | break; | ||
230 | |||
231 | case '\0': | ||
232 | break; | ||
233 | |||
234 | default: | ||
235 | /* Anything left over must be a date string to set the time */ | ||
236 | set_time = 1; | ||
237 | if(date_str != NULL) date_err(); | ||
238 | date_str = argv[optind]; | ||
239 | break; | ||
240 | } | ||
241 | optind++; | ||
242 | } | ||
243 | 224 | ||
244 | 225 | ||
245 | /* Now we have parsed all the information except the date format | 226 | /* Now we have parsed all the information except the date format |
@@ -267,14 +248,14 @@ date_main(int argc, char * * argv) | |||
267 | tm = mktime(&tm_time); | 248 | tm = mktime(&tm_time); |
268 | if (tm < 0 ) { | 249 | if (tm < 0 ) { |
269 | fprintf(stderr, "date: invalid date `%s'\n", date_str); | 250 | fprintf(stderr, "date: invalid date `%s'\n", date_str); |
270 | exit(1); | 251 | exit( FALSE); |
271 | } | 252 | } |
272 | 253 | ||
273 | /* if setting time, set it */ | 254 | /* if setting time, set it */ |
274 | if(set_time) { | 255 | if(set_time) { |
275 | if( stime(&tm) < 0) { | 256 | if( stime(&tm) < 0) { |
276 | fprintf(stderr, "date: can't set date.\n"); | 257 | fprintf(stderr, "date: can't set date.\n"); |
277 | exit(1); | 258 | exit( FALSE); |
278 | } | 259 | } |
279 | } | 260 | } |
280 | } | 261 | } |
@@ -292,7 +273,7 @@ date_main(int argc, char * * argv) | |||
292 | } else if ( *date_fmt == '\0' ) { | 273 | } else if ( *date_fmt == '\0' ) { |
293 | /* Imitate what GNU 'date' does with NO format string! */ | 274 | /* Imitate what GNU 'date' does with NO format string! */ |
294 | printf ("\n"); | 275 | printf ("\n"); |
295 | return(0); | 276 | exit( TRUE); |
296 | } | 277 | } |
297 | 278 | ||
298 | /* Handle special conversions */ | 279 | /* Handle special conversions */ |
@@ -306,6 +287,7 @@ date_main(int argc, char * * argv) | |||
306 | strftime(t_buff, 200, date_fmt, &tm_time); | 287 | strftime(t_buff, 200, date_fmt, &tm_time); |
307 | printf("%s\n", t_buff); | 288 | printf("%s\n", t_buff); |
308 | 289 | ||
309 | return(0); | 290 | exit( TRUE); |
310 | 291 | ||
311 | } | 292 | } |
293 | |||