time.c 56 KB

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