time.c 57 KB

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