time.c 57 KB

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