stdlib.c 28 KB

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