3
0

date.c 7.5 KB

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