stdlib.c 28 KB

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