time.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  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. o_count = 2;
  933. mod = ILLEGAL_SPEC;
  934. if ((*p == 'O') || (*p == 'E')) { /* modifier */
  935. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  936. ++o_count;
  937. ++p;
  938. }
  939. if ((((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  940. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  941. ) {
  942. if (!*p) {
  943. --p;
  944. --o_count;
  945. }
  946. goto OUTPUT;
  947. }
  948. code &= ILLEGAL_SPEC; /* modifiers are preserved in mod var. */
  949. if ((code & MASK_SPEC) == STACKED_SPEC) {
  950. if (lvl == MAX_PUSH) {
  951. goto OUTPUT; /* Stack full so treat as illegal spec. */
  952. }
  953. stack[lvl++] = ++p;
  954. if ((code &= 0xf) < 8) {
  955. ccp = (const char *)(spec + STACKED_STRINGS_START + code);
  956. ccp += *ccp;
  957. fmt_to_wc(p, ccp);
  958. goto LOOP;
  959. }
  960. ccp = (const char *)spec + STACKED_STRINGS_NL_ITEM_START + (code & 7);
  961. fmt_to_wc(p, ccp);
  962. #ifdef ENABLE_ERA_CODE
  963. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  964. && (*(ccp = __XL_NPP(nl_langinfo)(_NL_ITEM(LC_TIME,
  965. (int)(((unsigned char *)p)[4]))
  966. __LOCALE_ARG
  967. )))
  968. ) {
  969. fmt_to_wc(p, ccp);
  970. goto LOOP;
  971. }
  972. #endif
  973. ccp = __XL_NPP(nl_langinfo)(_NL_ITEM(LC_TIME,
  974. (int)(*((unsigned char *)p)))
  975. __LOCALE_ARG
  976. );
  977. fmt_to_wc(p, ccp);
  978. goto LOOP;
  979. }
  980. ccp = (const char *)(spec + 26); /* set to "????" */
  981. if ((code & MASK_SPEC) == CALC_SPEC) {
  982. if (*p == 's') {
  983. time_t t;
  984. /* Use a cast to silence the warning since *timeptr won't
  985. * be changed. */
  986. if ((t = _time_mktime((struct tm *) timeptr, 0))
  987. == ((time_t) -1)
  988. ) {
  989. o_count = 1;
  990. goto OUTPUT;
  991. }
  992. #ifdef TIME_T_IS_UNSIGNED
  993. ccp = _uintmaxtostr(buf + sizeof(buf) - 1,
  994. (uintmax_t) t,
  995. 10, __UIM_DECIMAL);
  996. #else
  997. ccp = _uintmaxtostr(buf + sizeof(buf) - 1,
  998. (uintmax_t) t,
  999. -10, __UIM_DECIMAL);
  1000. #endif
  1001. o_count = sizeof(buf);
  1002. fmt_to_wc(o, ccp);
  1003. goto OUTPUT;
  1004. } else if (((*p) | 0x20) == 'z') { /* 'z' or 'Z' */
  1005. if (timeptr->tm_isdst < 0) {
  1006. /* SUSv3 specifies this behavior for 'z', but we'll also
  1007. * treat it as "no timezone info" for 'Z' too. */
  1008. o_count = 0;
  1009. goto OUTPUT;
  1010. }
  1011. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  1012. # ifdef __USE_BSD
  1013. # define RSP_TZNAME timeptr->tm_zone
  1014. # define RSP_GMT_OFFSET (-timeptr->tm_gmtoff)
  1015. # else
  1016. # define RSP_TZNAME timeptr->__tm_zone
  1017. # define RSP_GMT_OFFSET (-timeptr->__tm_gmtoff)
  1018. # endif
  1019. #else
  1020. #define RSP_TZNAME rsp->tzname
  1021. #define RSP_GMT_OFFSET rsp->gmt_offset
  1022. __UCLIBC_MUTEX_LOCK(_time_tzlock);
  1023. rsp = _time_tzinfo;
  1024. if (timeptr->tm_isdst > 0) {
  1025. ++rsp;
  1026. }
  1027. #endif
  1028. if (*p == 'Z') {
  1029. ccp = RSP_TZNAME;
  1030. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  1031. /* Sigh... blasted glibc extensions. Of course we can't
  1032. * count on the pointer being valid. Best we can do is
  1033. * handle NULL, which looks to be all that glibc does.
  1034. * At least that catches the memset() with 0 case.
  1035. * NOTE: We handle this case differently than glibc!
  1036. * It uses system timezone name (based on tm_isdst) in this
  1037. * case... although it always seems to use the embedded
  1038. * tm_gmtoff value. What we'll do instead is treat the
  1039. * timezone name as unknown/invalid and return "???". */
  1040. if (!ccp) {
  1041. ccp = (const char *)(spec + 27); /* "???" */
  1042. }
  1043. #endif
  1044. assert(ccp != NULL);
  1045. #if 0
  1046. if (!ccp) { /* PARANOIA */
  1047. ccp = spec+30; /* empty string */
  1048. }
  1049. #endif
  1050. o_count = SIZE_MAX;
  1051. fmt_to_wc(o, ccp);
  1052. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  1053. goto OUTPUT;
  1054. #endif
  1055. } else { /* z */
  1056. *s = '+';
  1057. if ((tzo = -RSP_GMT_OFFSET) < 0) {
  1058. tzo = -tzo;
  1059. *s = '-';
  1060. }
  1061. ++s;
  1062. --count;
  1063. i = tzo / 60;
  1064. field_val = ((i / 60) * 100) + (i % 60);
  1065. i = 16 + 6; /* 0-fill, width = 4 */
  1066. }
  1067. #ifndef __UCLIBC_HAS_TM_EXTENSIONS__
  1068. __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  1069. if (*p == 'Z') {
  1070. goto OUTPUT;
  1071. }
  1072. #endif
  1073. } else {
  1074. /* TODO: don't need year for U, W */
  1075. for (i=0 ; i < 3 ; i++) {
  1076. if ((x[i] = load_field(spec[CALC_OFFSETS+i],timeptr)) < 0) {
  1077. goto OUTPUT;
  1078. }
  1079. }
  1080. i = 16 + 2; /* 0-fill, width = 2 */
  1081. if ((*p == 'U') || (*p == 'W')) {
  1082. field_val = ((x[1] - x[0]) + 7);
  1083. if (*p == 'W') {
  1084. ++field_val;
  1085. }
  1086. field_val /= 7;
  1087. if ((*p == 'W') && !x[0]) {
  1088. --field_val;
  1089. }
  1090. } else { /* ((*p == 'g') || (*p == 'G') || (*p == 'V')) */
  1091. ISO_LOOP:
  1092. isofm = (((x[1] - x[0]) + 11) % 7) - 3; /* [-3,3] */
  1093. if (x[1] < isofm) { /* belongs to previous year */
  1094. --x[2];
  1095. x[1] += 365 + __isleap(x[2]);
  1096. goto ISO_LOOP;
  1097. }
  1098. field_val = ((x[1] - isofm) / 7) + 1; /* week # */
  1099. days = 365 + __isleap(x[2]);
  1100. isofm = ((isofm + 7*53 + 3 - days)) % 7 + days - 3; /* next year */
  1101. if (x[1] >= isofm) { /* next year */
  1102. x[1] -= days;
  1103. ++x[2];
  1104. goto ISO_LOOP;
  1105. }
  1106. if (*p != 'V') { /* need year */
  1107. field_val = x[2]; /* TODO: what if x[2] now 10000 ?? */
  1108. if (*p == 'g') {
  1109. field_val %= 100;
  1110. } else {
  1111. i = 16 + 6; /* 0-fill, width = 4 */
  1112. }
  1113. }
  1114. }
  1115. }
  1116. } else {
  1117. i = TP_OFFSETS + (code & 0x1f);
  1118. if ((field_val = load_field(spec[i], timeptr)) < 0) {
  1119. goto OUTPUT;
  1120. }
  1121. i = spec[i+(TP_CODES - TP_OFFSETS)];
  1122. j = (i & 128) ? 100: 12;
  1123. if (i & 64) {
  1124. field_val /= j;;
  1125. }
  1126. if (i & 32) {
  1127. field_val %= j;
  1128. if (((i & 128) + field_val) == 0) { /* mod 12? == 0 */
  1129. field_val = j; /* set to 12 */
  1130. }
  1131. }
  1132. field_val += (i & 1);
  1133. if ((i & 8) && !field_val) {
  1134. field_val += 7;
  1135. }
  1136. }
  1137. if ((code & MASK_SPEC) == STRING_SPEC) {
  1138. o_count = SIZE_MAX;
  1139. field_val += spec[STRINGS_NL_ITEM_START + (code & 0xf)];
  1140. ccp = __XL_NPP(nl_langinfo)(_NL_ITEM(LC_TIME, field_val) __LOCALE_ARG);
  1141. fmt_to_wc(o, ccp);
  1142. } else {
  1143. o_count = ((i >> 1) & 3) + 1;
  1144. ccp = buf + o_count;
  1145. do {
  1146. *(char *)(--ccp) = '0' + (field_val % 10);
  1147. field_val /= 10;
  1148. } while (ccp > buf);
  1149. if (*buf == '0') {
  1150. *buf = ' ' + (i & 16);
  1151. }
  1152. fmt_to_wc(o, ccp);
  1153. }
  1154. }
  1155. OUTPUT:
  1156. ++p;
  1157. while (o_count && count && *o) {
  1158. *s++ = *o++;
  1159. --o_count;
  1160. --count;
  1161. }
  1162. #if defined __UCLIBC_HAS_WCHAR__ && (defined L_wcsftime || defined L_wcsftime_l)
  1163. if (allocno >= 0)
  1164. free((void *)alloc[allocno--]);
  1165. #endif
  1166. goto LOOP;
  1167. }
  1168. # ifdef L_strftime_l
  1169. libc_hidden_def(strftime_l)
  1170. # endif
  1171. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1172. #endif
  1173. /**********************************************************************/
  1174. #if defined(L_strptime) || defined(L_strptime_l)
  1175. #define ISDIGIT(C) __isdigit_char((C))
  1176. #ifdef __UCLIBC_DO_XLOCALE
  1177. #define ISSPACE(C) isspace_l((C), locale_arg)
  1178. #else
  1179. #define ISSPACE(C) isspace((C))
  1180. #endif
  1181. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  1182. char *strptime(const char *__restrict buf, const char *__restrict format,
  1183. struct tm *__restrict tm)
  1184. {
  1185. return strptime_l(buf, format, tm, __UCLIBC_CURLOCALE);
  1186. }
  1187. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1188. /* TODO:
  1189. * 1) %l and %k are space-padded, so "%l" by itself fails while " %l" succeeds.
  1190. * Both work for glibc. So, should we always strip spaces?
  1191. * 2) %Z
  1192. */
  1193. /* Notes:
  1194. * There are several differences between this strptime and glibc's strptime.
  1195. * 1) glibc strips leading space before numeric conversions.
  1196. * 2) glibc will read fields without whitespace in between. SUSv3 states
  1197. * that you must have whitespace between conversion operators. Besides,
  1198. * how do you know how long a number should be if there are leading 0s?
  1199. * 3) glibc attempts to compute some the struct tm fields based on the
  1200. * data retrieved; tm_wday in particular. I don't as I consider it
  1201. * another glibc attempt at mind-reading...
  1202. */
  1203. #define NO_E_MOD 0x80
  1204. #define NO_O_MOD 0x40
  1205. #define ILLEGAL_SPEC 0x3f
  1206. #define INT_SPEC 0x00 /* must be 0x00!! */
  1207. #define STRING_SPEC 0x10 /* must be 0x10!! */
  1208. #define CALC_SPEC 0x20
  1209. #define STACKED_SPEC 0x30
  1210. #define MASK_SPEC 0x30
  1211. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  1212. static const unsigned char spec[] = {
  1213. /* A */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1214. /* B */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1215. /* C */ 0x08 | INT_SPEC | NO_O_MOD,
  1216. /* D */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1217. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1218. /* F */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1219. /* G */ 0x0f | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1220. /* H */ 0x06 | INT_SPEC | NO_E_MOD,
  1221. /* I */ 0x07 | INT_SPEC | NO_E_MOD,
  1222. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1223. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1224. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1225. /* M */ 0x04 | INT_SPEC | NO_E_MOD,
  1226. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1227. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1228. /* P */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1229. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1230. /* R */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1231. /* S */ 0x05 | INT_SPEC | NO_E_MOD,
  1232. /* T */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1233. /* U */ 0x0c | INT_SPEC | NO_E_MOD,
  1234. /* V */ 0x0d | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1235. /* W */ 0x0c | INT_SPEC | NO_E_MOD,
  1236. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  1237. /* Y */ 0x0a | INT_SPEC | NO_O_MOD,
  1238. /* Z */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1239. /* WARNING! This assumes orderings:
  1240. * AM,PM
  1241. * ABDAY_1-ABDAY-7,DAY_1-DAY_7
  1242. * ABMON_1-ABMON_12,MON_1-MON12
  1243. * Also, there are exactly 6 bytes between 'Z' and 'a'.
  1244. */
  1245. #define STRINGS_NL_ITEM_START (26)
  1246. _NL_ITEM_INDEX(AM_STR), /* p (P) */
  1247. _NL_ITEM_INDEX(ABMON_1), /* B, b */
  1248. _NL_ITEM_INDEX(ABDAY_1), /* A, a */
  1249. 2,
  1250. 24,
  1251. 14,
  1252. /* a */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1253. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1254. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  1255. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  1256. /* e */ 0x00 | INT_SPEC | NO_E_MOD,
  1257. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1258. /* g */ 0x0e | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1259. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1260. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1261. /* j */ 0x01 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  1262. /* k */ 0x06 | INT_SPEC | NO_E_MOD, /* glibc */
  1263. /* l */ 0x07 | INT_SPEC | NO_E_MOD, /* glibc */
  1264. /* m */ 0x02 | INT_SPEC | NO_E_MOD,
  1265. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1266. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1267. /* p */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1268. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1269. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1270. /* s */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1271. /* t */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1272. /* u */ 0x0b | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1273. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1274. /* w */ 0x03 | INT_SPEC | NO_E_MOD,
  1275. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  1276. /* y */ 0x09 | INT_SPEC,
  1277. /* z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1278. #define INT_FIELD_START (26+6+26)
  1279. /* (field #) << 3 + lower bound (0|1) + correction 0:none, 2:-1, 4:-1900
  1280. * followed by upper bound prior to correction with 1=>366 and 2=>9999. */
  1281. /* d, e */ (3 << 3) + 1 + 0, 31,
  1282. /* j */ (7 << 3) + 1 + 2, /* 366 */ 1,
  1283. /* m */ (4 << 3) + 1 + 2, 12,
  1284. /* w */ (6 << 3) + 0 + 0, 6,
  1285. /* M */ (1 << 3) + 0 + 0, 59,
  1286. /* S */ 0 + 0 + 0, 60,
  1287. /* H (k) */ (2 << 3) + 0 + 0, 23,
  1288. /* I (l) */ (9 << 3) + 1 + 0, 12, /* goes with 8 -- am/pm */
  1289. /* C */ (10<< 3) + 0 + 0, 99,
  1290. /* y */ (11<< 3) + 0 + 0, 99,
  1291. /* Y */ (5 << 3) + 0 + 4, /* 9999 */ 2,
  1292. /* u */ (6 << 3) + 1 + 0, 7,
  1293. /* The following are processed and range-checked, but ignored otherwise. */
  1294. /* U, W */ (12<< 3) + 0 + 0, 53,
  1295. /* V */ (12<< 3) + 1 + 0, 53,
  1296. /* g */ (12<< 3) + 0 + 0, 99,
  1297. /* G */ (12<< 3) + 0 /*+ 4*/, /* 9999 */ 2, /* Note: -1 or 10000? */
  1298. #define STACKED_STRINGS_START (INT_FIELD_START+32)
  1299. 5, 6, 14, 22, 27, /* 5 - offsets from offset-count to strings */
  1300. ' ', 0, /* 2 - %n or %t */
  1301. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  1302. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  1303. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  1304. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  1305. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 40)
  1306. _NL_ITEM_INDEX(D_T_FMT), /* c */
  1307. _NL_ITEM_INDEX(D_FMT), /* x */
  1308. _NL_ITEM_INDEX(T_FMT), /* X */
  1309. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  1310. #ifdef ENABLE_ERA_CODE
  1311. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  1312. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  1313. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  1314. #endif
  1315. };
  1316. #define MAX_PUSH 4
  1317. char *__XL_NPP(strptime)(const char *__restrict buf, const char *__restrict format,
  1318. struct tm *__restrict tm __LOCALE_PARAM)
  1319. {
  1320. register const char *p;
  1321. char *o;
  1322. const char *stack[MAX_PUSH];
  1323. int i, j, lvl;
  1324. int fields[13];
  1325. unsigned char mod;
  1326. unsigned char code;
  1327. i = 0;
  1328. do {
  1329. fields[i] = INT_MIN;
  1330. } while (++i < 13);
  1331. lvl = 0;
  1332. p = format;
  1333. LOOP:
  1334. if (!*p) {
  1335. if (lvl == 0) { /* Done. */
  1336. if (fields[6] == 7) { /* Cleanup for %u here since just once. */
  1337. fields[6] = 0; /* Don't use mod in case unset. */
  1338. }
  1339. i = 0;
  1340. do { /* Store the values into tm. */
  1341. if (fields[i] != INT_MIN) {
  1342. ((int *) tm)[i] = fields[i];
  1343. }
  1344. } while (++i < 8);
  1345. return (char *) buf; /* Success. */
  1346. }
  1347. p = stack[--lvl];
  1348. goto LOOP;
  1349. }
  1350. if ((*p == '%') && (*++p != '%')) {
  1351. mod = ILLEGAL_SPEC;
  1352. if ((*p == 'O') || (*p == 'E')) { /* Modifier? */
  1353. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  1354. ++p;
  1355. }
  1356. if (!*p
  1357. || (((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  1358. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  1359. ) {
  1360. return NULL; /* Illegal spec. */
  1361. }
  1362. if ((code & MASK_SPEC) == STACKED_SPEC) {
  1363. if (lvl == MAX_PUSH) {
  1364. return NULL; /* Stack full so treat as illegal spec. */
  1365. }
  1366. stack[lvl++] = ++p;
  1367. if ((code &= 0xf) < 8) {
  1368. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  1369. p += *((unsigned char *)p);
  1370. goto LOOP;
  1371. }
  1372. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  1373. + (code & 7);
  1374. #ifdef ENABLE_ERA_CODE
  1375. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  1376. && (*(o = __XL_NPP(nl_langinfo)(_NL_ITEM(LC_TIME,
  1377. (int)(((unsigned char *)p)[4]))
  1378. __LOCALE_ARG
  1379. )))
  1380. ) {
  1381. p = o;
  1382. goto LOOP;
  1383. }
  1384. #endif
  1385. p = __XL_NPP(nl_langinfo)(_NL_ITEM(LC_TIME,
  1386. (int)(*((unsigned char *)p)))
  1387. __LOCALE_ARG
  1388. );
  1389. goto LOOP;
  1390. }
  1391. ++p;
  1392. if ((code & MASK_SPEC) == STRING_SPEC) {
  1393. code &= 0xf;
  1394. j = spec[STRINGS_NL_ITEM_START + 3 + code];
  1395. i = _NL_ITEM(LC_TIME, spec[STRINGS_NL_ITEM_START + code]);
  1396. /* Go backwards to check full names before abreviations. */
  1397. do {
  1398. --j;
  1399. o = __XL_NPP(nl_langinfo)(i+j __LOCALE_ARG);
  1400. if (!__XL_NPP(strncasecmp)(buf, o, strlen(o) __LOCALE_ARG) && *o) {
  1401. do { /* Found a match. */
  1402. ++buf;
  1403. } while (*++o);
  1404. if (!code) { /* am/pm */
  1405. fields[8] = j * 12;
  1406. if (fields[9] >= 0) { /* We have a previous %I or %l. */
  1407. fields[2] = fields[9] + fields[8];
  1408. }
  1409. } else { /* day (4) or month (6) */
  1410. fields[2 + (code << 1)]
  1411. = j % (spec[STRINGS_NL_ITEM_START + 3 + code] >> 1);
  1412. }
  1413. goto LOOP;
  1414. }
  1415. } while (j);
  1416. return NULL; /* Failed to match. */
  1417. }
  1418. if ((code & MASK_SPEC) == CALC_SPEC) {
  1419. if ((code &= 0xf) < 1) { /* s or z*/
  1420. time_t t;
  1421. o = (char *) buf;
  1422. i = errno;
  1423. __set_errno(0);
  1424. if (!ISSPACE(*buf)) { /* Signal an error if whitespace. */
  1425. #ifdef TIME_T_IS_UNSIGNED
  1426. t = __XL_NPP(strtoul)(buf, &o, 10 __LOCALE_ARG);
  1427. #else
  1428. t = __XL_NPP(strtol)(buf, &o, 10 __LOCALE_ARG);
  1429. #endif
  1430. }
  1431. if ((o == buf) || errno) { /* Not a number or overflow. */
  1432. return NULL;
  1433. }
  1434. __set_errno(i); /* Restore errno. */
  1435. buf = o;
  1436. if (!code) { /* s */
  1437. localtime_r(&t, tm); /* TODO: check for failure? */
  1438. i = 0;
  1439. do { /* Now copy values from tm to fields. */
  1440. fields[i] = ((int *) tm)[i];
  1441. } while (++i < 8);
  1442. }
  1443. }
  1444. /* TODO: glibc treats %Z as a nop. For now, do the same. */
  1445. goto LOOP;
  1446. }
  1447. assert((code & MASK_SPEC) == INT_SPEC);
  1448. {
  1449. register const unsigned char *x;
  1450. code &= 0xf;
  1451. x = spec + INT_FIELD_START + (code << 1);
  1452. if ((j = x[1]) < 3) { /* upper bound (inclusive) */
  1453. j = ((j==1) ? 366 : 9999);
  1454. }
  1455. i = -1;
  1456. while (ISDIGIT(*buf)) {
  1457. if (i < 0) {
  1458. i = 0;
  1459. }
  1460. if ((i = 10*i + (*buf - '0')) > j) { /* Overflow. */
  1461. return NULL;
  1462. }
  1463. ++buf;
  1464. }
  1465. if (i < (*x & 1)) { /* This catches no-digit case too. */
  1466. return NULL;
  1467. }
  1468. if (*x & 2) {
  1469. --i;
  1470. }
  1471. if (*x & 4) {
  1472. i -= 1900;
  1473. }
  1474. if (*x == (9 << 3) + 1 + 0) { /* %I or %l */
  1475. if (i == 12) {
  1476. i = 0;
  1477. }
  1478. if (fields[8] >= 0) { /* We have a previous %p or %P. */
  1479. fields[2] = i + fields[8];
  1480. }
  1481. }
  1482. fields[(*x) >> 3] = i;
  1483. if (((unsigned char)(*x - (10 << 3) + 0 + 0)) <= 8) { /* %C or %y */
  1484. if ((j = fields[10]) < 0) { /* No %C, so i must be %y data. */
  1485. if (i <= 68) { /* Map [0-68] to 2000+i */
  1486. i += 100;
  1487. }
  1488. } else { /* Have %C data, but what about %y? */
  1489. if ((i = fields[11]) < 0) { /* No %y data. */
  1490. i = 0; /* Treat %y val as 0 following glibc's example. */
  1491. }
  1492. i += 100*(j - 19);
  1493. }
  1494. fields[5] = i;
  1495. }
  1496. }
  1497. goto LOOP;
  1498. } else if (ISSPACE(*p)) {
  1499. ++p;
  1500. while (ISSPACE(*buf)) {
  1501. ++buf;
  1502. }
  1503. goto LOOP;
  1504. } else if (*buf++ == *p++) {
  1505. goto LOOP;
  1506. }
  1507. return NULL;
  1508. }
  1509. # ifdef L_strptime_l
  1510. libc_hidden_def(strptime_l)
  1511. # endif
  1512. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1513. #endif
  1514. /**********************************************************************/
  1515. #ifdef L_time
  1516. #ifndef __BCC__
  1517. #error The uClibc version of time is in sysdeps/linux/common.
  1518. #endif
  1519. time_t time(register time_t *tloc)
  1520. {
  1521. struct timeval tv;
  1522. register struct timeval *p = &tv;
  1523. gettimeofday(p, NULL); /* This should never fail... */
  1524. if (tloc) {
  1525. *tloc = p->tv_sec;
  1526. }
  1527. return p->tv_sec;
  1528. }
  1529. #endif
  1530. /**********************************************************************/
  1531. #ifdef L_tzset
  1532. static const char vals[] = {
  1533. 'T', 'Z', 0, /* 3 */
  1534. 'U', 'T', 'C', 0, /* 4 */
  1535. 25, 60, 60, 1, /* 4 */
  1536. '.', 1, /* M */
  1537. 5, '.', 1,
  1538. 6, 0, 0, /* Note: overloaded for non-M non-J case... */
  1539. 0, 1, 0, /* J */
  1540. ',', 'M', '4', '.', '1', '.', '0',
  1541. ',', 'M', '1', '0', '.', '5', '.', '0', 0,
  1542. ',', 'M', '3', '.', '2', '.', '0',
  1543. ',', 'M', '1', '1', '.', '1', '.', '0', 0
  1544. };
  1545. #define TZ vals
  1546. #define UTC (vals + 3)
  1547. #define RANGE (vals + 7)
  1548. #define RULE (vals + 11 - 1)
  1549. #define DEFAULT_RULES (vals + 22)
  1550. #define DEFAULT_2007_RULES (vals + 38)
  1551. /* Initialize to UTC. */
  1552. int daylight = 0;
  1553. long timezone = 0;
  1554. char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
  1555. __UCLIBC_MUTEX_INIT(_time_tzlock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  1556. rule_struct _time_tzinfo[2];
  1557. static const char *getoffset(register const char *e, long *pn)
  1558. {
  1559. register const char *s = RANGE-1;
  1560. long n;
  1561. int f;
  1562. n = 0;
  1563. f = -1;
  1564. do {
  1565. ++s;
  1566. if (__isdigit_char(*e)) {
  1567. f = *e++ - '0';
  1568. }
  1569. if (__isdigit_char(*e)) {
  1570. f = 10 * f + (*e++ - '0');
  1571. }
  1572. if (((unsigned int)f) >= *s) {
  1573. return NULL;
  1574. }
  1575. n = (*s) * n + f;
  1576. f = 0;
  1577. if (*e == ':') {
  1578. ++e;
  1579. --f;
  1580. }
  1581. } while (*s > 1);
  1582. *pn = n;
  1583. return e;
  1584. }
  1585. static const char *getnumber(register const char *e, int *pn)
  1586. {
  1587. #ifdef __BCC__
  1588. /* bcc can optimize the counter if it thinks it is a pointer... */
  1589. register const char *n = (const char *) 3;
  1590. int f;
  1591. f = 0;
  1592. while (n && __isdigit_char(*e)) {
  1593. f = 10 * f + (*e++ - '0');
  1594. --n;
  1595. }
  1596. *pn = f;
  1597. return (n == (const char *) 3) ? NULL : e;
  1598. #else /* __BCC__ */
  1599. int n, f;
  1600. n = 3;
  1601. f = 0;
  1602. while (n && __isdigit_char(*e)) {
  1603. f = 10 * f + (*e++ - '0');
  1604. --n;
  1605. }
  1606. *pn = f;
  1607. return (n == 3) ? NULL : e;
  1608. #endif /* __BCC__ */
  1609. }
  1610. #ifdef __UCLIBC_MJN3_ONLY__
  1611. #warning CONSIDER: Should we preserve errno from open/read/close errors re TZ file?
  1612. #endif
  1613. #ifdef __UCLIBC_HAS_TZ_FILE__
  1614. #ifndef __UCLIBC_HAS_TZ_FILE_READ_MANY__
  1615. static smallint TZ_file_read; /* Let BSS initialization set this to 0. */
  1616. #endif
  1617. static char *read_TZ_file(char *buf)
  1618. {
  1619. int r;
  1620. int fd;
  1621. char *p = NULL;
  1622. fd = open(__UCLIBC_TZ_FILE_PATH__, O_RDONLY);
  1623. if (fd >= 0) {
  1624. #if 0
  1625. /* TZ are small *files*. On files, short reads
  1626. * only occur on EOF (unlike, say, pipes).
  1627. * The code below is pedanticallly more correct,
  1628. * but this way we always read at least twice:
  1629. * 1st read is short, 2nd one is zero bytes.
  1630. */
  1631. size_t todo = TZ_BUFLEN;
  1632. p = buf;
  1633. do {
  1634. r = read(fd, p, todo);
  1635. if (r < 0)
  1636. goto ERROR;
  1637. if (r == 0)
  1638. break;
  1639. p += r;
  1640. todo -= r;
  1641. } while (todo);
  1642. #else
  1643. /* Shorter, and does one fewer read syscall */
  1644. r = read(fd, buf, TZ_BUFLEN);
  1645. if (r < 0)
  1646. goto ERROR;
  1647. p = buf + r;
  1648. #endif
  1649. if ((p > buf) && (p[-1] == '\n')) { /* Must end with newline */
  1650. p[-1] = 0;
  1651. p = buf;
  1652. #ifndef __UCLIBC_HAS_TZ_FILE_READ_MANY__
  1653. TZ_file_read = 1;
  1654. #endif
  1655. } else {
  1656. ERROR:
  1657. p = NULL;
  1658. }
  1659. close(fd);
  1660. }
  1661. #ifdef __UCLIBC_FALLBACK_TO_ETC_LOCALTIME__
  1662. else {
  1663. fd = open("/etc/localtime", O_RDONLY);
  1664. if (fd >= 0) {
  1665. r = read(fd, buf, TZ_BUFLEN);
  1666. if (r != TZ_BUFLEN
  1667. || strncmp(buf, "TZif", 4) != 0
  1668. || (unsigned char)buf[4] < 2
  1669. || lseek(fd, -TZ_BUFLEN, SEEK_END) < 0
  1670. ) {
  1671. goto ERROR;
  1672. }
  1673. /* tzfile.h from tzcode database says about TZif2+ files:
  1674. **
  1675. ** If tzh_version is '2' or greater, the above is followed by a second instance
  1676. ** of tzhead and a second instance of the data in which each coded transition
  1677. ** time uses 8 rather than 4 chars,
  1678. ** then a POSIX-TZ-environment-variable-style string for use in handling
  1679. ** instants after the last transition time stored in the file
  1680. ** (with nothing between the newlines if there is no POSIX representation for
  1681. ** such instants).
  1682. */
  1683. r = read(fd, buf, TZ_BUFLEN);
  1684. if (r <= 0 || buf[--r] != '\n')
  1685. goto ERROR;
  1686. buf[r] = 0;
  1687. while (r != 0) {
  1688. if (buf[--r] == '\n') {
  1689. p = buf + r + 1;
  1690. #ifndef __UCLIBC_HAS_TZ_FILE_READ_MANY__
  1691. TZ_file_read = 1;
  1692. #endif
  1693. break;
  1694. }
  1695. } /* else ('\n' not found): p remains NULL */
  1696. close(fd);
  1697. }
  1698. }
  1699. #endif /* __UCLIBC_FALLBACK_TO_ETC_LOCALTIME__ */
  1700. return p;
  1701. }
  1702. #endif /* __UCLIBC_HAS_TZ_FILE__ */
  1703. void tzset(void)
  1704. {
  1705. _time_tzset((time(NULL)) < new_rule_starts);
  1706. }
  1707. void _time_tzset(int use_old_rules)
  1708. {
  1709. register const char *e;
  1710. register char *s;
  1711. long off = 0;
  1712. short *p;
  1713. rule_struct new_rules[2];
  1714. int n, count, f;
  1715. char c;
  1716. #ifdef __UCLIBC_HAS_TZ_FILE__
  1717. char buf[TZ_BUFLEN];
  1718. #endif
  1719. #ifdef __UCLIBC_HAS_TZ_CACHING__
  1720. static char oldval[TZ_BUFLEN]; /* BSS-zero'd. */
  1721. #endif
  1722. /* Put this inside the lock to prevent the possibility of two different
  1723. * timezones being used in a threaded app. */
  1724. __UCLIBC_MUTEX_LOCK(_time_tzlock);
  1725. e = getenv(TZ); /* TZ env var always takes precedence. */
  1726. #if defined(__UCLIBC_HAS_TZ_FILE__) && !defined(__UCLIBC_HAS_TZ_FILE_READ_MANY__)
  1727. if (e) {
  1728. /* Never use TZfile if TZ env var is set. */
  1729. TZ_file_read = 0;
  1730. }
  1731. if (TZ_file_read) {
  1732. /* We already parsed TZfile before, skip everything. */
  1733. goto FAST_DONE;
  1734. }
  1735. #endif
  1736. /* Warning!!! Since uClibc doesn't do lib locking, the following is
  1737. * potentially unsafe in a multi-threaded program since it is remotely
  1738. * possible that another thread could call setenv() for TZ and overwrite
  1739. * the string being parsed. So, don't do that... */
  1740. #ifdef __UCLIBC_HAS_TZ_FILE__
  1741. if (!e)
  1742. e = read_TZ_file(buf);
  1743. #endif
  1744. if (!e /* TZ env var not set and no TZfile (or bad TZfile) */
  1745. || !*e /* or set to empty string. */
  1746. ) {
  1747. goto ILLEGAL;
  1748. }
  1749. if (*e == ':') { /* Ignore leading ':'. */
  1750. ++e;
  1751. }
  1752. #ifdef __UCLIBC_HAS_TZ_CACHING__
  1753. if (strcmp(e, oldval) == 0) {
  1754. /* Same string as last time... nothing to do. */
  1755. goto FAST_DONE;
  1756. }
  1757. /* Make a copy of the TZ env string. It won't be nul-terminated if
  1758. * it is too long, but it that case it will be illegal and will be reset
  1759. * to the empty string anyway. */
  1760. strncpy(oldval, e, TZ_BUFLEN);
  1761. #endif
  1762. count = 0;
  1763. new_rules[1].tzname[0] = 0;
  1764. LOOP:
  1765. /* Get std or dst name. */
  1766. c = 0;
  1767. if (*e == '<') {
  1768. ++e;
  1769. c = '>';
  1770. }
  1771. s = new_rules[count].tzname;
  1772. n = 0;
  1773. while (*e
  1774. && isascii(*e) /* SUSv3 requires char in portable char set. */
  1775. && (isalpha(*e)
  1776. || (c && (isalnum(*e) || (*e == '+') || (*e == '-')))
  1777. )
  1778. ) {
  1779. *s++ = *e++;
  1780. if (++n > TZNAME_MAX) {
  1781. goto ILLEGAL;
  1782. }
  1783. }
  1784. *s = 0;
  1785. if ((n < 3) /* Check for minimum length. */
  1786. || (c && (*e++ != c)) /* Match any quoting '<'. */
  1787. ) {
  1788. goto ILLEGAL;
  1789. }
  1790. /* Get offset */
  1791. s = (char *) e;
  1792. if ((*e != '-') && (*e != '+')) {
  1793. if (count && !__isdigit_char(*e)) {
  1794. off -= 3600; /* Default to 1 hour ahead of std. */
  1795. goto SKIP_OFFSET;
  1796. }
  1797. --e;
  1798. }
  1799. ++e;
  1800. e = getoffset(e, &off);
  1801. if (!e) {
  1802. goto ILLEGAL;
  1803. }
  1804. if (*s == '-') {
  1805. off = -off; /* Save off in case needed for dst default. */
  1806. }
  1807. SKIP_OFFSET:
  1808. new_rules[count].gmt_offset = off;
  1809. if (!count) {
  1810. new_rules[1].gmt_offset = off; /* Shouldn't be needed... */
  1811. if (*e) {
  1812. ++count;
  1813. goto LOOP;
  1814. }
  1815. } else { /* OK, we have dst, so get some rules. */
  1816. count = 0;
  1817. if (!*e) { /* No rules so default to US rules. */
  1818. e = use_old_rules ? DEFAULT_RULES : DEFAULT_2007_RULES;
  1819. #ifdef DEBUG_TZSET
  1820. if (e == DEFAULT_RULES)
  1821. printf("tzset: Using old rules.\n");
  1822. else if (e == DEFAULT_2007_RULES)
  1823. printf("tzset: Using new rules\n");
  1824. else
  1825. printf("tzset: Using undefined rules\n");
  1826. #endif
  1827. }
  1828. do {
  1829. if (*e++ != ',') {
  1830. goto ILLEGAL;
  1831. }
  1832. n = 365;
  1833. s = (char *) RULE;
  1834. c = *e++;
  1835. if (c == 'M') {
  1836. n = 12;
  1837. } else if (c == 'J') {
  1838. s += 8;
  1839. } else {
  1840. --e;
  1841. c = 0;
  1842. s += 6;
  1843. }
  1844. p = &new_rules[count].rule_type;
  1845. *p = c;
  1846. if (c != 'M') {
  1847. p -= 2;
  1848. }
  1849. do {
  1850. ++s;
  1851. e = getnumber(e, &f);
  1852. if (!e
  1853. || ((unsigned int)(f - s[1]) > n)
  1854. || (*s && (*e++ != *s))
  1855. ) {
  1856. goto ILLEGAL;
  1857. }
  1858. *--p = f;
  1859. s += 2;
  1860. n = *s;
  1861. } while (n > 0);
  1862. off = 2 * 60 * 60; /* Default to 2:00:00 */
  1863. if (*e == '/') {
  1864. ++e;
  1865. e = getoffset(e, &off);
  1866. if (!e) {
  1867. goto ILLEGAL;
  1868. }
  1869. }
  1870. new_rules[count].dst_offset = off;
  1871. } while (++count < 2);
  1872. if (*e) {
  1873. ILLEGAL:
  1874. #ifdef __UCLIBC_HAS_TZ_CACHING__
  1875. oldval[0] = 0; /* oldval = "" */
  1876. #endif
  1877. memset(_time_tzinfo, 0, sizeof(_time_tzinfo));
  1878. strcpy(_time_tzinfo[0].tzname, UTC);
  1879. goto DONE;
  1880. }
  1881. }
  1882. memcpy(_time_tzinfo, new_rules, sizeof(new_rules));
  1883. DONE:
  1884. tzname[0] = _time_tzinfo[0].tzname;
  1885. tzname[1] = _time_tzinfo[1].tzname;
  1886. daylight = !!_time_tzinfo[1].tzname[0];
  1887. timezone = _time_tzinfo[0].gmt_offset;
  1888. #if (defined(__UCLIBC_HAS_TZ_FILE__) && !defined(__UCLIBC_HAS_TZ_FILE_READ_MANY__)) || \
  1889. defined(__UCLIBC_HAS_TZ_CACHING__)
  1890. FAST_DONE:
  1891. #endif
  1892. __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  1893. }
  1894. libc_hidden_def(tzset)
  1895. #endif
  1896. /**********************************************************************/
  1897. /* #ifdef L_utime */
  1898. /* utime is a syscall in both linux and elks. */
  1899. /* int utime(const char *path, const struct utimbuf *times) */
  1900. /* #endif */
  1901. /**********************************************************************/
  1902. /* Non-SUSv3 */
  1903. /**********************************************************************/
  1904. #ifdef L_utimes
  1905. #ifndef __BCC__
  1906. #error The uClibc version of utimes is in sysdeps/linux/common.
  1907. #endif
  1908. #include <utime.h>
  1909. int utimes(const char *filename, register const struct timeval *tvp)
  1910. {
  1911. register struct utimbuf *p = NULL;
  1912. struct utimbuf utb;
  1913. if (tvp) {
  1914. p = &utb;
  1915. p->actime = tvp[0].tv_sec;
  1916. p->modtime = tvp[1].tv_sec;
  1917. }
  1918. return utime(filename, p);
  1919. }
  1920. #endif
  1921. /**********************************************************************/
  1922. #ifdef L__time_t2tm
  1923. static const uint16_t _vals[] = {
  1924. 60, 60, 24, 7 /* special */, 36524, 1461, 365, 0
  1925. };
  1926. static const unsigned char days[] = {
  1927. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1928. 29,
  1929. };
  1930. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  1931. static const char utc_string[] = "UTC";
  1932. #endif
  1933. /* Notes:
  1934. * If time_t is 32 bits, then no overflow is possible.
  1935. * It time_t is > 32 bits, this needs to be adjusted to deal with overflow.
  1936. */
  1937. /* Note: offset is the correction in _days_ to *timer! */
  1938. struct tm attribute_hidden *_time_t2tm(const time_t *__restrict timer,
  1939. int offset, struct tm *__restrict result)
  1940. {
  1941. register int *p;
  1942. time_t t1, t, v;
  1943. int wday = wday; /* ok to be uninitialized, shutting up warning */
  1944. {
  1945. register const uint16_t *vp;
  1946. t = *timer;
  1947. p = (int *) result;
  1948. p[7] = 0;
  1949. vp = _vals;
  1950. do {
  1951. if ((v = *vp) == 7) {
  1952. /* Overflow checking, assuming time_t is long int... */
  1953. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1954. #if (INT_MAX == 2147483647L) && (LONG_MAX == 9223372036854775807L)
  1955. /* Valid range for t is [-784223472856L, 784223421720L].
  1956. * Outside of this range, the tm_year field will overflow. */
  1957. if (((unsigned long)(t + offset- -784223472856L))
  1958. > (784223421720L - -784223472856L)
  1959. ) {
  1960. return NULL;
  1961. }
  1962. #else
  1963. #error overflow conditions unknown
  1964. #endif
  1965. #endif
  1966. /* We have days since the epoch, so caluclate the weekday. */
  1967. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1968. wday = (t + 4) % (*vp); /* t is unsigned */
  1969. #else
  1970. wday = ((int)((t % (*vp)) + 11)) % ((int)(*vp)); /* help bcc */
  1971. #endif
  1972. /* Set divisor to days in 400 years. Be kind to bcc... */
  1973. v = ((time_t)(vp[1])) << 2;
  1974. ++v;
  1975. /* Change to days since 1/1/1601 so that for 32 bit time_t
  1976. * values, we'll have t >= 0. This should be changed for
  1977. * archs with larger time_t types.
  1978. * Also, correct for offset since a multiple of 7. */
  1979. /* TODO: Does this still work on archs with time_t > 32 bits? */
  1980. t += (135140L - 366) + offset; /* 146097 - (365*30 + 7) -366 */
  1981. }
  1982. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1983. t -= ((t1 = t / v) * v);
  1984. #else
  1985. if ((t -= ((t1 = t / v) * v)) < 0) {
  1986. t += v;
  1987. --t1;
  1988. }
  1989. #endif
  1990. if ((*vp == 7) && (t == v-1)) {
  1991. --t; /* Correct for 400th year leap case */
  1992. ++p[4]; /* Stash the extra day... */
  1993. }
  1994. #if defined(__BCC__) && 0
  1995. *p = t1;
  1996. if (v <= 60) {
  1997. *p = t;
  1998. t = t1;
  1999. }
  2000. ++p;
  2001. #else
  2002. if (v <= 60) {
  2003. *p++ = t;
  2004. t = t1;
  2005. } else {
  2006. *p++ = t1;
  2007. }
  2008. #endif
  2009. } while (*++vp);
  2010. }
  2011. if (p[-1] == 4) {
  2012. --p[-1];
  2013. t = 365;
  2014. }
  2015. *p += ((int) t); /* result[7] .. tm_yday */
  2016. p -= 2; /* at result[5] */
  2017. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  2018. /* Protect against overflow. TODO: Unecessary if int arith wraps? */
  2019. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + (p[1] - 299); /* tm_year */
  2020. #else
  2021. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + p[1] - 299; /* tm_year */
  2022. #endif
  2023. p[1] = wday; /* result[6] .. tm_wday */
  2024. {
  2025. register const unsigned char *d = days;
  2026. wday = 1900 + *p;
  2027. if (__isleap(wday)) {
  2028. d += 11;
  2029. }
  2030. wday = p[2] + 1; /* result[7] .. tm_yday */
  2031. *--p = 0; /* at result[4] .. tm_mon */
  2032. while (wday > *d) {
  2033. wday -= *d;
  2034. if (*d == 29) {
  2035. d -= 11; /* Backup to non-leap Feb. */
  2036. }
  2037. ++d;
  2038. ++*p; /* Increment tm_mon. */
  2039. }
  2040. p[-1] = wday; /* result[3] .. tm_mday */
  2041. }
  2042. /* TODO -- should this be 0? */
  2043. p[4] = 0; /* result[8] .. tm_isdst */
  2044. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  2045. # ifdef __USE_BSD
  2046. result->tm_gmtoff = 0;
  2047. result->tm_zone = utc_string;
  2048. # else
  2049. result->__tm_gmtoff = 0;
  2050. result->__tm_zone = utc_string;
  2051. # endif
  2052. #endif /* __UCLIBC_HAS_TM_EXTENSIONS__ */
  2053. return result;
  2054. }
  2055. #endif
  2056. /**********************************************************************/
  2057. #ifdef L___time_tm
  2058. struct tm __time_tm; /* Global shared by gmtime() and localtime(). */
  2059. #endif
  2060. /**********************************************************************/
  2061. #ifdef L__time_mktime
  2062. time_t attribute_hidden _time_mktime(struct tm *timeptr, int store_on_success)
  2063. {
  2064. time_t t;
  2065. __UCLIBC_MUTEX_LOCK(_time_tzlock);
  2066. tzset();
  2067. t = _time_mktime_tzi(timeptr, store_on_success, _time_tzinfo);
  2068. __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
  2069. return t;
  2070. }
  2071. #endif
  2072. /**********************************************************************/
  2073. #ifdef L__time_mktime_tzi
  2074. static const unsigned char __vals[] = {
  2075. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  2076. 29,
  2077. };
  2078. time_t attribute_hidden _time_mktime_tzi(struct tm *timeptr, int store_on_success,
  2079. rule_struct *tzi)
  2080. {
  2081. #ifdef __BCC__
  2082. long days, secs;
  2083. #else
  2084. long long secs;
  2085. #endif
  2086. time_t t;
  2087. struct tm x;
  2088. /* 0:sec 1:min 2:hour 3:mday 4:mon 5:year 6:wday 7:yday 8:isdst */
  2089. register int *p = (int *) &x;
  2090. register const unsigned char *s;
  2091. int d, default_dst;
  2092. memcpy(p, timeptr, sizeof(struct tm));
  2093. if (!tzi[1].tzname[0]) { /* No dst in this timezone, */
  2094. p[8] = 0; /* so set tm_isdst to 0. */
  2095. }
  2096. default_dst = 0;
  2097. if (p[8]) { /* Either dst or unknown? */
  2098. default_dst = 1; /* Assume advancing (even if unknown). */
  2099. p[8] = ((p[8] > 0) ? 1 : -1); /* Normalize so abs() <= 1. */
  2100. }
  2101. d = 400;
  2102. p[5] = (p[5] - ((p[6] = p[5]/d) * d)) + (p[7] = p[4]/12);
  2103. if ((p[4] -= 12 * p[7]) < 0) {
  2104. p[4] += 12;
  2105. --p[5];
  2106. }
  2107. s = __vals;
  2108. d = (p[5] += 1900); /* Correct year. Now between 1900 and 2300. */
  2109. if (__isleap(d)) {
  2110. s += 11;
  2111. }
  2112. p[7] = 0;
  2113. d = p[4];
  2114. while (d) {
  2115. p[7] += *s;
  2116. if (*s == 29) {
  2117. s -= 11; /* Backup to non-leap Feb. */
  2118. }
  2119. ++s;
  2120. --d;
  2121. }
  2122. _time_tzset (x.tm_year < 2007); /* tm_year was expanded above */
  2123. #ifdef __BCC__
  2124. d = p[5] - 1;
  2125. days = -719163L + ((long)d)*365 + ((d/4) - (d/100) + (d/400) + p[3] + p[7]);
  2126. secs = p[0] + 60*( p[1] + 60*((long)(p[2])) )
  2127. + tzi[default_dst].gmt_offset;
  2128. DST_CORRECT:
  2129. if (secs < 0) {
  2130. secs += 120009600L;
  2131. days -= 1389;
  2132. }
  2133. if ( ((unsigned long)(days + secs/86400L)) > 49710L) {
  2134. t = ((time_t)(-1));
  2135. goto DONE;
  2136. }
  2137. secs += (days * 86400L);
  2138. #else
  2139. d = p[5] - 1;
  2140. d = -719163L + d*365 + (d/4) - (d/100) + (d/400);
  2141. secs = p[0]
  2142. + tzi[default_dst].gmt_offset
  2143. + 60*( p[1]
  2144. + 60*(p[2]
  2145. + 24*(((146073L * ((long long)(p[6])) + d)
  2146. + p[3]) + p[7])));
  2147. DST_CORRECT:
  2148. if (((unsigned long long)(secs - LONG_MIN))
  2149. > (((unsigned long long)LONG_MAX) - LONG_MIN)
  2150. ) {
  2151. t = ((time_t)(-1));
  2152. goto DONE;
  2153. }
  2154. #endif
  2155. d = ((struct tm *)p)->tm_isdst;
  2156. t = secs;
  2157. __time_localtime_tzi(&t, (struct tm *)p, tzi);
  2158. if (t == ((time_t)(-1))) { /* Remember, time_t can be unsigned. */
  2159. goto DONE;
  2160. }
  2161. if ((d < 0) && (((struct tm *)p)->tm_isdst != default_dst)) {
  2162. #ifdef __BCC__
  2163. secs -= (days * 86400L);
  2164. #endif
  2165. secs += (tzi[1-default_dst].gmt_offset
  2166. - tzi[default_dst].gmt_offset);
  2167. goto DST_CORRECT;
  2168. }
  2169. if (store_on_success) {
  2170. memcpy(timeptr, p, sizeof(struct tm));
  2171. }
  2172. DONE:
  2173. return t;
  2174. }
  2175. #endif
  2176. /**********************************************************************/
  2177. #if (defined(L_wcsftime) || defined(L_wcsftime_l))
  2178. /* Implemented via strftime / strftime_l wchar_t variants */
  2179. #endif
  2180. /**********************************************************************/
  2181. #ifdef L_dysize
  2182. /* Return the number of days in YEAR. */
  2183. int dysize(int year)
  2184. {
  2185. return __isleap(year) ? 366 : 365;
  2186. }
  2187. #endif
  2188. /**********************************************************************/