stdlib.c 27 KB

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