date.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #if !HAVE_POSIX_DECLS
  30. extern char * optarg;
  31. extern int optind;
  32. #endif
  33. static int retval = EXIT_SUCCESS;
  34. static void display(const char *, time_t);
  35. static void dogmt(void);
  36. static void errensure(void);
  37. static void timeout(FILE *, const char *, const struct tm *);
  38. ATTRIBUTE_NORETURN static void usage(void);
  39. int
  40. main(const int argc, char *argv[])
  41. {
  42. register const char * format = "+%+";
  43. register int ch;
  44. register bool rflag = false;
  45. time_t t;
  46. intmax_t secs;
  47. char * endarg;
  48. #ifdef LC_ALL
  49. setlocale(LC_ALL, "");
  50. #endif /* defined(LC_ALL) */
  51. #if HAVE_GETTEXT
  52. # ifdef TZ_DOMAINDIR
  53. bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
  54. # endif /* defined(TEXTDOMAINDIR) */
  55. textdomain(TZ_DOMAIN);
  56. #endif /* HAVE_GETTEXT */
  57. t = time(NULL);
  58. while ((ch = getopt(argc, argv, "ucr:")) != EOF && ch != -1) {
  59. switch (ch) {
  60. default:
  61. usage();
  62. case 'u': /* do it in UT */
  63. case 'c':
  64. dogmt();
  65. break;
  66. case 'r': /* seconds since 1970 */
  67. if (rflag) {
  68. fprintf(stderr,
  69. _("date: error: multiple -r's used"));
  70. usage();
  71. }
  72. rflag = true;
  73. errno = 0;
  74. secs = strtoimax(optarg, &endarg, 0);
  75. if (*endarg || optarg == endarg)
  76. errno = EINVAL;
  77. else if (! (TIME_T_MIN <= secs && secs <= TIME_T_MAX))
  78. errno = ERANGE;
  79. if (errno) {
  80. char const *e = strerror(errno);
  81. fprintf(stderr, _("date: %s: %s\n"),
  82. optarg, e);
  83. errensure();
  84. exit(retval);
  85. }
  86. t = secs;
  87. break;
  88. }
  89. }
  90. if (optind < argc) {
  91. if (argc - optind != 1) {
  92. fprintf(stderr,
  93. _("date: error: multiple operands in command line\n"));
  94. usage();
  95. }
  96. format = argv[optind];
  97. if (*format != '+') {
  98. fprintf(stderr, _("date: unknown operand: %s\n"), format);
  99. usage();
  100. }
  101. }
  102. display(format, t);
  103. return retval;
  104. }
  105. static void
  106. dogmt(void)
  107. {
  108. static char ** fakeenv;
  109. if (fakeenv == NULL) {
  110. static char tzeutc0[] = "TZ=UTC0";
  111. ptrdiff_t from, to, n;
  112. for (n = 0; environ[n] != NULL; ++n)
  113. continue;
  114. #if defined ckd_add && defined ckd_mul
  115. if (!ckd_add(&n, n, 2) && !ckd_mul(&n, n, sizeof *fakeenv)
  116. && n <= INDEX_MAX)
  117. fakeenv = malloc(n);
  118. #else
  119. if (n <= INDEX_MAX / sizeof *fakeenv - 2)
  120. fakeenv = malloc((n + 2) * sizeof *fakeenv);
  121. #endif
  122. if (fakeenv == NULL) {
  123. fprintf(stderr, _("date: Memory exhausted\n"));
  124. errensure();
  125. exit(retval);
  126. }
  127. to = 0;
  128. fakeenv[to++] = tzeutc0;
  129. for (from = 1; environ[from] != NULL; ++from)
  130. if (strncmp(environ[from], "TZ=", 3) != 0)
  131. fakeenv[to++] = environ[from];
  132. fakeenv[to] = NULL;
  133. environ = fakeenv;
  134. }
  135. }
  136. static void
  137. errensure(void)
  138. {
  139. if (retval == EXIT_SUCCESS)
  140. retval = EXIT_FAILURE;
  141. }
  142. static void
  143. usage(void)
  144. {
  145. fprintf(stderr,
  146. _("date: usage: date [-u] [-c] [-r seconds]"
  147. " [+format]\n"));
  148. errensure();
  149. exit(retval);
  150. }
  151. static void
  152. display(char const *format, time_t now)
  153. {
  154. struct tm *tmp;
  155. tmp = localtime(&now);
  156. if (!tmp) {
  157. fprintf(stderr,
  158. _("date: error: time out of range\n"));
  159. errensure();
  160. return;
  161. }
  162. timeout(stdout, format, tmp);
  163. putchar('\n');
  164. fflush(stdout);
  165. fflush(stderr);
  166. if (ferror(stdout) || ferror(stderr)) {
  167. fprintf(stderr,
  168. _("date: error: couldn't write results\n"));
  169. errensure();
  170. }
  171. }
  172. static void
  173. timeout(FILE *fp, char const *format, struct tm const *tmp)
  174. {
  175. char *cp = NULL;
  176. ptrdiff_t result;
  177. ptrdiff_t size = 1024 / 2;
  178. for ( ; ; ) {
  179. #ifdef ckd_mul
  180. bool bigger = !ckd_mul(&size, size, 2) && size <= INDEX_MAX;
  181. #else
  182. bool bigger = size <= INDEX_MAX / 2 && (size *= 2, true);
  183. #endif
  184. char *newcp = bigger ? realloc(cp, size) : NULL;
  185. if (!newcp) {
  186. fprintf(stderr,
  187. _("date: error: can't get memory\n"));
  188. errensure();
  189. exit(retval);
  190. }
  191. cp = newcp;
  192. result = strftime(cp, size, format, tmp);
  193. if (result != 0)
  194. break;
  195. }
  196. fwrite(cp + 1, 1, result - 1, fp);
  197. free(cp);
  198. }