stdlib.c 28 KB

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