stdlib.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * From my (incomplete) stdlib library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  19. *
  20. * This code is currently under development. Also, I plan to port
  21. * it to elks which is a 16-bit environment with a fairly limited
  22. * compiler. Therefore, please refrain from modifying this code
  23. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  24. *
  25. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  26. /* Oct 29, 2002
  27. * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs.
  28. *
  29. * Nov 21, 2002
  30. * Add wscto{inttype} functions.
  31. */
  32. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  33. #include <limits.h>
  34. #include <stdint.h>
  35. /* Work around gcc's refusal to create aliases.
  36. * TODO: Add in a define to disable the aliases? */
  37. #if UINT_MAX == ULONG_MAX
  38. #ifdef L_labs
  39. #define abs __ignore_abs
  40. #endif
  41. #ifdef L_atol
  42. #define atoi __ignore_atoi
  43. #endif
  44. #endif
  45. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  46. #ifdef L_labs
  47. #define llabs __ignore_llabs
  48. #endif
  49. #ifdef L_atol
  50. #define atoll __ignore_atoll
  51. #endif
  52. #ifdef L_strtol
  53. #define strtoll __ignore_strtoll
  54. #endif
  55. #ifdef L_strtoul
  56. #define strtoull __ignore_strtoull
  57. #endif
  58. #ifdef L_wcstol
  59. #define wcstoll __ignore_wcstoll
  60. #endif
  61. #ifdef L_wcstoul
  62. #define wcstoull __ignore_wcstoull
  63. #endif
  64. #ifdef L_strtol_l
  65. #define strtoll_l __ignore_strtoll_l
  66. #endif
  67. #ifdef L_strtoul_l
  68. #define strtoull_l __ignore_strtoull_l
  69. #endif
  70. #ifdef L_wcstol_l
  71. #define wcstoll_l __ignore_wcstoll_l
  72. #endif
  73. #ifdef L_wcstoul_l
  74. #define wcstoull_l __ignore_wcstoull_l
  75. #endif
  76. #endif
  77. #if defined(ULLONG_MAX) && (ULLONG_MAX == UINTMAX_MAX)
  78. #if defined L_labs || defined L_llabs
  79. #define imaxabs __ignore_imaxabs
  80. #endif
  81. #endif
  82. #include <stdint.h>
  83. #include <inttypes.h>
  84. #include <ctype.h>
  85. #include <errno.h>
  86. #include <assert.h>
  87. #include <unistd.h>
  88. #include <stdlib.h>
  89. #include <locale.h>
  90. #ifdef __UCLIBC_HAS_WCHAR__
  91. #include <wchar.h>
  92. #include <wctype.h>
  93. #include <bits/uClibc_uwchar.h>
  94. #ifdef __UCLIBC_HAS_XLOCALE__
  95. #include <xlocale.h>
  96. #endif /* __UCLIBC_HAS_XLOCALE__ */
  97. /* TODO: clean up the following... */
  98. #if WCHAR_MAX > 0xffffUL
  99. #define UTF_8_MAX_LEN 6
  100. #else
  101. #define UTF_8_MAX_LEN 3
  102. #endif
  103. #ifdef __UCLIBC_HAS_LOCALE__
  104. #define ENCODING (__UCLIBC_CURLOCALE->encoding)
  105. #ifndef __CTYPE_HAS_UTF_8_LOCALES
  106. #ifdef L_mblen
  107. /* emit only once */
  108. #warning __CTYPE_HAS_UTF_8_LOCALES not set!
  109. #endif
  110. #endif
  111. #else /* __UCLIBC_HAS_LOCALE__ */
  112. #ifdef __UCLIBC_MJN3_ONLY__
  113. #ifdef L_mblen
  114. /* emit only once */
  115. #warning devel checks
  116. #endif
  117. #endif
  118. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  119. #error __CTYPE_HAS_8_BIT_LOCALES is defined!
  120. #endif
  121. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  122. #error __CTYPE_HAS_UTF_8_LOCALES is defined!
  123. #endif
  124. #endif
  125. #endif /* __UCLIBC_HAS_LOCALE__ */
  126. /**********************************************************************/
  127. #ifdef __UCLIBC_HAS_XLOCALE__
  128. extern unsigned long
  129. _stdlib_strto_l_l(register const char * __restrict str,
  130. char ** __restrict endptr, int base, int sflag,
  131. __locale_t locale_arg) attribute_hidden;
  132. #if defined(ULLONG_MAX)
  133. extern unsigned long long
  134. _stdlib_strto_ll_l(register const char * __restrict str,
  135. char ** __restrict endptr, int base, int sflag,
  136. __locale_t locale_arg) attribute_hidden;
  137. #endif
  138. #ifdef __UCLIBC_HAS_WCHAR__
  139. extern unsigned long
  140. _stdlib_wcsto_l_l(register const wchar_t * __restrict str,
  141. wchar_t ** __restrict endptr, int base, int sflag,
  142. __locale_t locale_arg) attribute_hidden;
  143. #if defined(ULLONG_MAX)
  144. extern unsigned long long
  145. _stdlib_wcsto_ll_l(register const wchar_t * __restrict str,
  146. wchar_t ** __restrict endptr, int base, int sflag,
  147. __locale_t locale_arg) attribute_hidden;
  148. #endif
  149. #endif /* __UCLIBC_HAS_WCHAR__ */
  150. #endif /* __UCLIBC_HAS_XLOCALE__ */
  151. extern unsigned long
  152. _stdlib_strto_l(register const char * __restrict str,
  153. char ** __restrict endptr, int base, int sflag) attribute_hidden;
  154. #if defined(ULLONG_MAX)
  155. extern unsigned long long
  156. _stdlib_strto_ll(register const char * __restrict str,
  157. char ** __restrict endptr, int base, int sflag) attribute_hidden;
  158. #endif
  159. #ifdef __UCLIBC_HAS_WCHAR__
  160. extern unsigned long
  161. _stdlib_wcsto_l(register const wchar_t * __restrict str,
  162. wchar_t ** __restrict endptr, int base, int sflag) attribute_hidden;
  163. #if defined(ULLONG_MAX)
  164. extern unsigned long long
  165. _stdlib_wcsto_ll(register const wchar_t * __restrict str,
  166. wchar_t ** __restrict endptr, int base, int sflag) attribute_hidden;
  167. #endif
  168. #endif /* __UCLIBC_HAS_WCHAR__ */
  169. /**********************************************************************/
  170. #ifdef L_atof
  171. double atof(const char *nptr)
  172. {
  173. return strtod(nptr, (char **) NULL);
  174. }
  175. #endif
  176. /**********************************************************************/
  177. #ifdef L_abs
  178. #if INT_MAX < LONG_MAX
  179. int abs(int j)
  180. {
  181. return (j >= 0) ? j : -j;
  182. }
  183. #endif /* INT_MAX < LONG_MAX */
  184. #endif
  185. /**********************************************************************/
  186. #ifdef L_labs
  187. long int labs(long int j)
  188. {
  189. return (j >= 0) ? j : -j;
  190. }
  191. #if UINT_MAX == ULONG_MAX
  192. #undef abs
  193. extern __typeof(labs) abs;
  194. strong_alias(labs,abs)
  195. #endif
  196. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  197. #undef llabs
  198. extern __typeof(labs) llabs;
  199. strong_alias(labs,llabs)
  200. #endif
  201. #if ULONG_MAX == UINTMAX_MAX
  202. #undef imaxabs
  203. extern __typeof(labs) imaxabs;
  204. strong_alias(labs,imaxabs)
  205. #endif
  206. #endif
  207. /**********************************************************************/
  208. #ifdef L_llabs
  209. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  210. long long int llabs(long long int j)
  211. {
  212. return (j >= 0) ? j : -j;
  213. }
  214. #if (ULLONG_MAX == UINTMAX_MAX)
  215. #undef imaxabs
  216. extern __typeof(llabs) imaxabs;
  217. strong_alias(llabs,imaxabs)
  218. #endif
  219. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  220. #endif
  221. /**********************************************************************/
  222. #ifdef L_atoi
  223. #if INT_MAX < LONG_MAX
  224. int atoi(const char *nptr)
  225. {
  226. return (int) strtol(nptr, (char **) NULL, 10);
  227. }
  228. libc_hidden_def(atoi)
  229. #endif /* INT_MAX < LONG_MAX */
  230. #endif
  231. /**********************************************************************/
  232. #ifdef L_atol
  233. long atol(const char *nptr)
  234. {
  235. return strtol(nptr, (char **) NULL, 10);
  236. }
  237. libc_hidden_def(atol)
  238. #if UINT_MAX == ULONG_MAX
  239. #undef atoi
  240. extern __typeof(atol) atoi;
  241. libc_hidden_proto(atoi)
  242. strong_alias(atol,atoi)
  243. libc_hidden_def(atoi)
  244. #endif
  245. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  246. #undef atoll
  247. extern __typeof(atol) atoll;
  248. strong_alias(atol,atoll)
  249. #endif
  250. #endif
  251. /**********************************************************************/
  252. #ifdef L_atoll
  253. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  254. long long atoll(const char *nptr)
  255. {
  256. return strtoll(nptr, (char **) NULL, 10);
  257. }
  258. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  259. #endif
  260. /**********************************************************************/
  261. #if defined(L_strtol) || defined(L_strtol_l)
  262. libc_hidden_proto(__XL_NPP(strtol))
  263. long __XL_NPP(strtol)(const char * __restrict str, char ** __restrict endptr,
  264. int base __LOCALE_PARAM)
  265. {
  266. return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 1 __LOCALE_ARG);
  267. }
  268. libc_hidden_def(__XL_NPP(strtol))
  269. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtol_l)
  270. strong_alias(strtol,strtoimax)
  271. #endif
  272. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  273. #ifdef L_strtol_l
  274. #undef strtoll_l
  275. #else
  276. #undef strtoll
  277. #endif
  278. extern __typeof(__XL_NPP(strtol)) __XL_NPP(strtoll);
  279. libc_hidden_proto(__XL_NPP(strtoll))
  280. strong_alias(__XL_NPP(strtol),__XL_NPP(strtoll))
  281. libc_hidden_def(__XL_NPP(strtoll))
  282. #if !defined(L_strtol_l)
  283. strong_alias(strtol,strtoq)
  284. #endif
  285. #endif
  286. #endif
  287. /**********************************************************************/
  288. #if defined(L_strtoll) || defined(L_strtoll_l)
  289. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  290. libc_hidden_proto(__XL_NPP(strtoll))
  291. long long __XL_NPP(strtoll)(const char * __restrict str,
  292. char ** __restrict endptr, int base
  293. __LOCALE_PARAM)
  294. {
  295. return (long long) __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 1 __LOCALE_ARG);
  296. }
  297. libc_hidden_def(__XL_NPP(strtoll))
  298. #if !defined(L_strtoll_l)
  299. #if (ULLONG_MAX == UINTMAX_MAX)
  300. strong_alias(strtoll,strtoimax)
  301. #endif
  302. strong_alias(strtoll,strtoq)
  303. #endif
  304. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  305. #endif
  306. /**********************************************************************/
  307. #if defined(L_strtoul) || defined(L_strtoul_l)
  308. unsigned long __XL_NPP(strtoul)(const char * __restrict str,
  309. char ** __restrict endptr, int base
  310. __LOCALE_PARAM)
  311. {
  312. return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 0 __LOCALE_ARG);
  313. }
  314. libc_hidden_def(__XL_NPP(strtoul))
  315. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtoul_l)
  316. strong_alias(strtoul,strtoumax)
  317. #endif
  318. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  319. #ifdef L_strtoul_l
  320. #undef strtoull_l
  321. #else
  322. #undef strtoull
  323. #endif
  324. extern __typeof(__XL_NPP(strtoul)) __XL_NPP(strtoull);
  325. libc_hidden_proto(__XL_NPP(strtoull))
  326. strong_alias(__XL_NPP(strtoul),__XL_NPP(strtoull))
  327. libc_hidden_def(__XL_NPP(strtoull))
  328. #if !defined(L_strtoul_l)
  329. strong_alias(strtoul,strtouq)
  330. #endif
  331. #endif
  332. #endif
  333. /**********************************************************************/
  334. #if defined(L_strtoull) || defined(L_strtoull_l)
  335. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  336. libc_hidden_proto(__XL_NPP(strtoull))
  337. unsigned long long __XL_NPP(strtoull)(const char * __restrict str,
  338. char ** __restrict endptr, int base
  339. __LOCALE_PARAM)
  340. {
  341. return __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 0 __LOCALE_ARG);
  342. }
  343. libc_hidden_def(__XL_NPP(strtoull))
  344. #if !defined(L_strtoull_l)
  345. #if (ULLONG_MAX == UINTMAX_MAX)
  346. strong_alias(strtoull,strtoumax)
  347. #endif
  348. strong_alias(strtoull,strtouq)
  349. #endif
  350. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  351. #endif
  352. /**********************************************************************/
  353. /* Support routines follow */
  354. /**********************************************************************/
  355. /* Set if we want errno set appropriately. */
  356. /* NOTE: Implies _STRTO_ENDPTR below */
  357. #define _STRTO_ERRNO 1
  358. /* Set if we want support for the endptr arg. */
  359. /* Implied by _STRTO_ERRNO. */
  360. #define _STRTO_ENDPTR 1
  361. #if _STRTO_ERRNO
  362. #undef _STRTO_ENDPTR
  363. #define _STRTO_ENDPTR 1
  364. #define SET_ERRNO(X) __set_errno(X)
  365. #else
  366. #define SET_ERRNO(X) ((void)(X)) /* keep side effects */
  367. #endif
  368. /**********************************************************************/
  369. #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
  370. #ifndef L__stdlib_strto_l
  371. #define L__stdlib_strto_l
  372. #endif
  373. #endif
  374. #if defined(L__stdlib_strto_l) || defined(L__stdlib_strto_l_l)
  375. #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
  376. #define _stdlib_strto_l _stdlib_wcsto_l
  377. #define _stdlib_strto_l_l _stdlib_wcsto_l_l
  378. #define Wchar wchar_t
  379. #define Wuchar __uwchar_t
  380. #ifdef __UCLIBC_DO_XLOCALE
  381. #define ISSPACE(C) iswspace_l((C), locale_arg)
  382. #else
  383. #define ISSPACE(C) iswspace((C))
  384. #endif
  385. #else /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
  386. #define Wchar char
  387. #define Wuchar unsigned char
  388. #ifdef __UCLIBC_DO_XLOCALE
  389. #define ISSPACE(C) isspace_l((C), locale_arg)
  390. #else
  391. #define ISSPACE(C) isspace((C))
  392. #endif
  393. #endif /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
  394. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  395. unsigned long attribute_hidden _stdlib_strto_l(register const Wchar * __restrict str,
  396. Wchar ** __restrict endptr, int base,
  397. int sflag)
  398. {
  399. return _stdlib_strto_l_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
  400. }
  401. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  402. /* This is the main work fuction which handles both strtol (sflag = 1) and
  403. * strtoul (sflag = 0). */
  404. unsigned long attribute_hidden __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str,
  405. Wchar ** __restrict endptr, int base,
  406. int sflag __LOCALE_PARAM)
  407. {
  408. unsigned long number, cutoff;
  409. #if _STRTO_ENDPTR
  410. const Wchar *fail_char;
  411. #define SET_FAIL(X) fail_char = (X)
  412. #else
  413. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  414. #endif
  415. unsigned char negative, digit, cutoff_digit;
  416. assert(((unsigned int)sflag) <= 1);
  417. SET_FAIL(str);
  418. while (ISSPACE(*str)) { /* Skip leading whitespace. */
  419. ++str;
  420. }
  421. /* Handle optional sign. */
  422. negative = 0;
  423. switch (*str) {
  424. case '-': negative = 1; /* Fall through to increment str. */
  425. case '+': ++str;
  426. }
  427. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  428. base += 10; /* Default is 10 (26). */
  429. if (*str == '0') {
  430. SET_FAIL(++str);
  431. base -= 2; /* Now base is 8 or 16 (24). */
  432. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  433. ++str;
  434. base += base; /* Base is 16 (16 or 48). */
  435. }
  436. }
  437. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  438. base = 16;
  439. }
  440. }
  441. number = 0;
  442. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  443. cutoff_digit = ULONG_MAX % base;
  444. cutoff = ULONG_MAX / base;
  445. do {
  446. digit = ((Wuchar)(*str - '0') <= 9)
  447. ? /* 0..9 */ (*str - '0')
  448. : /* else */ (((Wuchar)(0x20 | *str) >= 'a') /* WARNING: assumes ascii. */
  449. ? /* >= A/a */ ((Wuchar)(0x20 | *str) - ('a' - 10))
  450. : /* else */ 40 /* bad value */);
  451. if (digit >= base) {
  452. break;
  453. }
  454. SET_FAIL(++str);
  455. if ((number > cutoff)
  456. || ((number == cutoff) && (digit > cutoff_digit))) {
  457. number = ULONG_MAX;
  458. negative &= sflag;
  459. SET_ERRNO(ERANGE);
  460. } else {
  461. number = number * base + digit;
  462. }
  463. } while (1);
  464. }
  465. #if _STRTO_ENDPTR
  466. if (endptr) {
  467. *endptr = (Wchar *) fail_char;
  468. }
  469. #endif
  470. {
  471. unsigned long tmp = (negative
  472. ? ((unsigned long)(-(1+LONG_MIN)))+1
  473. : LONG_MAX);
  474. if (sflag && (number > tmp)) {
  475. number = tmp;
  476. SET_ERRNO(ERANGE);
  477. }
  478. }
  479. return negative ? (unsigned long)(-((long)number)) : number;
  480. }
  481. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  482. #endif
  483. /**********************************************************************/
  484. #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
  485. #ifndef L__stdlib_strto_ll
  486. #define L__stdlib_strto_ll
  487. #endif
  488. #endif
  489. #if defined(L__stdlib_strto_ll) || defined(L__stdlib_strto_ll_l)
  490. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  491. #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
  492. #define _stdlib_strto_ll _stdlib_wcsto_ll
  493. #define _stdlib_strto_ll_l _stdlib_wcsto_ll_l
  494. #define Wchar wchar_t
  495. #define Wuchar __uwchar_t
  496. #ifdef __UCLIBC_DO_XLOCALE
  497. #define ISSPACE(C) iswspace_l((C), locale_arg)
  498. #else
  499. #define ISSPACE(C) iswspace((C))
  500. #endif
  501. #else /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
  502. #define Wchar char
  503. #define Wuchar unsigned char
  504. #ifdef __UCLIBC_DO_XLOCALE
  505. #define ISSPACE(C) isspace_l((C), locale_arg)
  506. #else
  507. #define ISSPACE(C) isspace((C))
  508. #endif
  509. #endif /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
  510. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  511. unsigned long long attribute_hidden _stdlib_strto_ll(register const Wchar * __restrict str,
  512. Wchar ** __restrict endptr, int base,
  513. int sflag)
  514. {
  515. return _stdlib_strto_ll_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
  516. }
  517. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  518. /* This is the main work fuction which handles both strtoll (sflag = 1) and
  519. * strtoull (sflag = 0). */
  520. unsigned long long attribute_hidden __XL_NPP(_stdlib_strto_ll)(register const Wchar * __restrict str,
  521. Wchar ** __restrict endptr, int base,
  522. int sflag __LOCALE_PARAM)
  523. {
  524. unsigned long long number;
  525. #if _STRTO_ENDPTR
  526. const Wchar *fail_char;
  527. #define SET_FAIL(X) fail_char = (X)
  528. #else
  529. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  530. #endif
  531. unsigned int n1;
  532. unsigned char negative, digit;
  533. assert(((unsigned int)sflag) <= 1);
  534. SET_FAIL(str);
  535. while (ISSPACE(*str)) { /* Skip leading whitespace. */
  536. ++str;
  537. }
  538. /* Handle optional sign. */
  539. negative = 0;
  540. switch (*str) {
  541. case '-': negative = 1; /* Fall through to increment str. */
  542. case '+': ++str;
  543. }
  544. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  545. base += 10; /* Default is 10 (26). */
  546. if (*str == '0') {
  547. SET_FAIL(++str);
  548. base -= 2; /* Now base is 8 or 16 (24). */
  549. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  550. ++str;
  551. base += base; /* Base is 16 (16 or 48). */
  552. }
  553. }
  554. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  555. base = 16;
  556. }
  557. }
  558. number = 0;
  559. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  560. do {
  561. digit = ((Wuchar)(*str - '0') <= 9)
  562. ? /* 0..9 */ (*str - '0')
  563. : /* else */ (((Wuchar)(0x20 | *str) >= 'a') /* WARNING: assumes ascii. */
  564. ? /* >= A/a */ ((Wuchar)(0x20 | *str) - ('a' - 10))
  565. : /* else */ 40 /* bad value */);
  566. if (digit >= base) {
  567. break;
  568. }
  569. SET_FAIL(++str);
  570. #if 1
  571. /* Optional, but speeds things up in the usual case. */
  572. if (number <= (ULLONG_MAX >> 6)) {
  573. number = number * base + digit;
  574. } else
  575. #endif
  576. {
  577. n1 = ((unsigned char) number) * base + digit;
  578. number = (number >> CHAR_BIT) * base;
  579. if (number + (n1 >> CHAR_BIT) <= (ULLONG_MAX >> CHAR_BIT)) {
  580. number = (number << CHAR_BIT) + n1;
  581. } else { /* Overflow. */
  582. number = ULLONG_MAX;
  583. negative &= sflag;
  584. SET_ERRNO(ERANGE);
  585. }
  586. }
  587. } while (1);
  588. }
  589. #if _STRTO_ENDPTR
  590. if (endptr) {
  591. *endptr = (Wchar *) fail_char;
  592. }
  593. #endif
  594. {
  595. unsigned long long tmp = ((negative)
  596. ? ((unsigned long long)(-(1+LLONG_MIN)))+1
  597. : LLONG_MAX);
  598. if (sflag && (number > tmp)) {
  599. number = tmp;
  600. SET_ERRNO(ERANGE);
  601. }
  602. }
  603. return negative ? (unsigned long long)(-((long long)number)) : number;
  604. }
  605. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  606. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  607. #endif
  608. /**********************************************************************/
  609. /* Made _Exit() an alias for _exit(), as per C99. */
  610. /* #ifdef L__Exit */
  611. /* void _Exit(int status) */
  612. /* { */
  613. /* _exit(status); */
  614. /* } */
  615. /* #endif */
  616. /**********************************************************************/
  617. #ifdef L_bsearch
  618. void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
  619. size_t size, int (*compar)(const void *, const void *))
  620. {
  621. register char *p;
  622. size_t low;
  623. size_t mid;
  624. int r;
  625. if (size > 0) { /* TODO: change this to an assert?? */
  626. low = 0;
  627. while (low < high) {
  628. mid = low + ((high - low) >> 1); /* Avoid possible overflow here. */
  629. p = ((char *)base) + mid * size; /* Could overflow here... */
  630. r = (*compar)(key, p); /* but that's an application problem! */
  631. if (r > 0) {
  632. low = mid + 1;
  633. } else if (r < 0) {
  634. high = mid;
  635. } else {
  636. return p;
  637. }
  638. }
  639. }
  640. return NULL;
  641. }
  642. #endif
  643. /**********************************************************************/
  644. #ifdef L_qsort
  645. /* This code is derived from a public domain shell sort routine by
  646. * Ray Gardner and found in Bob Stout's snippets collection. The
  647. * original code is included below in an #if 0/#endif block.
  648. *
  649. * I modified it to avoid the possibility of overflow in the wgap
  650. * calculation, as well as to reduce the generated code size with
  651. * bcc and gcc. */
  652. void qsort(void *base,
  653. size_t nel,
  654. size_t width,
  655. int (*comp)(const void *, const void *))
  656. {
  657. size_t wgap, i, j, k;
  658. char tmp;
  659. if ((nel > 1) && (width > 0)) {
  660. assert(nel <= ((size_t)(-1)) / width); /* check for overflow */
  661. wgap = 0;
  662. do {
  663. wgap = 3 * wgap + 1;
  664. } while (wgap < (nel-1)/3);
  665. /* From the above, we know that either wgap == 1 < nel or */
  666. /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */
  667. wgap *= width; /* So this can not overflow if wnel doesn't. */
  668. nel *= width; /* Convert nel to 'wnel' */
  669. do {
  670. i = wgap;
  671. do {
  672. j = i;
  673. do {
  674. register char *a;
  675. register char *b;
  676. j -= wgap;
  677. a = j + ((char *)base);
  678. b = a + wgap;
  679. if ((*comp)(a, b) <= 0) {
  680. break;
  681. }
  682. k = width;
  683. do {
  684. tmp = *a;
  685. *a++ = *b;
  686. *b++ = tmp;
  687. } while (--k);
  688. } while (j >= wgap);
  689. i += width;
  690. } while (i < nel);
  691. wgap = (wgap - width)/3;
  692. } while (wgap);
  693. }
  694. }
  695. libc_hidden_def(qsort)
  696. /* ---------- original snippets version below ---------- */
  697. #if 0
  698. /*
  699. ** ssort() -- Fast, small, qsort()-compatible Shell sort
  700. **
  701. ** by Ray Gardner, public domain 5/90
  702. */
  703. #include <stddef.h>
  704. void ssort(void *base,
  705. size_t nel,
  706. size_t width,
  707. int (*comp)(const void *, const void *))
  708. {
  709. size_t wnel, gap, wgap, i, j, k;
  710. char *a, *b, tmp;
  711. wnel = width * nel;
  712. for (gap = 0; ++gap < nel;)
  713. gap *= 3;
  714. while ((gap /= 3) != 0) {
  715. wgap = width * gap;
  716. for (i = wgap; i < wnel; i += width) {
  717. for (j = i - wgap; ;j -= wgap) {
  718. a = j + (char *)base;
  719. b = a + wgap;
  720. if ((*comp)(a, b) <= 0)
  721. break;
  722. k = width;
  723. do {
  724. tmp = *a;
  725. *a++ = *b;
  726. *b++ = tmp;
  727. } while (--k);
  728. if (j < wgap)
  729. break;
  730. }
  731. }
  732. }
  733. }
  734. #endif
  735. #endif
  736. /**********************************************************************/
  737. #ifdef L__stdlib_mb_cur_max
  738. size_t _stdlib_mb_cur_max(void)
  739. {
  740. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  741. return __UCLIBC_CURLOCALE->mb_cur_max;
  742. #else
  743. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  744. #ifdef __UCLIBC_MJN3_ONLY__
  745. #warning need to change this when/if transliteration is implemented
  746. #endif
  747. #endif
  748. return 1;
  749. #endif
  750. }
  751. libc_hidden_def(_stdlib_mb_cur_max)
  752. #endif
  753. #ifdef __UCLIBC_HAS_LOCALE__
  754. /*
  755. * The following function return 1 if the encoding is stateful, 0 if stateless.
  756. * To note, until now all the supported encoding are stateless.
  757. */
  758. static __always_inline int is_stateful(unsigned char encoding)
  759. {
  760. switch (encoding)
  761. {
  762. case __ctype_encoding_7_bit:
  763. case __ctype_encoding_utf8:
  764. case __ctype_encoding_8_bit:
  765. return 0;
  766. default:
  767. assert(0);
  768. return -1;
  769. }
  770. }
  771. #else
  772. #define is_stateful(encoding) 0
  773. #endif
  774. /**********************************************************************/
  775. #ifdef L_mblen
  776. int mblen(register const char *s, size_t n)
  777. {
  778. static mbstate_t state;
  779. size_t r;
  780. if (!s) {
  781. state.__mask = 0;
  782. /*
  783. In this case we have to return 0 because the only multibyte supported encoding
  784. is utf-8, that is a stateless encoding. See mblen() documentation.
  785. */
  786. return is_stateful(ENCODING);
  787. }
  788. if (*s == '\0')
  789. /* According to the ISO C 89 standard this is the expected behaviour. */
  790. return 0;
  791. if ((r = mbrlen(s, n, &state)) == (size_t) -2) {
  792. /* TODO: Should we set an error state? */
  793. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  794. return -1; /* TODO: Change error code above? */
  795. }
  796. return r;
  797. }
  798. #endif
  799. /**********************************************************************/
  800. #ifdef L_mbtowc
  801. int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
  802. {
  803. static mbstate_t state;
  804. size_t r;
  805. if (!s) {
  806. state.__mask = 0;
  807. /*
  808. In this case we have to return 0 because the only multibyte supported encoding
  809. is utf-8, that is a stateless encoding. See mbtowc() documentation.
  810. */
  811. return is_stateful(ENCODING);
  812. }
  813. if (*s == '\0')
  814. /* According to the ISO C 89 standard this is the expected behaviour. */
  815. return 0;
  816. if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
  817. /* TODO: Should we set an error state? */
  818. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  819. return -1; /* TODO: Change error code above? */
  820. }
  821. return r;
  822. }
  823. #endif
  824. /**********************************************************************/
  825. #ifdef L_wctomb
  826. /* Note: We completely ignore state in all currently supported conversions. */
  827. int wctomb(register char *__restrict s, wchar_t swc)
  828. {
  829. return (!s)
  830. ?
  831. /*
  832. In this case we have to return 0 because the only multibyte supported encoding
  833. is utf-8, that is a stateless encoding. See wctomb() documentation.
  834. */
  835. is_stateful(ENCODING)
  836. : ((ssize_t) wcrtomb(s, swc, NULL));
  837. }
  838. #endif
  839. /**********************************************************************/
  840. #ifdef L_mbstowcs
  841. size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
  842. {
  843. mbstate_t state;
  844. const char *e = s; /* Needed because of restrict. */
  845. state.__mask = 0; /* Always start in initial shift state. */
  846. return mbsrtowcs(pwcs, &e, n, &state);
  847. }
  848. #endif
  849. /**********************************************************************/
  850. #ifdef L_wcstombs
  851. /* Note: We completely ignore state in all currently supported conversions. */
  852. size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
  853. {
  854. const wchar_t *e = pwcs; /* Needed because of restrict. */
  855. return wcsrtombs(s, &e, n, NULL);
  856. }
  857. #endif
  858. /**********************************************************************/
  859. #if defined(L_wcstol) || defined(L_wcstol_l)
  860. libc_hidden_proto(__XL_NPP(wcstol))
  861. long __XL_NPP(wcstol)(const wchar_t * __restrict str,
  862. wchar_t ** __restrict endptr, int base __LOCALE_PARAM)
  863. {
  864. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 1 __LOCALE_ARG);
  865. }
  866. libc_hidden_def(__XL_NPP(wcstol))
  867. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstol_l)
  868. strong_alias(wcstol,wcstoimax)
  869. #endif
  870. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  871. #ifdef L_wcstol_l
  872. #undef wcstoll_l
  873. #else
  874. #undef wcstoll
  875. #endif
  876. extern __typeof(__XL_NPP(wcstol)) __XL_NPP(wcstoll);
  877. libc_hidden_proto(__XL_NPP(wcstoll))
  878. strong_alias(__XL_NPP(wcstol),__XL_NPP(wcstoll))
  879. libc_hidden_def(__XL_NPP(wcstoll))
  880. #endif
  881. #endif
  882. /**********************************************************************/
  883. #if defined(L_wcstoll) || defined(L_wcstoll_l)
  884. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  885. libc_hidden_proto(__XL_NPP(wcstoll))
  886. long long __XL_NPP(wcstoll)(const wchar_t * __restrict str,
  887. wchar_t ** __restrict endptr, int base
  888. __LOCALE_PARAM)
  889. {
  890. return (long long) __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 1 __LOCALE_ARG);
  891. }
  892. libc_hidden_def(__XL_NPP(wcstoll))
  893. #if !defined(L_wcstoll_l)
  894. #if (ULLONG_MAX == UINTMAX_MAX)
  895. strong_alias(wcstoll,wcstoimax)
  896. #endif
  897. strong_alias(wcstoll,wcstoq)
  898. #endif
  899. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  900. #endif
  901. /**********************************************************************/
  902. #if defined(L_wcstoul) || defined(L_wcstoul_l)
  903. libc_hidden_proto(__XL_NPP(wcstoul))
  904. unsigned long __XL_NPP(wcstoul)(const wchar_t * __restrict str,
  905. wchar_t ** __restrict endptr, int base
  906. __LOCALE_PARAM)
  907. {
  908. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 0 __LOCALE_ARG);
  909. }
  910. libc_hidden_def(__XL_NPP(wcstoul))
  911. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstoul_l)
  912. strong_alias(wcstoul,wcstoumax)
  913. #endif
  914. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  915. #ifdef L_wcstoul_l
  916. #undef wcstoull_l
  917. #else
  918. #undef wcstoull
  919. #endif
  920. extern __typeof(__XL_NPP(wcstoul)) __XL_NPP(wcstoull);
  921. libc_hidden_proto(__XL_NPP(wcstoull))
  922. strong_alias(__XL_NPP(wcstoul),__XL_NPP(wcstoull))
  923. libc_hidden_def(__XL_NPP(wcstoull))
  924. #endif
  925. #endif
  926. /**********************************************************************/
  927. #if defined(L_wcstoull) || defined(L_wcstoull_l)
  928. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  929. libc_hidden_proto(__XL_NPP(wcstoull))
  930. unsigned long long __XL_NPP(wcstoull)(const wchar_t * __restrict str,
  931. wchar_t ** __restrict endptr, int base
  932. __LOCALE_PARAM)
  933. {
  934. return __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 0 __LOCALE_ARG);
  935. }
  936. libc_hidden_def(__XL_NPP(wcstoull))
  937. #if !defined(L_wcstoull_l)
  938. #if (ULLONG_MAX == UINTMAX_MAX)
  939. strong_alias(wcstoull,wcstoumax)
  940. #endif
  941. strong_alias(wcstoull,wcstouq)
  942. #endif
  943. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  944. #endif
  945. /**********************************************************************/