stdlib.c 27 KB

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