time.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. /* June 15, 2002 Initial Notes:
  28. *
  29. * Note: It is assumed throught that time_t is either long or unsigned long.
  30. * Similarly, clock_t is assumed to be long int.
  31. *
  32. * Warning: Assumptions are made about the layout of struct tm! It is
  33. * assumed that the initial fields of struct tm are (in order):
  34. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday
  35. *
  36. * Reached the inital goal of supporting the ANSI/ISO C99 time functions
  37. * as well as SUSv3's strptime. All timezone info is obtained from the
  38. * TZ env variable.
  39. *
  40. * Differences from glibc worth noting:
  41. *
  42. * Leap seconds are not considered here.
  43. *
  44. * glibc stores additional timezone info the struct tm, whereas we don't.
  45. *
  46. * Alternate digits and era handling are not currently implemented.
  47. * The modifiers are accepted, and tested for validity with the following
  48. * specifier, but are ignored otherwise.
  49. *
  50. * strftime does not implement glibc extension modifiers or widths for
  51. * conversion specifiers. However it does implement the glibc
  52. * extension specifiers %l, %k, and %s. It also recognizes %P, but
  53. * treats it as a synonym for %p; i.e. doesn't convert to lower case.
  54. *
  55. * strptime implements the glibc extension specifiers. However, it follows
  56. * SUSv3 in requiring at least one non-alphanumeric char between
  57. * conversion specifiers. Also, strptime only sets struct tm fields
  58. * for which format specifiers appear and does not try to infer other
  59. * fields (such as wday) as glibc's version does.
  60. *
  61. * TODO - Since glibc's %l and %k can space-pad their output in strftime,
  62. * it might be reasonable to eat whitespace first for those specifiers.
  63. * This could be done by pushing " %I" and " %H" respectively so that
  64. * leading whitespace is consumed. This is really only an issue if %l
  65. * or %k occurs at the start of the format string.
  66. *
  67. * TODO - Implement getdate? tzfile? struct tm extensions?
  68. *
  69. * TODO - Rework _time_mktime to remove the dependency on long long.
  70. */
  71. /* Oct 28, 2002
  72. *
  73. * Fixed allowed char check for std and dst TZ fields.
  74. *
  75. * Added several options concerned with timezone support. The names will
  76. * probably change once Erik gets the new config system in place.
  77. *
  78. * Defining __TIME_TZ_FILE causes tzset() to attempt to read the TZ value
  79. * from the file /etc/TZ if the TZ env variable isn't set. The file contents
  80. * must be the intended value of TZ, followed by a newline. No other chars,
  81. * spacing, etc is allowed. As an example, an easy way for me to init
  82. * /etc/TZ appropriately would be: echo CST6CDT > /etc/TZ
  83. *
  84. * Defining __TIME_TZ_FILE_ONCE will cause all further accesses of /etc/TZ
  85. * to be skipped once a legal value has been read.
  86. *
  87. * Defining __TIME_TZ_OPT_SPEED will cause a tzset() to keep a copy of the
  88. * last TZ setting string and do a "fast out" if the current string is the
  89. * same.
  90. *
  91. * Nov 21, 2002 Fix an error return case in _time_mktime.
  92. *
  93. */
  94. #define _GNU_SOURCE
  95. #define _STDIO_UTILITY
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. #include <stddef.h>
  99. #include <string.h>
  100. #include <time.h>
  101. #include <limits.h>
  102. #include <assert.h>
  103. #include <errno.h>
  104. #include <ctype.h>
  105. #include <langinfo.h>
  106. #include <locale.h>
  107. #ifndef __isleap
  108. #define __isleap(y) ( !((y) % 4) && ( ((y) % 100) || !((y) % 400) ) )
  109. #endif
  110. #ifndef TZNAME_MAX
  111. #define TZNAME_MAX _POSIX_TZNAME_MAX
  112. #endif
  113. /**********************************************************************/
  114. /* The era code is currently unfinished. */
  115. /* #define ENABLE_ERA_CODE */
  116. #define __TIME_TZ_FILE
  117. /* #define __TIME_TZ_FILE_ONCE */
  118. #define __TIME_TZ_OPT_SPEED
  119. #define TZ_BUFLEN (2*TZNAME_MAX + 56)
  120. #ifdef __TIME_TZ_FILE
  121. #include <sys/stat.h>
  122. #include <fcntl.h>
  123. #include <unistd.h>
  124. /* ":<tzname>+hh:mm:ss<tzname>+hh:mm:ss,Mmm.w.d/hh:mm:ss,Mmm.w.d/hh:mm:ss" + nul */
  125. /* 1 + 2*(1+TZNAME_MAX+1 + 9 + 7 + 9) + 1 = 2*TZNAME_MAX + 56 */
  126. #else /* __TIME_TZ_FILE */
  127. #undef __TIME_TZ_FILE_ONCE
  128. #endif /* __TIME_TZ_FILE */
  129. /**********************************************************************/
  130. extern struct tm __time_tm;
  131. typedef struct {
  132. long gmt_offset;
  133. long dst_offset;
  134. short day; /* for J or normal */
  135. short week;
  136. short month;
  137. short rule_type; /* J, M, \0 */
  138. char tzname[TZNAME_MAX+1];
  139. } rule_struct;
  140. #ifdef __UCLIBC_HAS_THREADS__
  141. #include <pthread.h>
  142. extern pthread_mutex_t _time_tzlock;
  143. #define TZLOCK pthread_mutex_lock(&_time_tzlock)
  144. #define TZUNLOCK pthread_mutex_unlock(&_time_tzlock)
  145. #else
  146. #define TZLOCK ((void) 0)
  147. #define TZUNLOCK ((void) 0)
  148. #endif
  149. extern rule_struct _time_tzinfo[2];
  150. extern struct tm *_time_t2tm(const time_t *__restrict timer,
  151. int offset, struct tm *__restrict result);
  152. extern time_t _time_mktime(struct tm *timeptr, int store_on_success);
  153. /**********************************************************************/
  154. #ifdef L_asctime
  155. static char __time_str[26];
  156. char *asctime(const struct tm *__restrict ptm)
  157. {
  158. return asctime_r(ptm, __time_str);
  159. }
  160. #endif
  161. /**********************************************************************/
  162. #ifdef L_asctime_r
  163. /* Strictly speaking, this implementation isn't correct. ANSI/ISO specifies
  164. * that the implementation of asctime() be equivalent to
  165. *
  166. * char *asctime(const struct tm *timeptr)
  167. * {
  168. * static char wday_name[7][3] = {
  169. * "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  170. * };
  171. * static char mon_name[12][3] = {
  172. * "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  173. * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  174. * };
  175. * static char result[26];
  176. *
  177. * sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
  178. * wday_name[timeptr->tm_wday],
  179. * mon_name[timeptr->tm_mon],
  180. * timeptr->tm_mday, timeptr->tm_hour,
  181. * timeptr->tm_min, timeptr->tm_sec,
  182. * 1900 + timeptr->tm_year);
  183. * return result;
  184. * }
  185. *
  186. * but the above is either inherently unsafe, or carries with it the implicit
  187. * assumption that all fields of timeptr fall within their usual ranges, and
  188. * that the tm_year value falls in the range [-2899,8099] to avoid overflowing
  189. * the static buffer.
  190. *
  191. * If we take the implicit assumption as given, then the implementation below
  192. * is still incorrect for tm_year values < -900, as there will be either
  193. * 0-padding and/or a missing negative sign for the year conversion . But given
  194. * the ususal use of asctime(), I think it isn't unreasonable to restrict correct
  195. * operation to the domain of years between 1000 and 9999.
  196. */
  197. /* This is generally a good thing, but if you're _sure_ any data passed will be
  198. * in range, you can #undef this. */
  199. #define SAFE_ASCTIME_R 1
  200. static const unsigned char at_data[] = {
  201. 'S', 'u', 'n', 'M', 'o', 'n', 'T', 'u', 'e', 'W', 'e', 'd',
  202. 'T', 'h', 'u', 'F', 'r', 'i', 'S', 'a', 't',
  203. 'J', 'a', 'n', 'F', 'e', 'b', 'M', 'a', 'r', 'A', 'p', 'r',
  204. 'M', 'a', 'y', 'J', 'u', 'n', 'J', 'u', 'l', 'A', 'u', 'g',
  205. 'S', 'e', 'p', 'O', 'c', 't', 'N', 'o', 'v', 'D', 'e', 'c',
  206. #ifdef SAFE_ASCTIME_R
  207. '?', '?', '?',
  208. #endif
  209. ' ', '?', '?', '?',
  210. ' ', '0',
  211. offsetof(struct tm, tm_mday),
  212. ' ', '0',
  213. offsetof(struct tm, tm_hour),
  214. ':', '0',
  215. offsetof(struct tm, tm_min),
  216. ':', '0',
  217. offsetof(struct tm, tm_sec),
  218. ' ', '?', '?', '?', '?', '\n', 0
  219. };
  220. char *asctime_r(register const struct tm *__restrict ptm,
  221. register char *__restrict buffer)
  222. {
  223. int tmp;
  224. assert(ptm);
  225. assert(buffer);
  226. #ifdef SAFE_ASCTIME_R
  227. memcpy(buffer, at_data + 3*(7 + 12), sizeof(at_data) - 3*(7 + 12));
  228. if (((unsigned int)(ptm->tm_wday)) <= 6) {
  229. memcpy(buffer, at_data + 3 * ptm->tm_wday, 3);
  230. }
  231. if (((unsigned int)(ptm->tm_mon)) <= 11) {
  232. memcpy(buffer + 4, at_data + 3*7 + 3 * ptm->tm_mon, 3);
  233. }
  234. #else
  235. assert(((unsigned int)(ptm->tm_wday)) <= 6);
  236. assert(((unsigned int)(ptm->tm_mon)) <= 11);
  237. memcpy(buffer, at_data + 3*(7 + 12) - 3, sizeof(at_data) + 3 - 3*(7 + 12));
  238. memcpy(buffer, at_data + 3 * ptm->tm_wday, 3);
  239. memcpy(buffer + 4, at_data + 3*7 + 3 * ptm->tm_mon, 3);
  240. #endif
  241. #ifdef SAFE_ASCTIME_R
  242. buffer += 19;
  243. tmp = ptm->tm_year + 1900;
  244. if (((unsigned int) tmp) < 10000) {
  245. buffer += 4;
  246. do {
  247. *buffer = '0' + (tmp % 10);
  248. tmp /= 10;
  249. } while (*--buffer == '?');
  250. }
  251. #else /* SAFE_ASCTIME_R */
  252. buffer += 23;
  253. tmp = ptm->tm_year + 1900;
  254. assert( ((unsigned int) tmp) < 10000 );
  255. do {
  256. *buffer = '0' + (tmp % 10);
  257. tmp /= 10;
  258. } while (*--buffer == '?');
  259. #endif /* SAFE_ASCTIME_R */
  260. do {
  261. --buffer;
  262. tmp = *((int *)(((const char *) ptm) + (int) *buffer));
  263. #ifdef SAFE_ASCTIME_R
  264. if (((unsigned int) tmp) >= 100) { /* Just check 2 digit non-neg. */
  265. buffer[-1] = *buffer = '?';
  266. } else
  267. #else /* SAFE_ASCTIME_R */
  268. assert(((unsigned int) tmp) < 100); /* Just check 2 digit non-neg. */
  269. #endif /* SAFE_ASCTIME_R */
  270. {
  271. *buffer = '0' + (tmp % 10);
  272. #ifdef __BCC__
  273. buffer[-1] = '0' + (tmp/10);
  274. #else /* __BCC__ */
  275. buffer[-1] += (tmp/10);
  276. #endif /* __BCC__ */
  277. }
  278. } while ((buffer -= 2)[-2] == '0');
  279. if (*++buffer == '0') { /* Space-pad day of month. */
  280. *buffer = ' ';
  281. }
  282. return buffer - 8;
  283. }
  284. #endif
  285. /**********************************************************************/
  286. #ifdef L_clock
  287. #include <sys/times.h>
  288. /* Note: According to glibc...
  289. * CAE XSH, Issue 4, Version 2: <time.h>
  290. * The value of CLOCKS_PER_SEC is required to be 1 million on all
  291. * XSI-conformant systems.
  292. */
  293. #ifndef __BCC__
  294. #if CLOCKS_PER_SEC != 1000000L
  295. #error unexpected value for CLOCKS_PER_SEC!
  296. #endif
  297. #endif
  298. clock_t clock(void)
  299. {
  300. struct tms xtms;
  301. unsigned long t;
  302. times(&xtms);
  303. t = ((unsigned long) xtms.tms_utime) + xtms.tms_stime;
  304. #ifndef __UCLIBC_CLK_TCK_CONST
  305. #error __UCLIBC_CLK_TCK_CONST not defined!
  306. #endif
  307. #undef CLK_TCK
  308. #define CLK_TCK __UCLIBC_CLK_TCK_CONST
  309. #if CLK_TCK > CLOCKS_PER_SEC
  310. #error __UCLIBC_CLK_TCK_CONST > CLOCKS_PER_SEC!
  311. #elif CLK_TCK < 1
  312. #error __UCLIBC_CLK_TCK_CONST < 1!
  313. #endif
  314. #if (CLK_TCK == CLOCKS_PER_SEC)
  315. return (t <= LONG_MAX) ? t : -1;
  316. #elif (CLOCKS_PER_SEC % CLK_TCK) == 0
  317. return (t <= (LONG_MAX / (CLOCKS_PER_SEC/CLK_TCK)))
  318. ? t * (CLOCKS_PER_SEC/CLK_TCK)
  319. : -1;
  320. #else
  321. return (t <= ((LONG_MAX / CLOCKS_PER_SEC) * CLK_TCK
  322. + ((LONG_MAX % CLOCKS_PER_SEC) * CLK_TCK) / CLOCKS_PER_SEC))
  323. ? (((t / CLK_TCK) * CLOCKS_PER_SEC)
  324. + (((t % CLK_TCK) * CLOCKS_PER_SEC) / CLK_TCK))
  325. : -1;
  326. #endif
  327. }
  328. #endif
  329. /**********************************************************************/
  330. #ifdef L_ctime
  331. char *ctime(const time_t *clock)
  332. {
  333. /* ANSI/ISO/SUSv3 say that ctime is equivalent to the following. */
  334. return asctime(localtime(clock));
  335. }
  336. #endif
  337. /**********************************************************************/
  338. #ifdef L_ctime_r
  339. char *ctime_r(const time_t *clock, char *buf)
  340. {
  341. struct tm xtms;
  342. return asctime_r(localtime_r(clock, &xtms), buf);
  343. }
  344. #endif
  345. /**********************************************************************/
  346. #ifdef L_difftime
  347. #include <float.h>
  348. #if FLT_RADIX != 2
  349. #error difftime implementation assumptions violated for you arch!
  350. #endif
  351. double difftime(time_t time1, time_t time0)
  352. {
  353. #if (LONG_MAX >> DBL_MANT_DIG) == 0
  354. /* time_t fits in the mantissa of a double. */
  355. return ((double) time1) - time0;
  356. #elif ((LONG_MAX >> DBL_MANT_DIG) >> DBL_MANT_DIG) == 0
  357. /* time_t can overflow the mantissa of a double. */
  358. time_t t1, t0, d;
  359. d = ((time_t) 1) << DBL_MANT_DIG;
  360. t1 = time1 / d;
  361. time1 -= (t1 * d);
  362. t0 = time0 / d;
  363. time0 -= (t0*d);
  364. /* Since FLT_RADIX==2 and d is a power of 2, the only possible
  365. * rounding error in the expression below would occur from the
  366. * addition. */
  367. return (((double) t1) - t0) * d + (((double) time1) - time0);
  368. #else
  369. #error difftime needs special implementation on your arch.
  370. #endif
  371. }
  372. #endif
  373. /**********************************************************************/
  374. #ifdef L_gmtime
  375. struct tm *gmtime(const time_t *timer)
  376. {
  377. register struct tm *ptm = &__time_tm;
  378. _time_t2tm(timer, 0, ptm); /* Can return NULL... */
  379. return ptm;
  380. }
  381. #endif
  382. /**********************************************************************/
  383. #ifdef L_gmtime_r
  384. struct tm *gmtime_r(const time_t *__restrict timer,
  385. struct tm *__restrict result)
  386. {
  387. return _time_t2tm(timer, 0, result);
  388. }
  389. #endif
  390. /**********************************************************************/
  391. #ifdef L_localtime
  392. struct tm *localtime(const time_t *timer)
  393. {
  394. register struct tm *ptm = &__time_tm;
  395. /* In this implementation, tzset() is called by localtime_r(). */
  396. localtime_r(timer, ptm); /* Can return NULL... */
  397. return ptm;
  398. }
  399. #endif
  400. /**********************************************************************/
  401. #ifdef L_localtime_r
  402. static const unsigned char day_cor[] = { /* non-leap */
  403. 31, 31, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 38
  404. /* 0, 0, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7 */
  405. /* 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 */
  406. };
  407. /* Note: timezone locking is done by localtime_r. */
  408. static int tm_isdst(register const struct tm *__restrict ptm)
  409. {
  410. register rule_struct *r = _time_tzinfo;
  411. long sec;
  412. int i, isdst, isleap, day, day0, monlen, mday;
  413. int oday; /* Note: oday can be uninitialized. */
  414. isdst = 0;
  415. if (r[1].tzname[0] != 0) {
  416. /* First, get the current seconds offset from the start of the year.
  417. * Fields of ptm are assumed to be in their normal ranges. */
  418. sec = ptm->tm_sec
  419. + 60 * (ptm->tm_min
  420. + 60 * (long)(ptm->tm_hour
  421. + 24 * ptm->tm_yday));
  422. /* Do some prep work. */
  423. i = (ptm->tm_year % 400) + 1900; /* Make sure we don't overflow. */
  424. isleap = __isleap(i);
  425. --i;
  426. day0 = (1
  427. + i /* Normal years increment 1 wday. */
  428. + (i/4)
  429. - (i/100)
  430. + (i/400) ) % 7;
  431. i = 0;
  432. do {
  433. day = r->day; /* Common for 'J' and # case. */
  434. if (r->rule_type == 'J') {
  435. if (!isleap || (day < (31+29))) {
  436. --day;
  437. }
  438. } else if (r->rule_type == 'M') {
  439. /* Find 0-based day number for 1st of the month. */
  440. day = 31*r->month - day_cor[r->month -1];
  441. if (isleap && (day >= 59)) {
  442. ++day;
  443. }
  444. monlen = 31 + day_cor[r->month -1] - day_cor[r->month];
  445. if (isleap && (r->month > 1)) {
  446. ++monlen;
  447. }
  448. /* Wweekday (0 is Sunday) of 1st of the month
  449. * is (day0 + day) % 7. */
  450. if ((mday = r->day - ((day0 + day) % 7)) >= 0) {
  451. mday -= 7; /* Back up into prev month since r->week>0. */
  452. }
  453. if ((mday += 7 * r->week) >= monlen) {
  454. mday -= 7;
  455. }
  456. /* So, 0-based day number is... */
  457. day += mday;
  458. }
  459. if (i != 0) {
  460. /* Adjust sec since dst->std change time is in dst. */
  461. sec += (r[-1].gmt_offset - r->gmt_offset);
  462. if (oday > day) {
  463. ++isdst; /* Year starts in dst. */
  464. }
  465. }
  466. oday = day;
  467. /* Now convert day to seconds and add offset and compare. */
  468. if (sec >= (day * 86400L) + r->dst_offset) {
  469. ++isdst;
  470. }
  471. ++r;
  472. } while (++i < 2);
  473. }
  474. return (isdst & 1);
  475. }
  476. struct tm *localtime_r(register const time_t *__restrict timer,
  477. register struct tm *__restrict result)
  478. {
  479. time_t x[1];
  480. long offset;
  481. int days, dst;
  482. TZLOCK;
  483. tzset();
  484. dst = 0;
  485. do {
  486. days = -7;
  487. offset = 604800L - _time_tzinfo[dst].gmt_offset;
  488. if (*timer > (LONG_MAX - 604800L)) {
  489. days = -days;
  490. offset = -offset;
  491. }
  492. *x = *timer + offset;
  493. _time_t2tm(x, days, result);
  494. if (dst) {
  495. result->tm_isdst = dst;
  496. break;
  497. }
  498. ++dst;
  499. } while ((result->tm_isdst = tm_isdst(result)) != 0);
  500. TZUNLOCK;
  501. return result;
  502. }
  503. #endif
  504. /**********************************************************************/
  505. #ifdef L_mktime
  506. time_t mktime(struct tm *timeptr)
  507. {
  508. return _time_mktime(timeptr, 1);
  509. }
  510. #endif
  511. /**********************************************************************/
  512. #ifdef L_strftime
  513. #define NO_E_MOD 0x80
  514. #define NO_O_MOD 0x40
  515. #define ILLEGAL_SPEC 0x3f
  516. #define INT_SPEC 0x00 /* must be 0x00!! */
  517. #define STRING_SPEC 0x10 /* must be 0x10!! */
  518. #define CALC_SPEC 0x20
  519. #define STACKED_SPEC 0x30
  520. #define MASK_SPEC 0x30
  521. /* Compatibility:
  522. *
  523. * No alternate digit (%O?) handling. Always uses 0-9.
  524. * Alternate locale format (%E?) handling is broken for nontrivial ERAs.
  525. * glibc's %P is currently faked by %p. This means it doesn't do lower case.
  526. * glibc's %k, %l, and %s are handled.
  527. * glibc apparently allows (and ignores) extraneous 'E' and 'O' modifiers,
  528. * while they are flagged as illegal conversions here.
  529. */
  530. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  531. static const unsigned char spec[] = {
  532. /* A */ 0x03 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  533. /* B */ 0x04 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  534. /* C */ 0x0a | INT_SPEC | NO_O_MOD,
  535. /* D */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  536. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  537. /* F */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  538. /* G */ 0x03 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  539. /* H */ 0x0b | INT_SPEC | NO_E_MOD,
  540. /* I */ 0x0c | INT_SPEC | NO_E_MOD,
  541. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  542. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  543. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  544. /* M */ 0x0d | INT_SPEC | NO_E_MOD,
  545. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  546. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  547. /* P */ 0x05 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc ; use %p */
  548. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  549. /* R */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  550. /* S */ 0x0e | INT_SPEC | NO_E_MOD,
  551. /* T */ 0x05 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  552. /* U */ 0x04 | CALC_SPEC | NO_E_MOD,
  553. /* V */ 0x05 | CALC_SPEC | NO_E_MOD,
  554. /* W */ 0x06 | CALC_SPEC | NO_E_MOD,
  555. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  556. /* Y */ 0x0f | INT_SPEC | NO_O_MOD,
  557. /* Z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  558. '?', /* 26 */
  559. '?', /* 27 */
  560. '?', /* 28 */
  561. '?', /* 29 */
  562. 0, /* 30 */
  563. 0, /* 31 */
  564. /* a */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  565. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  566. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  567. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  568. /* e */ 0x01 | INT_SPEC | NO_E_MOD,
  569. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  570. /* g */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  571. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* same as b */
  572. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  573. /* j */ 0x08 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  574. /* k */ 0x03 | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  575. /* l */ 0x04 | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  576. /* m */ 0x05 | INT_SPEC | NO_E_MOD,
  577. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  578. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  579. /* p */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  580. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  581. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  582. /* s */ 0x07 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  583. /* t */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  584. /* u */ 0x07 | INT_SPEC | NO_E_MOD,
  585. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  586. /* w */ 0x02 | INT_SPEC | NO_E_MOD,
  587. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  588. /* y */ 0x09 | INT_SPEC,
  589. /* z */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  590. /* WARNING!!! These are dependent on the layout of struct tm!!! */
  591. #define FIELD_MAX (26+6+26)
  592. 60 /* 61? */, 59, 23, 31, 11, 0 /* 9999 */, 6, 0 /* 365 */,
  593. #define TP_OFFSETS (FIELD_MAX+8)
  594. 3, /* d */
  595. 3, /* e */
  596. 6, /* w */
  597. 2, /* k */
  598. 2, /* l */
  599. 4, /* m */
  600. 0, /* CURRENTLY UNUSED */
  601. /* NOTE: u,j,y order must be preserved as 6,7,5 seq is used in the code! */
  602. #define CALC_OFFSETS (TP_OFFSETS + 7)
  603. 6, /* u */
  604. 7, /* j */
  605. 5, /* y */
  606. 5, /* C */
  607. 2, /* H */
  608. 2, /* I */
  609. 1, /* M */
  610. 0, /* S */
  611. 5, /* Y */
  612. 6, /* a */
  613. 4, /* b, h */
  614. 2, /* p */
  615. 6, /* A */
  616. 4, /* B */
  617. 2, /* P */
  618. #define TP_CODES (TP_OFFSETS + 16 + 6)
  619. 2 | 16, /* d */
  620. 2, /* e */
  621. 0 | 16, /* w */
  622. 2, /* k */
  623. 2 | 32 | 0, /* l */
  624. 2 | 16 | 1, /* m */
  625. 0, /* CURRENTLY UNUSED */
  626. 0 | 16 | 8 , /* u */
  627. 4 | 16 | 1, /* j */
  628. 2 | 128 | 32 | 16 , /* y */
  629. 2 | 128 | 64 | 32 | 16 , /* C */
  630. 2 | 16, /* H */
  631. 2 | 32 | 16 | 0, /* I */
  632. 2 | 16, /* M */
  633. 2 | 16, /* S */
  634. 6 | 16, /* Y */
  635. 2, /* a */
  636. 2, /* b, h */
  637. 2 | 64, /* p */
  638. 2, /* A */
  639. 2, /* B */
  640. 2 | 64, /* P */
  641. #define STRINGS_NL_ITEM_START (TP_CODES + 16 + 6)
  642. _NL_ITEM_INDEX(ABDAY_1), /* a */
  643. _NL_ITEM_INDEX(ABMON_1), /* b, h */
  644. _NL_ITEM_INDEX(AM_STR), /* p */
  645. _NL_ITEM_INDEX(DAY_1), /* A */
  646. _NL_ITEM_INDEX(MON_1), /* B */
  647. _NL_ITEM_INDEX(AM_STR), /* P -- wrong! need lower case */
  648. #define STACKED_STRINGS_START (STRINGS_NL_ITEM_START+6)
  649. 6, 7, 8, 16, 24, 29, /* 6 - offsets from offset-count to strings */
  650. '\n', 0, /* 2 */
  651. '\t', 0, /* 2 */
  652. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  653. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  654. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  655. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  656. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 43)
  657. _NL_ITEM_INDEX(D_T_FMT), /* c */
  658. _NL_ITEM_INDEX(D_FMT), /* x */
  659. _NL_ITEM_INDEX(T_FMT), /* X */
  660. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  661. #ifdef ENABLE_ERA_CODE
  662. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  663. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  664. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  665. #endif
  666. };
  667. static int load_field(int k, const struct tm *__restrict timeptr)
  668. {
  669. int r;
  670. int r_max;
  671. r = ((int *) timeptr)[k];
  672. r_max = spec[FIELD_MAX + k];
  673. if (k == 7) {
  674. r_max = 365;
  675. } else if (k == 5) {
  676. r += 1900;
  677. r_max = 9999;
  678. }
  679. if ((((unsigned int) r) > r_max) || ((k == 3) && !r)) {
  680. r = -1;
  681. }
  682. return r;
  683. }
  684. #define MAX_PUSH 4
  685. size_t strftime(char *__restrict s, size_t maxsize,
  686. const char *__restrict format,
  687. const struct tm *__restrict timeptr)
  688. {
  689. long tzo;
  690. register const char *p;
  691. register const char *o;
  692. const rule_struct *rsp;
  693. const char *stack[MAX_PUSH];
  694. size_t count;
  695. size_t o_count;
  696. int field_val, i, j, lvl;
  697. int x[3]; /* wday, yday, year */
  698. int isofm, days;
  699. char buf[__UIM_BUFLEN_LONG];
  700. unsigned char mod;
  701. unsigned char code;
  702. tzset(); /* We'll, let's get this out of the way. */
  703. lvl = 0;
  704. p = format;
  705. count = maxsize;
  706. LOOP:
  707. if (!count) {
  708. return 0;
  709. }
  710. if (!*p) {
  711. if (lvl == 0) {
  712. *s = 0; /* nul-terminate */
  713. return maxsize - count;
  714. }
  715. p = stack[--lvl];
  716. goto LOOP;
  717. }
  718. o_count = 1;
  719. if ((*(o = p) == '%') && (*++p != '%')) {
  720. o_count = 2;
  721. mod = ILLEGAL_SPEC;
  722. if ((*p == 'O') || (*p == 'E')) { /* modifier */
  723. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  724. ++o_count;
  725. ++p;
  726. }
  727. if ((((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  728. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  729. ) {
  730. if (!*p) {
  731. --p;
  732. --o_count;
  733. }
  734. goto OUTPUT;
  735. }
  736. code &= ILLEGAL_SPEC; /* modifiers are preserved in mod var. */
  737. if ((code & MASK_SPEC) == STACKED_SPEC) {
  738. if (lvl == MAX_PUSH) {
  739. goto OUTPUT; /* Stack full so treat as illegal spec. */
  740. }
  741. stack[lvl++] = ++p;
  742. if ((code &= 0xf) < 8) {
  743. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  744. p += *((unsigned char *)p);
  745. goto LOOP;
  746. }
  747. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  748. + (code & 7);
  749. #ifdef ENABLE_ERA_CODE
  750. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  751. && (*(o = nl_langinfo(_NL_ITEM(LC_TIME,
  752. (int)(((unsigned char *)p)[4]))
  753. )))
  754. ) {
  755. p = o;
  756. goto LOOP;
  757. }
  758. #endif
  759. p = nl_langinfo(_NL_ITEM(LC_TIME,
  760. (int)(*((unsigned char *)p))));
  761. goto LOOP;
  762. }
  763. o = spec + 26; /* set to "????" */
  764. if ((code & MASK_SPEC) == CALC_SPEC) {
  765. if (*p == 's') {
  766. time_t t;
  767. /* Use a cast to silence the warning since *timeptr won't
  768. * be changed. */
  769. if ((t = _time_mktime((struct tm *) timeptr, 0))
  770. == ((time_t) -1)
  771. ) {
  772. o_count = 1;
  773. goto OUTPUT;
  774. }
  775. #ifdef TIME_T_IS_UNSIGNED
  776. o = _uintmaxtostr(buf + sizeof(buf) - 1,
  777. (uintmax_t) t,
  778. 10, __UIM_DECIMAL);
  779. #else
  780. o = _uintmaxtostr(buf + sizeof(buf) - 1,
  781. (uintmax_t) t,
  782. -10, __UIM_DECIMAL);
  783. #endif
  784. o_count = sizeof(buf);
  785. goto OUTPUT;
  786. } else if (((*p) | 0x20) == 'z') { /* 'z' or 'Z' */
  787. if (timeptr->tm_isdst < 0) {
  788. /* SUSv3 specifies this behavior for 'z', but we'll also
  789. * treat it as "no timezone info" for 'Z' too. */
  790. o_count = 0;
  791. goto OUTPUT;
  792. }
  793. TZLOCK;
  794. rsp = _time_tzinfo;
  795. if (timeptr->tm_isdst > 0) {
  796. ++rsp;
  797. }
  798. if (*p == 'Z') {
  799. o = rsp->tzname;
  800. assert(o != NULL);
  801. #if 0
  802. if (!o) { /* PARANOIA */
  803. o = spec+30; /* empty string */
  804. }
  805. #endif
  806. o_count = SIZE_MAX;
  807. TZUNLOCK;
  808. goto OUTPUT;
  809. } else { /* z */
  810. *s = '+';
  811. if ((tzo = -rsp->gmt_offset) < 0) {
  812. tzo = -tzo;
  813. *s = '-';
  814. }
  815. TZUNLOCK;
  816. ++s;
  817. --count;
  818. i = tzo / 60;
  819. field_val = ((i / 60) * 100) + (i % 60);
  820. i = 16 + 6; /* 0-fill, width = 4 */
  821. }
  822. } else {
  823. /* TODO: don't need year for U, W */
  824. for (i=0 ; i < 3 ; i++) {
  825. if ((x[i] = load_field(spec[CALC_OFFSETS+i],timeptr)) < 0) {
  826. goto OUTPUT;
  827. }
  828. }
  829. i = 16 + 2; /* 0-fill, width = 2 */
  830. if ((*p == 'U') || (*p == 'W')) {
  831. field_val = ((x[1] - x[0]) + 7);
  832. if (*p == 'W') {
  833. ++field_val;
  834. }
  835. field_val /= 7;
  836. if ((*p == 'W') && !x[0]) {
  837. --field_val;
  838. }
  839. } else { /* ((*p == 'g') || (*p == 'G') || (*p == 'V')) */
  840. ISO_LOOP:
  841. isofm = (((x[1] - x[0]) + 11) % 7) - 3; /* [-3,3] */
  842. if (x[1] < isofm) { /* belongs to previous year */
  843. --x[2];
  844. x[1] += 365 + __isleap(x[2]);
  845. goto ISO_LOOP;
  846. }
  847. field_val = ((x[1] - isofm) / 7) + 1; /* week # */
  848. days = 365 + __isleap(x[2]);
  849. isofm = ((isofm + 7*53 + 3 - days)) %7 + days - 3; /* next year */
  850. if (x[1] >= isofm) { /* next year */
  851. x[1] -= days;
  852. ++x[2];
  853. goto ISO_LOOP;
  854. }
  855. if (*p != 'V') { /* need year */
  856. field_val = x[2]; /* TODO: what if x[2] now 10000 ?? */
  857. if (*p == 'g') {
  858. field_val %= 100;
  859. } else {
  860. i = 16 + 6; /* 0-fill, width = 4 */
  861. }
  862. }
  863. }
  864. }
  865. } else {
  866. i = TP_OFFSETS + (code & 0x1f);
  867. if ((field_val = load_field(spec[i],timeptr)) < 0) {
  868. goto OUTPUT;
  869. }
  870. i = spec[i+(TP_CODES - TP_OFFSETS)];
  871. j = (i & 128) ? 100: 12;
  872. if (i & 64) {
  873. field_val /= j;;
  874. }
  875. if (i & 32) {
  876. field_val %= j;
  877. if (((i&128) + field_val) == 0) { /* mod 12? == 0 */
  878. field_val = j; /* set to 12 */
  879. }
  880. }
  881. field_val += (i & 1);
  882. if ((i & 8) && !field_val) {
  883. field_val += 7;
  884. }
  885. }
  886. if ((code & MASK_SPEC) == STRING_SPEC) {
  887. o_count = SIZE_MAX;
  888. field_val += spec[STRINGS_NL_ITEM_START + (code & 0xf)];
  889. o = nl_langinfo(_NL_ITEM(LC_TIME, field_val));
  890. } else {
  891. o_count = ((i >> 1) & 3) + 1;
  892. o = buf + o_count;
  893. do {
  894. *(char *)(--o) = '0' + (field_val % 10);
  895. field_val /= 10;
  896. } while (o > buf);
  897. if (*buf == '0') {
  898. *buf = ' ' + (i & 16);
  899. }
  900. }
  901. }
  902. OUTPUT:
  903. ++p;
  904. while (o_count && count && *o) {
  905. *s++ = *o++;
  906. --o_count;
  907. --count;
  908. }
  909. goto LOOP;
  910. }
  911. #endif
  912. /**********************************************************************/
  913. #ifdef L_strptime
  914. /* TODO:
  915. * 1) %l and %k are space-padded, so "%l" by itself fails while " %l" succeeds.
  916. * Both work for glibc. So, should we always strip spaces?
  917. * 2) %Z
  918. */
  919. /* Notes:
  920. * There are several differences between this strptime and glibc's strptime.
  921. * 1) glibc strips leading space before numeric conversions.
  922. * 2) glibc will read fields without whitespace in between. SUSv3 states
  923. * that you must have whitespace between conversion operators. Besides,
  924. * how do you know how long a number should be if there are leading 0s?
  925. * 3) glibc attempts to compute some the struct tm fields based on the
  926. * data retrieved; tm_wday in particular. I don't as I consider it
  927. * another glibc attempt at mind-reading...
  928. */
  929. #define NO_E_MOD 0x80
  930. #define NO_O_MOD 0x40
  931. #define ILLEGAL_SPEC 0x3f
  932. #define INT_SPEC 0x00 /* must be 0x00!! */
  933. #define STRING_SPEC 0x10 /* must be 0x10!! */
  934. #define CALC_SPEC 0x20
  935. #define STACKED_SPEC 0x30
  936. #define MASK_SPEC 0x30
  937. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  938. static const unsigned char spec[] = {
  939. /* A */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  940. /* B */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  941. /* C */ 0x08 | INT_SPEC | NO_O_MOD,
  942. /* D */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  943. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  944. /* F */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  945. /* G */ 0x0f | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  946. /* H */ 0x06 | INT_SPEC | NO_E_MOD,
  947. /* I */ 0x07 | INT_SPEC | NO_E_MOD,
  948. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  949. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  950. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  951. /* M */ 0x04 | INT_SPEC | NO_E_MOD,
  952. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  953. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  954. /* P */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  955. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  956. /* R */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  957. /* S */ 0x05 | INT_SPEC | NO_E_MOD,
  958. /* T */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  959. /* U */ 0x0c | INT_SPEC | NO_E_MOD,
  960. /* V */ 0x0d | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  961. /* W */ 0x0c | INT_SPEC | NO_E_MOD,
  962. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  963. /* Y */ 0x0a | INT_SPEC | NO_O_MOD,
  964. /* Z */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  965. /* WARNING! This assumes orderings:
  966. * AM,PM
  967. * ABDAY_1-ABDAY-7,DAY_1-DAY_7
  968. * ABMON_1-ABMON_12,MON_1-MON12
  969. * Also, there are exactly 6 bytes between 'Z' and 'a'.
  970. */
  971. #define STRINGS_NL_ITEM_START (26)
  972. _NL_ITEM_INDEX(AM_STR), /* p (P) */
  973. _NL_ITEM_INDEX(ABMON_1), /* B, b */
  974. _NL_ITEM_INDEX(ABDAY_1), /* A, a */
  975. 2,
  976. 24,
  977. 14,
  978. /* a */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  979. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  980. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  981. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  982. /* e */ 0x00 | INT_SPEC | NO_E_MOD,
  983. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  984. /* g */ 0x0e | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  985. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  986. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  987. /* j */ 0x01 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  988. /* k */ 0x06 | INT_SPEC | NO_E_MOD, /* glibc */
  989. /* l */ 0x07 | INT_SPEC | NO_E_MOD, /* glibc */
  990. /* m */ 0x02 | INT_SPEC | NO_E_MOD,
  991. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  992. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  993. /* p */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  994. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  995. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  996. /* s */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  997. /* t */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  998. /* u */ 0x0b | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  999. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1000. /* w */ 0x03 | INT_SPEC | NO_E_MOD,
  1001. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  1002. /* y */ 0x09 | INT_SPEC,
  1003. /* z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1004. #define INT_FIELD_START (26+6+26)
  1005. /* (field #) << 3 + lower bound (0|1) + correction 0:none, 2:-1, 4:-1900
  1006. * followed by upper bound prior to correction with 1=>366 and 2=>9999. */
  1007. /* d, e */ (3 << 3) + 1 + 0, 31,
  1008. /* j */ (7 << 3) + 1 + 2, /* 366 */ 1,
  1009. /* m */ (4 << 3) + 1 + 2, 12,
  1010. /* w */ (6 << 3) + 0 + 0, 6,
  1011. /* M */ (1 << 3) + 0 + 0, 59,
  1012. /* S */ 0 + 0 + 0, 60,
  1013. /* H (k) */ (2 << 3) + 0 + 0, 23,
  1014. /* I (l) */ (9 << 3) + 1 + 0, 12, /* goes with 8 -- am/pm */
  1015. /* C */ (10<< 3) + 0 + 0, 99,
  1016. /* y */ (11<< 3) + 0 + 0, 99,
  1017. /* Y */ (5 << 3) + 0 + 4, /* 9999 */ 2,
  1018. /* u */ (6 << 3) + 1 + 0, 7,
  1019. /* The following are processed and range-checked, but ignored otherwise. */
  1020. /* U, W */ (12<< 3) + 0 + 0, 53,
  1021. /* V */ (12<< 3) + 1 + 0, 53,
  1022. /* g */ (12<< 3) + 0 + 0, 99,
  1023. /* G */ (12<< 3) + 0 /*+ 4*/, /* 9999 */ 2, /* Note: -1 or 10000? */
  1024. #define STACKED_STRINGS_START (INT_FIELD_START+32)
  1025. 5, 6, 14, 22, 27, /* 5 - offsets from offset-count to strings */
  1026. ' ', 0, /* 2 - %n or %t */
  1027. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  1028. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  1029. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  1030. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  1031. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 40)
  1032. _NL_ITEM_INDEX(D_T_FMT), /* c */
  1033. _NL_ITEM_INDEX(D_FMT), /* x */
  1034. _NL_ITEM_INDEX(T_FMT), /* X */
  1035. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  1036. #ifdef ENABLE_ERA_CODE
  1037. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  1038. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  1039. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  1040. #endif
  1041. };
  1042. #define MAX_PUSH 4
  1043. char *strptime(const char *__restrict buf, const char *__restrict format,
  1044. struct tm *__restrict tm)
  1045. {
  1046. register const char *p;
  1047. char *o;
  1048. const char *stack[MAX_PUSH];
  1049. int i, j, lvl;
  1050. int fields[13];
  1051. unsigned char mod;
  1052. unsigned char code;
  1053. i = 0;
  1054. do {
  1055. fields[i] = INT_MIN;
  1056. } while (++i < 13);
  1057. lvl = 0;
  1058. p = format;
  1059. LOOP:
  1060. if (!*p) {
  1061. if (lvl == 0) { /* Done. */
  1062. if (fields[6] == 7) { /* Cleanup for %u here since just once. */
  1063. fields[6] = 0; /* Don't use mod in case unset. */
  1064. }
  1065. i = 0;
  1066. do { /* Store the values into tm. */
  1067. ((int *) tm)[i] = fields[i];
  1068. } while (++i < 8);
  1069. return (char *) buf; /* Success. */
  1070. }
  1071. p = stack[--lvl];
  1072. goto LOOP;
  1073. }
  1074. if ((*p == '%') && (*++p != '%')) {
  1075. mod = ILLEGAL_SPEC;
  1076. if ((*p == 'O') || (*p == 'E')) { /* Modifier? */
  1077. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  1078. ++p;
  1079. }
  1080. if (!*p
  1081. || (((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  1082. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  1083. ) {
  1084. return NULL; /* Illegal spec. */
  1085. }
  1086. if ((code & MASK_SPEC) == STACKED_SPEC) {
  1087. if (lvl == MAX_PUSH) {
  1088. return NULL; /* Stack full so treat as illegal spec. */
  1089. }
  1090. stack[lvl++] = ++p;
  1091. if ((code &= 0xf) < 8) {
  1092. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  1093. p += *((unsigned char *)p);
  1094. goto LOOP;
  1095. }
  1096. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  1097. + (code & 7);
  1098. #ifdef ENABLE_ERA_CODE
  1099. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  1100. && (*(o = nl_langinfo(_NL_ITEM(LC_TIME,
  1101. (int)(((unsigned char *)p)[4]))
  1102. )))
  1103. ) {
  1104. p = o;
  1105. goto LOOP;
  1106. }
  1107. #endif
  1108. p = nl_langinfo(_NL_ITEM(LC_TIME,
  1109. (int)(*((unsigned char *)p))));
  1110. goto LOOP;
  1111. }
  1112. ++p;
  1113. if ((code & MASK_SPEC) == STRING_SPEC) {
  1114. code &= 0xf;
  1115. j = spec[STRINGS_NL_ITEM_START + 3 + code];
  1116. i = _NL_ITEM(LC_TIME, spec[STRINGS_NL_ITEM_START + code]);
  1117. /* Go backwards to check full names before abreviations. */
  1118. do {
  1119. --j;
  1120. o = nl_langinfo(i+j);
  1121. if (!strncasecmp(buf,o,strlen(o)) && *o) { /* Found a match. */
  1122. do {
  1123. ++buf;
  1124. } while (*++o);
  1125. if (!code) { /* am/pm */
  1126. fields[8] = j * 12;
  1127. if (fields[9] >= 0) { /* We have a previous %I or %l. */
  1128. fields[2] = fields[9] + fields[8];
  1129. }
  1130. } else { /* day (4) or month (6) */
  1131. fields[2 + (code << 1)]
  1132. = j % (spec[STRINGS_NL_ITEM_START + 3 + code] >> 1);
  1133. }
  1134. goto LOOP;
  1135. }
  1136. } while (j);
  1137. return NULL; /* Failed to match. */
  1138. }
  1139. if ((code & MASK_SPEC) == CALC_SPEC) {
  1140. if ((code &= 0xf) < 1) { /* s or z*/
  1141. time_t t;
  1142. o = (char *) buf;
  1143. i = errno;
  1144. __set_errno(0);
  1145. if (!isspace(*buf)) { /* Signal an error if whitespace. */
  1146. #ifdef TIME_T_IS_UNSIGNED
  1147. t = strtoul(buf, &o, 10);
  1148. #else
  1149. t = strtol(buf, &o, 10);
  1150. #endif
  1151. }
  1152. if ((o == buf) || errno) { /* Not a number or overflow. */
  1153. return NULL;
  1154. }
  1155. __set_errno(i); /* Restore errno. */
  1156. buf = o;
  1157. if (!code) { /* s */
  1158. localtime_r(&t, tm); /* TODO: check for failure? */
  1159. i = 0;
  1160. do { /* Now copy values from tm to fields. */
  1161. fields[i] = ((int *) tm)[i];
  1162. } while (++i < 8);
  1163. }
  1164. }
  1165. /* TODO: glibc treats %Z as a nop. For now, do the same. */
  1166. goto LOOP;
  1167. }
  1168. assert((code & MASK_SPEC) == INT_SPEC);
  1169. {
  1170. register const unsigned char *x;
  1171. code &= 0xf;
  1172. x = spec + INT_FIELD_START + (code << 1);
  1173. if ((j = x[1]) < 3) { /* upper bound (inclusive) */
  1174. j = ((j==1) ? 366 : 9999);
  1175. }
  1176. i = -1;
  1177. while (isdigit(*buf)) {
  1178. if (i < 0) {
  1179. i = 0;
  1180. }
  1181. if ((i = 10*i + (*buf - '0')) > j) { /* Overflow. */
  1182. return NULL;
  1183. }
  1184. ++buf;
  1185. }
  1186. if (i < (*x & 1)) { /* This catches no-digit case too. */
  1187. return NULL;
  1188. }
  1189. if (*x & 2) {
  1190. --i;
  1191. }
  1192. if (*x & 4) {
  1193. i -= 1900;
  1194. }
  1195. if (*x == (9 << 3) + 1 + 0) { /* %I or %l */
  1196. if (i == 12) {
  1197. i = 0;
  1198. }
  1199. if (fields[8] >= 0) { /* We have a previous %p or %P. */
  1200. fields[2] = i + fields[8];
  1201. }
  1202. }
  1203. fields[(*x) >> 3] = i;
  1204. if (((unsigned char)(*x - (10<< 3) + 0 + 0)) <= 8) { /* %C or %y */
  1205. if ((j = fields[10]) < 0) { /* No %C, so i must be %y data. */
  1206. if (i <= 68) { /* Map [0-68] to 2000+i */
  1207. i += 100;
  1208. }
  1209. } else { /* Have %C data, but what about %y? */
  1210. if ((i = fields[11]) < 0) { /* No %y data. */
  1211. i = 0; /* Treat %y val as 0 following glibc's example. */
  1212. }
  1213. i += 100*(j - 19);
  1214. }
  1215. fields[5] = i;
  1216. }
  1217. }
  1218. goto LOOP;
  1219. } else if (isspace(*p)) {
  1220. ++p;
  1221. while (isspace(*buf)) {
  1222. ++buf;
  1223. }
  1224. goto LOOP;
  1225. } else if (*buf++ == *p++) {
  1226. goto LOOP;
  1227. }
  1228. return NULL;
  1229. }
  1230. #endif
  1231. /**********************************************************************/
  1232. #ifdef L_time
  1233. #ifndef __BCC__
  1234. #error The uClibc version of time is in sysdeps/linux/common.
  1235. #endif
  1236. time_t time(register time_t *tloc)
  1237. {
  1238. struct timeval tv;
  1239. register struct timeval *p = &tv;
  1240. gettimeofday(p, NULL); /* This should never fail... */
  1241. if (tloc) {
  1242. *tloc = p->tv_sec;
  1243. }
  1244. return p->tv_sec;
  1245. }
  1246. #endif
  1247. /**********************************************************************/
  1248. #ifdef L_tzset
  1249. static const char vals[] = {
  1250. 'T', 'Z', 0, /* 3 */
  1251. 'U', 'T', 'C', 0, /* 4 */
  1252. 25, 60, 60, 1, /* 4 */
  1253. '.', 1, /* M */
  1254. 5, '.', 1,
  1255. 6, 0, 0, /* Note: overloaded for non-M non-J case... */
  1256. 0, 1, 0, /* J */
  1257. ',', 'M', '4', '.', '1', '.', '0',
  1258. ',', 'M', '1', '0', '.', '5', '.', '0', 0
  1259. };
  1260. #define TZ vals
  1261. #define UTC (vals + 3)
  1262. #define RANGE (vals + 7)
  1263. #define RULE (vals + 11 - 1)
  1264. #define DEFAULT_RULES (vals + 22)
  1265. /* Initialize to UTC. */
  1266. int daylight = 0;
  1267. long timezone = 0;
  1268. char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
  1269. #ifdef __UCLIBC_HAS_THREADS__
  1270. pthread_mutex_t _time_tzlock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  1271. #endif
  1272. rule_struct _time_tzinfo[2];
  1273. static const char *getoffset(register const char *e, long *pn)
  1274. {
  1275. register const char *s = RANGE-1;
  1276. long n;
  1277. int f;
  1278. n = 0;
  1279. f = -1;
  1280. do {
  1281. ++s;
  1282. if (isdigit(*e)) {
  1283. f = *e++ - '0';
  1284. }
  1285. if (isdigit(*e)) {
  1286. f = 10 * f + (*e++ - '0');
  1287. }
  1288. if (((unsigned int)f) >= *s) {
  1289. return NULL;
  1290. }
  1291. n = (*s) * n + f;
  1292. f = 0;
  1293. if (*e == ':') {
  1294. ++e;
  1295. --f;
  1296. }
  1297. } while (*s > 1);
  1298. *pn = n;
  1299. return e;
  1300. }
  1301. static const char *getnumber(register const char *e, int *pn)
  1302. {
  1303. #ifdef __BCC__
  1304. /* bcc can optimize the counter if it thinks it is a pointer... */
  1305. register const char *n = (const char *) 3;
  1306. int f;
  1307. f = 0;
  1308. while (n && isdigit(*e)) {
  1309. f = 10 * f + (*e++ - '0');
  1310. --n;
  1311. }
  1312. *pn = f;
  1313. return (n == (const char *) 3) ? NULL : e;
  1314. #else /* __BCC__ */
  1315. int n, f;
  1316. n = 3;
  1317. f = 0;
  1318. while (n && isdigit(*e)) {
  1319. f = 10 * f + (*e++ - '0');
  1320. --n;
  1321. }
  1322. *pn = f;
  1323. return (n == 3) ? NULL : e;
  1324. #endif /* __BCC__ */
  1325. }
  1326. #ifdef __TIME_TZ_FILE
  1327. #ifdef __TIME_TZ_FILE_ONCE
  1328. static int TZ_file_read; /* Let BSS initialization set this to 0. */
  1329. #endif /* __TIME_TZ_FILE_ONCE */
  1330. static char *read_TZ_file(char *buf)
  1331. {
  1332. int fd;
  1333. ssize_t r;
  1334. size_t todo;
  1335. char *p = NULL;
  1336. if ((fd = open("/etc/TZ", O_RDONLY)) >= 0) {
  1337. todo = TZ_BUFLEN;
  1338. p = buf;
  1339. do {
  1340. if ((r = read(fd, p, todo)) < 0) {
  1341. goto ERROR;
  1342. }
  1343. if (r == 0) {
  1344. break;
  1345. }
  1346. p += r;
  1347. todo -= r;
  1348. } while (todo);
  1349. if ((p > buf) && (p[-1] == '\n')) { /* Must end with newline. */
  1350. p[-1] = 0;
  1351. p = buf;
  1352. #ifdef __TIME_TZ_FILE_ONCE
  1353. ++TZ_file_read;
  1354. #endif /* __TIME_TZ_FILE_ONCE */
  1355. } else {
  1356. ERROR:
  1357. p = NULL;
  1358. }
  1359. close(fd);
  1360. }
  1361. return p;
  1362. }
  1363. #endif /* __TIME_TZ_FILE */
  1364. void tzset(void)
  1365. {
  1366. register const char *e;
  1367. register char *s;
  1368. long off;
  1369. short *p;
  1370. rule_struct new_rules[2];
  1371. int n, count, f;
  1372. char c;
  1373. #ifdef __TIME_TZ_FILE
  1374. char buf[TZ_BUFLEN];
  1375. #endif /* __TIME_TZ_FILE */
  1376. #ifdef __TIME_TZ_OPT_SPEED
  1377. static char oldval[TZ_BUFLEN]; /* BSS-zero'd. */
  1378. #endif /* __TIME_TZ_OPT_SPEED */
  1379. TZLOCK;
  1380. e = getenv(TZ); /* TZ env var always takes precedence. */
  1381. #ifdef __TIME_TZ_FILE_ONCE
  1382. /* Put this inside the lock to prevent the possiblity of two different
  1383. * timezones being used in a threaded app. */
  1384. if (e != NULL) {
  1385. TZ_file_read = 0; /* Reset if the TZ env var is set. */
  1386. } else if (TZ_file_read > 0) {
  1387. goto FAST_DONE;
  1388. }
  1389. #endif /* __TIME_TZ_FILE_ONCE */
  1390. /* Warning!!! Since uClibc doesn't do lib locking, the following is
  1391. * potentially unsafe in a multi-threaded program since it is remotely
  1392. * possible that another thread could call setenv() for TZ and overwrite
  1393. * the string being parsed. So, don't do that... */
  1394. if ((!e /* TZ env var not set... */
  1395. #ifdef __TIME_TZ_FILE
  1396. && !(e = read_TZ_file(buf)) /* and no file or invalid file */
  1397. #endif /* __TIME_TZ_FILE */
  1398. ) || !*e) { /* or set to empty string. */
  1399. ILLEGAL: /* TODO: Clean up the following... */
  1400. #ifdef __TIME_TZ_OPT_SPEED
  1401. *oldval = 0; /* Set oldval tonnn empty string. */
  1402. #endif /* __TIME_TZ_OPT_SPEED */
  1403. s = _time_tzinfo[0].tzname;
  1404. *s = 'U';
  1405. *++s = 'T';
  1406. *++s = 'C';
  1407. *++s =
  1408. *_time_tzinfo[1].tzname = 0;
  1409. _time_tzinfo[0].gmt_offset = 0;
  1410. goto DONE;
  1411. }
  1412. if (*e == ':') { /* Ignore leading ':'. */
  1413. ++e;
  1414. }
  1415. #ifdef __TIME_TZ_OPT_SPEED
  1416. if (strcmp(e, oldval) == 0) { /* Same string as last time... */
  1417. goto FAST_DONE; /* So nothing to do. */
  1418. }
  1419. /* Make a copy of the TZ env string. It won't be nul-terminated if
  1420. * it is too long, but it that case it will be illegal and will be reset
  1421. * to the empty string anyway. */
  1422. strncpy(oldval, e, TZ_BUFLEN);
  1423. #endif /* __TIME_TZ_OPT_SPEED */
  1424. count = 0;
  1425. new_rules[1].tzname[0] = 0;
  1426. LOOP:
  1427. /* Get std or dst name. */
  1428. c = 0;
  1429. if (*e == '<') {
  1430. ++e;
  1431. c = '>';
  1432. }
  1433. s = new_rules[count].tzname;
  1434. n = 0;
  1435. while (*e
  1436. && isascii(*e) /* SUSv3 requires char in portable char set. */
  1437. && (isalpha(*e)
  1438. || (c && (isalnum(*e) || (*e == '+') || (*e == '-'))))
  1439. ) {
  1440. *s++ = *e++;
  1441. if (++n > TZNAME_MAX) {
  1442. goto ILLEGAL;
  1443. }
  1444. }
  1445. *s = 0;
  1446. if ((n < 3) /* Check for minimum length. */
  1447. || (c && (*e++ != c)) /* Match any quoting '<'. */
  1448. ) {
  1449. goto ILLEGAL;
  1450. }
  1451. /* Get offset */
  1452. s = (char *) e;
  1453. if ((*e != '-') && (*e != '+')) {
  1454. if (count && !isdigit(*e)) {
  1455. off -= 3600; /* Default to 1 hour ahead of std. */
  1456. goto SKIP_OFFSET;
  1457. }
  1458. --e;
  1459. }
  1460. ++e;
  1461. if (!(e = getoffset(e, &off))) {
  1462. goto ILLEGAL;
  1463. }
  1464. if (*s == '-') {
  1465. off = -off; /* Save off in case needed for dst default. */
  1466. }
  1467. SKIP_OFFSET:
  1468. new_rules[count].gmt_offset = off;
  1469. if (!count) {
  1470. if (*e) {
  1471. ++count;
  1472. goto LOOP;
  1473. }
  1474. } else { /* OK, we have dst, so get some rules. */
  1475. count = 0;
  1476. if (!*e) { /* No rules so default to US rules. */
  1477. e = DEFAULT_RULES;
  1478. }
  1479. do {
  1480. if (*e++ != ',') {
  1481. goto ILLEGAL;
  1482. }
  1483. n = 365;
  1484. s = (char *) RULE;
  1485. if ((c = *e++) == 'M') {
  1486. n = 12;
  1487. } else if (c == 'J') {
  1488. s += 8;
  1489. } else {
  1490. --e;
  1491. c = 0;
  1492. s += 6;
  1493. }
  1494. *(p = &new_rules[count].rule_type) = c;
  1495. if (c != 'M') {
  1496. p -= 2;
  1497. }
  1498. do {
  1499. ++s;
  1500. if (!(e = getnumber(e, &f))
  1501. || (((unsigned int)(f - s[1])) > n)
  1502. || (*s && (*e++ != *s))
  1503. ) {
  1504. goto ILLEGAL;
  1505. }
  1506. *--p = f;
  1507. } while ((n = *(s += 2)) > 0);
  1508. off = 2 * 60 * 60; /* Default to 2:00:00 */
  1509. if (*e == '/') {
  1510. ++e;
  1511. if (!(e = getoffset(e, &off))) {
  1512. goto ILLEGAL;
  1513. }
  1514. }
  1515. new_rules[count].dst_offset = off;
  1516. } while (++count < 2);
  1517. if (*e) {
  1518. goto ILLEGAL;
  1519. }
  1520. }
  1521. memcpy(_time_tzinfo, new_rules, sizeof(new_rules));
  1522. DONE:
  1523. tzname[0] = _time_tzinfo[0].tzname;
  1524. tzname[1] = _time_tzinfo[1].tzname;
  1525. daylight = !!new_rules[1].tzname[0];
  1526. timezone = new_rules[0].gmt_offset;
  1527. FAST_DONE:
  1528. TZUNLOCK;
  1529. }
  1530. #endif
  1531. /**********************************************************************/
  1532. /* #ifdef L_utime */
  1533. /* utime is a syscall in both linux and elks. */
  1534. /* int utime(const char *path, const struct utimbuf *times) */
  1535. /* #endif */
  1536. /**********************************************************************/
  1537. /* Non-SUSv3 */
  1538. /**********************************************************************/
  1539. #ifdef L_utimes
  1540. #ifndef __BCC__
  1541. #error The uClibc version of utimes is in sysdeps/linux/common.
  1542. #endif
  1543. #include <utime.h>
  1544. #include <sys/time.h>
  1545. int utimes(const char *filename, register const struct timeval *tvp)
  1546. {
  1547. register struct utimbuf *p = NULL;
  1548. struct utimbuf utb;
  1549. if (tvp) {
  1550. p = &utb;
  1551. p->actime = tvp[0].tv_sec;
  1552. p->modtime = tvp[1].tv_sec;
  1553. }
  1554. return utime(filename, p);
  1555. }
  1556. #endif
  1557. /**********************************************************************/
  1558. #ifdef L__time_t2tm
  1559. static const uint16_t vals[] = {
  1560. 60, 60, 24, 7 /* special */, 36524, 1461, 365, 0
  1561. };
  1562. static const unsigned char days[] = {
  1563. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1564. 29,
  1565. };
  1566. /* Notes:
  1567. * If time_t is 32 bits, then no overflow is possible.
  1568. * It time_t is > 32 bits, this needs to be adjusted to deal with overflow.
  1569. */
  1570. /* Note: offset is the correction in _days_ to *timer! */
  1571. struct tm *_time_t2tm(const time_t *__restrict timer,
  1572. int offset, struct tm *__restrict result)
  1573. {
  1574. register int *p;
  1575. time_t t1, t, v;
  1576. int wday; /* Note: wday can be uninitialized. */
  1577. {
  1578. register const uint16_t *vp;
  1579. t = *timer;
  1580. p = (int *) result;
  1581. p[7] = 0;
  1582. vp = vals;
  1583. do {
  1584. if ((v = *vp) == 7) {
  1585. /* Overflow checking, assuming time_t is long int... */
  1586. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1587. #if (INT_MAX == 2147483647L) && (LONG_MAX == 9223372036854775807L)
  1588. /* Valid range for t is [-784223472856L, 784223421720L].
  1589. * Outside of this range, the tm_year field will overflow. */
  1590. if (((unsigned long)(t + offset- -784223472856L))
  1591. > (784223421720L - -784223472856L)
  1592. ) {
  1593. return NULL;
  1594. }
  1595. #else
  1596. #error overflow conditions unknown
  1597. #endif
  1598. #endif
  1599. /* We have days since the epoch, so caluclate the weekday. */
  1600. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1601. wday = (t + 4) % (*vp); /* t is unsigned */
  1602. #else
  1603. wday = ((int)((t % (*vp)) + 11)) % ((int)(*vp)); /* help bcc */
  1604. #endif
  1605. /* Set divisor to days in 400 years. Be kind to bcc... */
  1606. v = ((time_t)(vp[1])) << 2;
  1607. ++v;
  1608. /* Change to days since 1/1/1601 so that for 32 bit time_t
  1609. * values, we'll have t >= 0. This should be changed for
  1610. * archs with larger time_t types.
  1611. * Also, correct for offset since a multiple of 7. */
  1612. /* TODO: Does this still work on archs with time_t > 32 bits? */
  1613. t += (135140L - 366) + offset; /* 146097 - (365*30 + 7) -366 */
  1614. }
  1615. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1616. t -= ((t1 = t / v) * v);
  1617. #else
  1618. if ((t -= ((t1 = t / v) * v)) < 0) {
  1619. t += v;
  1620. --t1;
  1621. }
  1622. #endif
  1623. if ((*vp == 7) && (t == v-1)) {
  1624. --t; /* Correct for 400th year leap case */
  1625. ++p[4]; /* Stash the extra day... */
  1626. }
  1627. #if defined(__BCC__) && 0
  1628. *p = t1;
  1629. if (v <= 60) {
  1630. *p = t;
  1631. t = t1;
  1632. }
  1633. ++p;
  1634. #else
  1635. if (v <= 60) {
  1636. *p++ = t;
  1637. t = t1;
  1638. } else {
  1639. *p++ = t1;
  1640. }
  1641. #endif
  1642. } while (*++vp);
  1643. }
  1644. if (p[-1] == 4) {
  1645. --p[-1];
  1646. t = 365;
  1647. }
  1648. *p += ((int) t); /* result[7] .. tm_yday */
  1649. p -= 2; /* at result[5] */
  1650. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1651. /* Protect against overflow. TODO: Unecessary if int arith wraps? */
  1652. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + (p[1] - 299); /* tm_year */
  1653. #else
  1654. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + p[1] - 299; /* tm_year */
  1655. #endif
  1656. p[1] = wday; /* result[6] .. tm_wday */
  1657. {
  1658. register const unsigned char *d = days;
  1659. wday = 1900 + *p;
  1660. if (__isleap(wday)) {
  1661. d += 11;
  1662. }
  1663. wday = p[2] + 1; /* result[7] .. tm_yday */
  1664. *--p = 0; /* at result[4] .. tm_mon */
  1665. while (wday > *d) {
  1666. wday -= *d;
  1667. if (*d == 29) {
  1668. d -= 11; /* Backup to non-leap Feb. */
  1669. }
  1670. ++d;
  1671. ++*p; /* Increment tm_mon. */
  1672. }
  1673. p[-1] = wday; /* result[3] .. tm_mday */
  1674. }
  1675. /* TODO -- should this be 0? */
  1676. p[4] = 0; /* result[8] .. tm_isdst */
  1677. return result;
  1678. }
  1679. #endif
  1680. /**********************************************************************/
  1681. #ifdef L___time_tm
  1682. struct tm __time_tm; /* Global shared by gmtime() and localtime(). */
  1683. #endif
  1684. /**********************************************************************/
  1685. #ifdef L__time_mktime
  1686. static const unsigned char vals[] = {
  1687. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1688. 29,
  1689. };
  1690. time_t _time_mktime(struct tm *timeptr, int store_on_success)
  1691. {
  1692. #ifdef __BCC__
  1693. long days, secs;
  1694. #else
  1695. long long secs;
  1696. #endif
  1697. time_t t;
  1698. struct tm x;
  1699. /* 0:sec 1:min 2:hour 3:mday 4:mon 5:year 6:wday 7:yday 8:isdst */
  1700. register int *p = (int *) &x;
  1701. register const unsigned char *s;
  1702. int d;
  1703. tzset();
  1704. memcpy(p, timeptr, sizeof(struct tm));
  1705. d = 400;
  1706. p[5] = (p[5] - ((p[6] = p[5]/d) * d)) + (p[7] = p[4]/12);
  1707. if ((p[4] -= 12 * p[7]) < 0) {
  1708. p[4] += 12;
  1709. --p[5];
  1710. }
  1711. s = vals;
  1712. d = (p[5] += 1900); /* Correct year. Now between 1900 and 2300. */
  1713. if (__isleap(d)) {
  1714. s += 11;
  1715. }
  1716. p[7] = 0;
  1717. d = p[4];
  1718. while (d) {
  1719. p[7] += *s;
  1720. if (*s == 29) {
  1721. s -= 11; /* Backup to non-leap Feb. */
  1722. }
  1723. ++s;
  1724. --d;
  1725. }
  1726. #ifdef __BCC__
  1727. d = p[5] - 1;
  1728. days = -719163L + ((long)d)*365 + ((d/4) - (d/100) + (d/400) + p[3] + p[7]);
  1729. secs = p[0] + 60*( p[1] + 60*((long)(p[2])) )
  1730. + _time_tzinfo[timeptr->tm_isdst > 0].gmt_offset;
  1731. if (secs < 0) {
  1732. secs += 120009600L;
  1733. days -= 1389;
  1734. }
  1735. if ( ((unsigned long)(days + secs/86400L)) > 49710L) {
  1736. return -1;
  1737. }
  1738. secs += (days * 86400L);
  1739. #else
  1740. TZLOCK;
  1741. d = p[5] - 1;
  1742. d = -719163L + d*365 + (d/4) - (d/100) + (d/400);
  1743. secs = p[0]
  1744. + _time_tzinfo[timeptr->tm_isdst > 0].gmt_offset
  1745. + 60*( p[1]
  1746. + 60*(p[2]
  1747. + 24*(((146073L * ((long long)(p[6])) + d)
  1748. + p[3]) + p[7])));
  1749. TZUNLOCK;
  1750. if (((unsigned long long)(secs - LONG_MIN))
  1751. > (((unsigned long long)LONG_MAX) - LONG_MIN)
  1752. ) {
  1753. return -1;
  1754. }
  1755. #endif
  1756. t = secs;
  1757. localtime_r(&t, (struct tm *)p);
  1758. if (t < 0) {
  1759. return -1;
  1760. }
  1761. if (store_on_success) {
  1762. memcpy(timeptr, p, sizeof(struct tm));
  1763. }
  1764. return t;
  1765. }
  1766. #endif
  1767. /**********************************************************************/