time.c 66 KB

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