time.c 56 KB

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