time.c 57 KB

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