time.c 64 KB

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