stdlib.c 29 KB

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