stdlib.c 26 KB

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