stdlib.c 28 KB

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