stdlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  27. #define _GNU_SOURCE
  28. #include <stdlib.h>
  29. #include <limits.h>
  30. #include <stdint.h>
  31. #include <inttypes.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. extern unsigned long
  36. _stdlib_strto_l(register const char * __restrict str,
  37. char ** __restrict endptr, int base, int sflag);
  38. extern unsigned long long
  39. _stdlib_strto_ll(register const char * __restrict str,
  40. char ** __restrict endptr, int base, int sflag);
  41. /* TODO: gcc reports an error due to prototype conflicts. Don't include
  42. * the header for the problem cases? */
  43. #define HEADER_ALIAS_PROBLEM
  44. /**********************************************************************/
  45. #ifdef L_abs
  46. #ifdef HEADER_ALIAS_PROBLEM
  47. /* #if UINT_MAX < ULONG_MAX */
  48. int abs(int j)
  49. {
  50. return (j >= 0) ? j : -j;
  51. }
  52. #endif /* UINT_MAX < ULONG_MAX */
  53. #endif
  54. /**********************************************************************/
  55. #ifdef L_labs
  56. #ifndef HEADER_ALIAS_PROBLEM
  57. /* #if UINT_MAX == ULONG_MAX */
  58. strong_alias(labs,abs)
  59. #endif
  60. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  61. strong_alias(labs,llabs)
  62. #endif
  63. #if ULONG_MAX == UINTMAX_MAX
  64. strong_alias(labs,imaxabs)
  65. #endif
  66. long int labs(long int j)
  67. {
  68. return (j >= 0) ? j : -j;
  69. }
  70. #endif
  71. /**********************************************************************/
  72. #ifdef L_llabs
  73. #if defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)
  74. #if (ULLONG_MAX == UINTMAX_MAX)
  75. strong_alias(llabs,imaxabs)
  76. #endif
  77. long long int llabs(long long int j)
  78. {
  79. return (j >= 0) ? j : -j;
  80. }
  81. #endif /* defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX) */
  82. #endif
  83. /**********************************************************************/
  84. #ifdef L_atoi
  85. #ifdef HEADER_ALIAS_PROBLEM
  86. /* #if UINT_MAX < ULONG_MAX */
  87. int atoi(const char *nptr)
  88. {
  89. return (int) strtol(nptr, (char **) NULL, 10);
  90. }
  91. #endif /* UINT_MAX < ULONG_MAX */
  92. #endif
  93. /**********************************************************************/
  94. #ifdef L_atol
  95. #ifndef HEADER_ALIAS_PROBLEM
  96. /* #if UINT_MAX == ULONG_MAX */
  97. strong_alias(atol,atoi)
  98. #endif
  99. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  100. strong_alias(atol,atoll)
  101. #endif
  102. long atol(const char *nptr)
  103. {
  104. return strtol(nptr, (char **) NULL, 10);
  105. }
  106. #endif
  107. /**********************************************************************/
  108. #ifdef L_atoll
  109. #if defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)
  110. long long atoll(const char *nptr)
  111. {
  112. return strtoll(nptr, (char **) NULL, 10);
  113. }
  114. #endif /* defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX) */
  115. #endif
  116. /**********************************************************************/
  117. #ifdef L_strtol
  118. #if ULONG_MAX == UINTMAX_MAX
  119. strong_alias(strtol,strtoimax)
  120. #endif
  121. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  122. strong_alias(strtol,strtoll)
  123. #endif
  124. long strtol(const char * __restrict str, char ** __restrict endptr, int base)
  125. {
  126. return _stdlib_strto_l(str, endptr, base, 1);
  127. }
  128. #endif
  129. /**********************************************************************/
  130. #ifdef L_strtoll
  131. #if defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)
  132. #if (ULLONG_MAX == UINTMAX_MAX)
  133. strong_alias(strtoll,strtoimax)
  134. #endif
  135. long long strtoll(const char * __restrict str,
  136. char ** __restrict endptr, int base)
  137. {
  138. return (long long) _stdlib_strto_ll(str, endptr, base, 1);
  139. }
  140. #endif /* defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX) */
  141. #endif
  142. /**********************************************************************/
  143. #ifdef L_strtoul
  144. #if ULONG_MAX == UINTMAX_MAX
  145. strong_alias(strtoul,strtoumax)
  146. #endif
  147. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  148. strong_alias(strtoul,strtoull)
  149. #endif
  150. unsigned long strtoul(const char * __restrict str,
  151. char ** __restrict endptr, int base)
  152. {
  153. return _stdlib_strto_l(str, endptr, base, 0);
  154. }
  155. #endif
  156. /**********************************************************************/
  157. #ifdef L_strtoull
  158. #if defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)
  159. #if (ULLONG_MAX == UINTMAX_MAX)
  160. strong_alias(strtoull,strtoumax)
  161. #endif
  162. unsigned long long strtoull(const char * __restrict str,
  163. char ** __restrict endptr, int base)
  164. {
  165. return _stdlib_strto_ll(str, endptr, base, 0);
  166. }
  167. #endif /* defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX) */
  168. #endif
  169. /**********************************************************************/
  170. /* Support routines follow */
  171. /**********************************************************************/
  172. /* Set if we want errno set appropriately. */
  173. /* NOTE: Implies _STRTO_ENDPTR below */
  174. #define _STRTO_ERRNO 1
  175. /* Set if we want support for the endptr arg. */
  176. /* Implied by _STRTO_ERRNO. */
  177. #define _STRTO_ENDPTR 1
  178. #if _STRTO_ERRNO
  179. #undef _STRTO_ENDPTR
  180. #define _STRTO_ENDPTR 1
  181. #define SET_ERRNO(X) __set_errno(X)
  182. #else
  183. #define SET_ERRNO(X) ((void)(X)) /* keep side effects */
  184. #endif
  185. /**********************************************************************/
  186. #ifdef L__stdlib_strto_l
  187. /* This is the main work fuction which handles both strtol (sflag = 1) and
  188. * strtoul (sflag = 0). */
  189. unsigned long _stdlib_strto_l(register const char * __restrict str,
  190. char ** __restrict endptr, int base, int sflag)
  191. {
  192. unsigned long number, cutoff;
  193. #if _STRTO_ENDPTR
  194. const char *fail_char;
  195. #define SET_FAIL(X) fail_char = (X)
  196. #else
  197. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  198. #endif
  199. unsigned char negative, digit, cutoff_digit;
  200. assert((sflag == 0) || (sflag == 1));
  201. SET_FAIL(str);
  202. while (isspace(*str)) { /* Skip leading whitespace. */
  203. ++str;
  204. }
  205. /* Handle optional sign. */
  206. negative = 0;
  207. switch(*str) {
  208. case '-': negative = 1; /* Fall through to increment str. */
  209. case '+': ++str;
  210. }
  211. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  212. base += 10; /* Default is 10 (26). */
  213. if (*str == '0') {
  214. SET_FAIL(++str);
  215. base -= 2; /* Now base is 8 or 16 (24). */
  216. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  217. ++str;
  218. base += base; /* Base is 16 (16 or 48). */
  219. }
  220. }
  221. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  222. base = 16;
  223. }
  224. }
  225. number = 0;
  226. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  227. cutoff_digit = ULONG_MAX % base;
  228. cutoff = ULONG_MAX / base;
  229. do {
  230. digit = (((unsigned char)(*str - '0')) <= 9)
  231. ? (*str - '0')
  232. : ((*str >= 'A')
  233. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  234. : 40);
  235. if (digit >= base) {
  236. break;
  237. }
  238. SET_FAIL(++str);
  239. if ((number > cutoff)
  240. || ((number == cutoff) && (digit > cutoff_digit))) {
  241. number = ULONG_MAX;
  242. negative &= sflag;
  243. SET_ERRNO(ERANGE);
  244. } else {
  245. number = number * base + digit;
  246. }
  247. } while (1);
  248. }
  249. #if _STRTO_ENDPTR
  250. if (endptr) {
  251. *endptr = (char *) fail_char;
  252. }
  253. #endif
  254. {
  255. unsigned long tmp = ((negative)
  256. ? ((unsigned long)(-(1+LONG_MIN)))+1
  257. : LONG_MAX);
  258. if (sflag && (number > tmp)) {
  259. number = tmp;
  260. SET_ERRNO(ERANGE);
  261. }
  262. }
  263. return negative ? (unsigned long)(-((long)number)) : number;
  264. }
  265. #endif
  266. /**********************************************************************/
  267. #ifdef L__stdlib_strto_ll
  268. #if defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)
  269. /* This is the main work fuction which handles both strtoll (sflag = 1) and
  270. * strtoull (sflag = 0). */
  271. unsigned long long _stdlib_strto_ll(register const char * __restrict str,
  272. char ** __restrict endptr, int base,
  273. int sflag)
  274. {
  275. unsigned long long number;
  276. #if _STRTO_ENDPTR
  277. const char *fail_char;
  278. #define SET_FAIL(X) fail_char = (X)
  279. #else
  280. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  281. #endif
  282. unsigned int n1;
  283. unsigned char negative, digit;
  284. assert((sflag == 0) || (sflag == 1));
  285. SET_FAIL(str);
  286. while (isspace(*str)) { /* Skip leading whitespace. */
  287. ++str;
  288. }
  289. /* Handle optional sign. */
  290. negative = 0;
  291. switch(*str) {
  292. case '-': negative = 1; /* Fall through to increment str. */
  293. case '+': ++str;
  294. }
  295. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  296. base += 10; /* Default is 10 (26). */
  297. if (*str == '0') {
  298. SET_FAIL(++str);
  299. base -= 2; /* Now base is 8 or 16 (24). */
  300. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  301. ++str;
  302. base += base; /* Base is 16 (16 or 48). */
  303. }
  304. }
  305. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  306. base = 16;
  307. }
  308. }
  309. number = 0;
  310. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  311. do {
  312. digit = (((unsigned char)(*str - '0')) <= 9)
  313. ? (*str - '0')
  314. : ((*str >= 'A')
  315. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  316. : 40);
  317. if (digit >= base) {
  318. break;
  319. }
  320. SET_FAIL(++str);
  321. #if 1
  322. /* Optional, but speeds things up in the usual case. */
  323. if (number <= (ULLONG_MAX >> 6)) {
  324. number = number * base + digit;
  325. } else
  326. #endif
  327. {
  328. n1 = ((unsigned char) number) * base + digit;
  329. number = (number >> CHAR_BIT) * base;
  330. if (number + (n1 >> CHAR_BIT) <= (ULLONG_MAX >> CHAR_BIT)) {
  331. number = (number << CHAR_BIT) + n1;
  332. } else { /* Overflow. */
  333. number = ULLONG_MAX;
  334. negative &= sflag;
  335. SET_ERRNO(ERANGE);
  336. }
  337. }
  338. } while (1);
  339. }
  340. #if _STRTO_ENDPTR
  341. if (endptr) {
  342. *endptr = (char *) fail_char;
  343. }
  344. #endif
  345. {
  346. unsigned long long tmp = ((negative)
  347. ? ((unsigned long long)(-(1+LLONG_MIN)))+1
  348. : LLONG_MAX);
  349. if (sflag && (number > tmp)) {
  350. number = tmp;
  351. SET_ERRNO(ERANGE);
  352. }
  353. }
  354. return negative ? (unsigned long long)(-((long long)number)) : number;
  355. }
  356. #endif /* defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX) */
  357. #endif
  358. /**********************************************************************/