stdlib.c 28 KB

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