time.c 51 KB

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