stdlib.c 26 KB

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