time.c 52 KB

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