stdlib.c 28 KB

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