time.c 62 KB

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