Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini date implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * by Matthew Grant <grantma@anathoth.gen.nz> |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <errno.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <unistd.h> |
| 27 | #include <time.h> |
| 28 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <string.h> |
Eric Andersen | 88f50b6 | 2000-08-10 17:59:11 +0000 | [diff] [blame] | 30 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 31 | #include "busybox.h" |
| 32 | #define BB_DECLARE_EXTERN |
| 33 | #define bb_need_invalid_date |
| 34 | #define bb_need_memory_exhausted |
| 35 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /* This 'date' command supports only 2 time setting formats, |
| 39 | all the GNU strftime stuff (its in libc, lets use it), |
| 40 | setting time using UTC and displaying int, as well as |
| 41 | an RFC 822 complient date output for shell scripting |
| 42 | mail commands */ |
| 43 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | /* Input parsing code is always bulky - used heavy duty libc stuff as |
| 45 | much as possible, missed out a lot of bounds checking */ |
| 46 | |
| 47 | /* Default input handling to save suprising some people */ |
| 48 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 49 | static struct tm *date_conv_time(struct tm *tm_time, const char *t_string) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | { |
| 51 | int nr; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | nr = sscanf(t_string, "%2d%2d%2d%2d%d", |
| 54 | &(tm_time->tm_mon), |
| 55 | &(tm_time->tm_mday), |
| 56 | &(tm_time->tm_hour), |
| 57 | &(tm_time->tm_min), &(tm_time->tm_year)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | if (nr < 4 || nr > 5) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 60 | error_msg_and_die(invalid_date, t_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | /* correct for century - minor Y2K problem here? */ |
| 64 | if (tm_time->tm_year >= 1900) |
| 65 | tm_time->tm_year -= 1900; |
| 66 | /* adjust date */ |
| 67 | tm_time->tm_mon -= 1; |
| 68 | |
| 69 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* The new stuff for LRP */ |
| 75 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 76 | static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 78 | struct tm t; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | /* Parse input and assign appropriately to tm_time */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 82 | if (t=*tm_time,sscanf(t_string, "%d:%d:%d", |
| 83 | &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { |
| 84 | /* no adjustments needed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 86 | } else if (t=*tm_time,sscanf(t_string, "%d:%d", |
| 87 | &t.tm_hour, &t.tm_min) == 2) { |
| 88 | /* no adjustments needed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 91 | } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d:%d", |
| 92 | &t.tm_mon, |
| 93 | &t.tm_mday, |
| 94 | &t.tm_hour, |
| 95 | &t.tm_min, &t.tm_sec) == 5) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 97 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 99 | } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d", |
| 100 | &t.tm_mon, |
| 101 | &t.tm_mday, |
| 102 | &t.tm_hour, &t.tm_min) == 4) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 104 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 105 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 106 | } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d:%d", |
| 107 | &t.tm_year, |
| 108 | &t.tm_mon, |
| 109 | &t.tm_mday, |
| 110 | &t.tm_hour, |
| 111 | &t.tm_min, &t.tm_sec) == 6) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 113 | t.tm_year -= 1900; /* Adjust years */ |
| 114 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 116 | } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d", |
| 117 | &t.tm_year, |
| 118 | &t.tm_mon, |
| 119 | &t.tm_mday, |
| 120 | &t.tm_hour, &t.tm_min) == 5) { |
| 121 | t.tm_year -= 1900; /* Adjust years */ |
| 122 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 124 | } else { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 125 | error_msg_and_die(invalid_date, t_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | } |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 127 | *tm_time = t; |
| 128 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 132 | int date_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | char *date_str = NULL; |
| 135 | char *date_fmt = NULL; |
| 136 | char *t_buff; |
Pavel Roskin | 47d4926 | 2000-07-17 16:17:19 +0000 | [diff] [blame] | 137 | int c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | int set_time = 0; |
| 139 | int rfc822 = 0; |
| 140 | int utc = 0; |
| 141 | int use_arg = 0; |
| 142 | time_t tm; |
| 143 | struct tm tm_time; |
| 144 | |
| 145 | /* Interpret command line args */ |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 146 | while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) { |
| 147 | switch (c) { |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 148 | case 'R': |
| 149 | rfc822 = 1; |
| 150 | break; |
| 151 | case 's': |
| 152 | set_time = 1; |
| 153 | if ((date_str != NULL) || ((date_str = optarg) == NULL)) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 154 | show_usage(); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 155 | } |
| 156 | break; |
| 157 | case 'u': |
| 158 | utc = 1; |
| 159 | if (putenv("TZ=UTC0") != 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 160 | error_msg_and_die(memory_exhausted); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 161 | break; |
| 162 | case 'd': |
| 163 | use_arg = 1; |
| 164 | if ((date_str != NULL) || ((date_str = optarg) == NULL)) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 165 | show_usage(); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 166 | break; |
| 167 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 168 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 170 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 171 | |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 172 | if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) |
| 173 | date_fmt = &argv[optind][1]; /* Skip over the '+' */ |
| 174 | else if (date_str == NULL) { |
| 175 | set_time = 1; |
| 176 | date_str = argv[optind]; |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 177 | } |
| 178 | #if 0 |
| 179 | else { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 180 | error_msg("date_str='%s' date_fmt='%s'\n", date_str, date_fmt); |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 181 | show_usage(); |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 182 | } |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 183 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 185 | /* Now we have parsed all the information except the date format |
| 186 | which depends on whether the clock is being set or read */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 187 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 188 | time(&tm); |
| 189 | memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); |
| 190 | /* Zero out fields - take her back to midnight! */ |
| 191 | if (date_str != NULL) { |
| 192 | tm_time.tm_sec = 0; |
| 193 | tm_time.tm_min = 0; |
| 194 | tm_time.tm_hour = 0; |
| 195 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 196 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | /* Process any date input to UNIX time since 1 Jan 1970 */ |
| 198 | if (date_str != NULL) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 199 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | if (strchr(date_str, ':') != NULL) { |
| 201 | date_conv_ftime(&tm_time, date_str); |
| 202 | } else { |
| 203 | date_conv_time(&tm_time, date_str); |
| 204 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 205 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | /* Correct any day of week and day of year etc fields */ |
| 207 | tm = mktime(&tm_time); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 208 | if (tm < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 209 | error_msg_and_die(invalid_date, date_str); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 210 | if ( utc ) { |
| 211 | if (putenv("TZ=UTC0") != 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 212 | error_msg_and_die(memory_exhausted); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 213 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 214 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | /* if setting time, set it */ |
| 216 | if (set_time) { |
| 217 | if (stime(&tm) < 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 218 | perror_msg("cannot set date"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 222 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | /* Display output */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 224 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | /* Deal with format string */ |
| 226 | if (date_fmt == NULL) { |
| 227 | date_fmt = (rfc822 |
| 228 | ? (utc |
| 229 | ? "%a, %_d %b %Y %H:%M:%S GMT" |
| 230 | : "%a, %_d %b %Y %H:%M:%S %z") |
| 231 | : "%a %b %e %H:%M:%S %Z %Y"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 232 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | } else if (*date_fmt == '\0') { |
| 234 | /* Imitate what GNU 'date' does with NO format string! */ |
| 235 | printf("\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 236 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 238 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 239 | /* Handle special conversions */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 240 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | if (strncmp(date_fmt, "%f", 2) == 0) { |
| 242 | date_fmt = "%Y.%m.%d-%H:%M:%S"; |
| 243 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 244 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 245 | /* Print OUTPUT (after ALL that!) */ |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 246 | t_buff = xmalloc(201); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 247 | strftime(t_buff, 200, date_fmt, &tm_time); |
| 248 | printf("%s\n", t_buff); |
| 249 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 250 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 251 | } |