stdlib.c 28 KB

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