getdate.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Convert a string representation of time to a time value.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <limits.h>
  17. #include <stdio.h>
  18. #include <stdio_ext.h>
  19. #include <stdlib.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #include <ctype.h>
  26. #include <alloca.h>
  27. #define TM_YEAR_BASE 1900
  28. /* Prototypes for local functions. */
  29. static int first_wday (int year, int mon, int wday);
  30. static int check_mday (int year, int mon, int mday);
  31. /* Set to one of the following values to indicate an error.
  32. 1 the DATEMSK environment variable is null or undefined,
  33. 2 the template file cannot be opened for reading,
  34. 3 failed to get file status information,
  35. 4 the template file is not a regular file,
  36. 5 an error is encountered while reading the template file,
  37. 6 memory allication failed (not enough memory available),
  38. 7 there is no line in the template that matches the input,
  39. 8 invalid input specification Example: February 31 or a time is
  40. specified that can not be represented in a time_t (representing
  41. the time in seconds since 00:00:00 UTC, January 1, 1970) */
  42. int getdate_err;
  43. /* Returns the first weekday WDAY of month MON in the year YEAR. */
  44. static int
  45. first_wday (int year, int mon, int wday)
  46. {
  47. struct tm tm;
  48. if (wday == INT_MIN)
  49. return 1;
  50. memset (&tm, 0, sizeof (struct tm));
  51. tm.tm_year = year;
  52. tm.tm_mon = mon;
  53. tm.tm_mday = 1;
  54. mktime (&tm);
  55. return (1 + (wday - tm.tm_wday + 7) % 7);
  56. }
  57. /* Returns 1 if MDAY is a valid day of the month in month MON of year
  58. YEAR, and 0 if it is not. */
  59. static int
  60. check_mday (int year, int mon, int mday)
  61. {
  62. switch (mon)
  63. {
  64. case 0:
  65. case 2:
  66. case 4:
  67. case 6:
  68. case 7:
  69. case 9:
  70. case 11:
  71. if (mday >= 1 && mday <= 31)
  72. return 1;
  73. break;
  74. case 3:
  75. case 5:
  76. case 8:
  77. case 10:
  78. if (mday >= 1 && mday <= 30)
  79. return 1;
  80. break;
  81. case 1:
  82. if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28))
  83. return 1;
  84. break;
  85. }
  86. return 0;
  87. }
  88. int
  89. __getdate_r (const char *string, struct tm *tp)
  90. {
  91. FILE *fp;
  92. char *line;
  93. size_t len;
  94. char *datemsk;
  95. char *result = NULL;
  96. time_t timer;
  97. struct tm tm;
  98. struct stat64 st;
  99. int mday_ok = 0;
  100. datemsk = getenv ("DATEMSK");
  101. if (datemsk == NULL || *datemsk == '\0')
  102. return 1;
  103. if (stat64 (datemsk, &st) < 0)
  104. return 3;
  105. if (!S_ISREG (st.st_mode))
  106. return 4;
  107. if (__access (datemsk, R_OK) < 0)
  108. return 2;
  109. /* Open the template file. */
  110. fp = fopen (datemsk, "rce");
  111. if (fp == NULL)
  112. return 2;
  113. /* No threads reading this stream. */
  114. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  115. /* Skip leading whitespace. */
  116. while (isspace (*string))
  117. string++;
  118. size_t inlen, oldlen;
  119. oldlen = inlen = strlen (string);
  120. /* Skip trailing whitespace. */
  121. while (inlen > 0 && isspace (string[inlen - 1]))
  122. inlen--;
  123. char *instr = NULL;
  124. if (inlen < oldlen)
  125. {
  126. bool using_malloc = false;
  127. if (__libc_use_alloca (inlen + 1))
  128. instr = alloca (inlen + 1);
  129. else
  130. {
  131. instr = malloc (inlen + 1);
  132. if (instr == NULL)
  133. {
  134. fclose (fp);
  135. return 6;
  136. }
  137. using_malloc = true;
  138. }
  139. memcpy (instr, string, inlen);
  140. instr[inlen] = '\0';
  141. string = instr;
  142. if (!using_malloc)
  143. instr = NULL;
  144. }
  145. line = NULL;
  146. len = 0;
  147. do
  148. {
  149. ssize_t n;
  150. n = __getline (&line, &len, fp);
  151. if (n < 0)
  152. break;
  153. if (line[n - 1] == '\n')
  154. line[n - 1] = '\0';
  155. /* Do the conversion. */
  156. tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
  157. tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
  158. tp->tm_isdst = -1;
  159. tp->tm_gmtoff = 0;
  160. tp->tm_zone = NULL;
  161. result = strptime (string, line, tp);
  162. if (result && *result == '\0')
  163. break;
  164. }
  165. while (!__feof_unlocked (fp));
  166. free (instr);
  167. /* Free the buffer. */
  168. free (line);
  169. /* Check for errors. */
  170. if (__ferror_unlocked (fp))
  171. {
  172. fclose (fp);
  173. return 5;
  174. }
  175. /* Close template file. */
  176. fclose (fp);
  177. if (result == NULL || *result != '\0')
  178. return 7;
  179. /* Get current time. */
  180. time (&timer);
  181. __localtime_r (&timer, &tm);
  182. /* If only the weekday is given, today is assumed if the given day
  183. is equal to the current day and next week if it is less. */
  184. if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
  185. && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
  186. {
  187. tp->tm_year = tm.tm_year;
  188. tp->tm_mon = tm.tm_mon;
  189. tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
  190. mday_ok = 1;
  191. }
  192. /* If only the month is given, the current month is assumed if the
  193. given month is equal to the current month and next year if it is
  194. less and no year is given (the first day of month is assumed if
  195. no day is given. */
  196. if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
  197. {
  198. if (tp->tm_year == INT_MIN)
  199. tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
  200. tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
  201. mday_ok = 1;
  202. }
  203. /* If no hour, minute and second are given the current hour, minute
  204. and second are assumed. */
  205. if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
  206. {
  207. tp->tm_hour = tm.tm_hour;
  208. tp->tm_min = tm.tm_min;
  209. tp->tm_sec = tm.tm_sec;
  210. }
  211. /* Fill in the gaps. */
  212. if (tp->tm_hour == INT_MIN)
  213. tp->tm_hour = 0;
  214. if (tp->tm_min == INT_MIN)
  215. tp->tm_min = 0;
  216. if (tp->tm_sec == INT_MIN)
  217. tp->tm_sec = 0;
  218. /* If no date is given, today is assumed if the given hour is
  219. greater than the current hour and tomorrow is assumed if
  220. it is less. */
  221. if (tp->tm_hour >= 0 && tp->tm_hour <= 23
  222. && tp->tm_mon == INT_MIN
  223. && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
  224. {
  225. tp->tm_mon = tm.tm_mon;
  226. tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
  227. mday_ok = 1;
  228. }
  229. /* More fillers. */
  230. if (tp->tm_year == INT_MIN)
  231. tp->tm_year = tm.tm_year;
  232. if (tp->tm_mon == INT_MIN)
  233. tp->tm_mon = tm.tm_mon;
  234. /* Check if the day of month is within range, and if the time can be
  235. represented in a time_t. We make use of the fact that the mktime
  236. call normalizes the struct tm. */
  237. if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
  238. tp->tm_mday))
  239. || mktime (tp) == (time_t) -1)
  240. return 8;
  241. return 0;
  242. }
  243. #ifdef weak_alias
  244. weak_alias (__getdate_r, getdate_r)
  245. #endif
  246. struct tm *
  247. getdate (const char *string)
  248. {
  249. /* Buffer returned by getdate. */
  250. static struct tm tmbuf;
  251. int errval = __getdate_r (string, &tmbuf);
  252. if (errval != 0)
  253. {
  254. getdate_err = errval;
  255. return NULL;
  256. }
  257. return &tmbuf;
  258. }