stdlib.c 28 KB

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