time.c 64 KB

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