date.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Display or set the current time and date. */
  2. /* Copyright 1985, 1987, 1988 The Regents of the University of California.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. Neither the name of the University nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. SUCH DAMAGE. */
  26. #include "private.h"
  27. #include <locale.h>
  28. #include <stdio.h>
  29. /*
  30. ** The two things date knows about time are. . .
  31. */
  32. #ifndef TM_YEAR_BASE
  33. #define TM_YEAR_BASE 1900
  34. #endif /* !defined TM_YEAR_BASE */
  35. #ifndef SECSPERMIN
  36. #define SECSPERMIN 60
  37. #endif /* !defined SECSPERMIN */
  38. #if !HAVE_POSIX_DECLS
  39. extern char * optarg;
  40. extern int optind;
  41. #endif
  42. static int retval = EXIT_SUCCESS;
  43. static void display(const char *, time_t);
  44. static void dogmt(void);
  45. static void errensure(void);
  46. static void timeout(FILE *, const char *, const struct tm *);
  47. static _Noreturn void usage(void);
  48. int
  49. main(const int argc, char *argv[])
  50. {
  51. register const char * format = "+%+";
  52. register int ch;
  53. register bool rflag = false;
  54. time_t t;
  55. intmax_t secs;
  56. char * endarg;
  57. #ifdef LC_ALL
  58. setlocale(LC_ALL, "");
  59. #endif /* defined(LC_ALL) */
  60. #if HAVE_GETTEXT
  61. #ifdef TZ_DOMAINDIR
  62. bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
  63. #endif /* defined(TEXTDOMAINDIR) */
  64. textdomain(TZ_DOMAIN);
  65. #endif /* HAVE_GETTEXT */
  66. t = time(NULL);
  67. while ((ch = getopt(argc, argv, "ucr:")) != EOF && ch != -1) {
  68. switch (ch) {
  69. default:
  70. usage();
  71. case 'u': /* do it in UT */
  72. case 'c':
  73. dogmt();
  74. break;
  75. case 'r': /* seconds since 1970 */
  76. if (rflag) {
  77. fprintf(stderr,
  78. _("date: error: multiple -r's used"));
  79. usage();
  80. }
  81. rflag = true;
  82. errno = 0;
  83. secs = strtoimax(optarg, &endarg, 0);
  84. if (*endarg || optarg == endarg)
  85. errno = EINVAL;
  86. else if (! (TIME_T_MIN <= secs && secs <= TIME_T_MAX))
  87. errno = ERANGE;
  88. if (errno) {
  89. perror(optarg);
  90. errensure();
  91. exit(retval);
  92. }
  93. t = secs;
  94. break;
  95. }
  96. }
  97. if (optind < argc) {
  98. if (argc - optind != 1) {
  99. fprintf(stderr,
  100. _("date: error: multiple operands in command line\n"));
  101. usage();
  102. }
  103. format = argv[optind];
  104. if (*format != '+') {
  105. fprintf(stderr, _("date: unknown operand: %s\n"), format);
  106. usage();
  107. }
  108. }
  109. display(format, t);
  110. return retval;
  111. }
  112. static void
  113. dogmt(void)
  114. {
  115. static char ** fakeenv;
  116. if (fakeenv == NULL) {
  117. register int from;
  118. register int to;
  119. register int n;
  120. static char tzegmt0[] = "TZ=GMT0";
  121. for (n = 0; environ[n] != NULL; ++n)
  122. continue;
  123. fakeenv = malloc((n + 2) * sizeof *fakeenv);
  124. if (fakeenv == NULL) {
  125. fprintf(stderr, _("date: Memory exhausted\n"));
  126. errensure();
  127. exit(retval);
  128. }
  129. to = 0;
  130. fakeenv[to++] = tzegmt0;
  131. for (from = 1; environ[from] != NULL; ++from)
  132. if (strncmp(environ[from], "TZ=", 3) != 0)
  133. fakeenv[to++] = environ[from];
  134. fakeenv[to] = NULL;
  135. environ = fakeenv;
  136. }
  137. }
  138. static void
  139. errensure(void)
  140. {
  141. if (retval == EXIT_SUCCESS)
  142. retval = EXIT_FAILURE;
  143. }
  144. static void
  145. usage(void)
  146. {
  147. fprintf(stderr,
  148. _("date: usage: date [-u] [-c] [-r seconds]"
  149. " [+format]\n"));
  150. errensure();
  151. exit(retval);
  152. }
  153. static void
  154. display(char const *format, time_t now)
  155. {
  156. struct tm *tmp;
  157. tmp = localtime(&now);
  158. if (!tmp) {
  159. fprintf(stderr,
  160. _("date: error: time out of range\n"));
  161. errensure();
  162. return;
  163. }
  164. timeout(stdout, format, tmp);
  165. putchar('\n');
  166. fflush(stdout);
  167. fflush(stderr);
  168. if (ferror(stdout) || ferror(stderr)) {
  169. fprintf(stderr,
  170. _("date: error: couldn't write results\n"));
  171. errensure();
  172. }
  173. }
  174. #define INCR 1024
  175. static void
  176. timeout(FILE *fp, char const *format, struct tm const *tmp)
  177. {
  178. char * cp;
  179. size_t result;
  180. size_t size;
  181. struct tm tm;
  182. if (!tmp) {
  183. fprintf(stderr, _("date: error: time out of range\n"));
  184. errensure();
  185. return;
  186. }
  187. tm = *tmp;
  188. tmp = &tm;
  189. size = INCR;
  190. cp = malloc(size);
  191. for ( ; ; ) {
  192. if (cp == NULL) {
  193. fprintf(stderr,
  194. _("date: error: can't get memory\n"));
  195. errensure();
  196. exit(retval);
  197. }
  198. result = strftime(cp, size, format, tmp);
  199. if (result != 0)
  200. break;
  201. size += INCR;
  202. cp = realloc(cp, size);
  203. }
  204. fwrite(cp + 1, 1, result - 1, fp);
  205. free(cp);
  206. }