stdlib.c 28 KB

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