time.c 63 KB

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