stdlib.c 27 KB

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