stdlib.c 27 KB

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