📄 stdlib.c
字号:
/* Copyright (C) 2002 Manuel Novoa III * From my (incomplete) stdlib library for linux and (soon) elks. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! * * This code is currently under development. Also, I plan to port * it to elks which is a 16-bit environment with a fairly limited * compiler. Therefore, please refrain from modifying this code * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel * * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! *//* Oct 29, 2002 * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs. * * Nov 21, 2002 * Add wscto{inttype} functions. */#define _ISOC99_SOURCE /* for ULLONG primarily... */#define _GNU_SOURCE#include <limits.h>#include <stdint.h>#include <inttypes.h>#include <ctype.h>#include <errno.h>#include <assert.h>#include <unistd.h>/* Work around gcc's refusal to create aliases. * TODO: Add in a define to disable the aliases? */#if UINT_MAX == ULONG_MAX#define atoi __ignore_atoi#define abs __ignore_abs#endif#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)#define llabs __ignore_llabs#define atoll __ignore_atoll#define strtoll __ignore_strtoll#define strtoull __ignore_strtoull#define wcstoll __ignore_wcstoll#define wcstoull __ignore_wcstoull#define strtoll_l __ignore_strtoll_l#define strtoull_l __ignore_strtoull_l#define wcstoll_l __ignore_wcstoll_l#define wcstoull_l __ignore_wcstoull_l#endif#include <stdlib.h>#include <locale.h>#ifdef __UCLIBC_HAS_WCHAR__#include <wchar.h>#include <wctype.h>#include <bits/uClibc_uwchar.h>#ifdef __UCLIBC_HAS_XLOCALE__#include <xlocale.h>#endif /* __UCLIBC_HAS_XLOCALE__ *//* TODO: clean up the following... */#if WCHAR_MAX > 0xffffUL#define UTF_8_MAX_LEN 6#else#define UTF_8_MAX_LEN 3#endif#ifdef __UCLIBC_HAS_LOCALE__#define ENCODING ((__UCLIBC_CURLOCALE_DATA).encoding)#ifndef __CTYPE_HAS_UTF_8_LOCALES#ifdef L_mblen/* emit only once */#warning __CTYPE_HAS_UTF_8_LOCALES not set!#endif#endif#else /* __UCLIBC_HAS_LOCALE__ */#ifdef __UCLIBC_MJN3_ONLY__#ifdef L_mblen/* emit only once */#warning devel checks#endif#endif#ifdef __CTYPE_HAS_8_BIT_LOCALES#error __CTYPE_HAS_8_BIT_LOCALES is defined!#endif#ifdef __CTYPE_HAS_UTF_8_LOCALES#error __CTYPE_HAS_UTF_8_LOCALES is defined!#endif#endif#endif /* __UCLIBC_HAS_LOCALE__ */#if UINT_MAX == ULONG_MAX#undef atoi#undef abs#endif#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)#undef llabs#undef atoll#undef strtoll#undef strtoull#undef wcstoll#undef wcstoull#undef strtoll_l#undef strtoull_l#undef wcstoll_l#undef wcstoull_l#endif /* __UCLIBC_HAS_WCHAR__ *//**********************************************************************/#ifdef __UCLIBC_HAS_XLOCALE__extern unsigned long_stdlib_strto_l_l(register const char * __restrict str, char ** __restrict endptr, int base, int sflag, __locale_t locale_arg);#if defined(ULLONG_MAX)extern unsigned long long_stdlib_strto_ll_l(register const char * __restrict str, char ** __restrict endptr, int base, int sflag, __locale_t locale_arg);#endif#ifdef __UCLIBC_HAS_WCHAR__extern unsigned long_stdlib_wcsto_l_l(register const wchar_t * __restrict str, wchar_t ** __restrict endptr, int base, int sflag, __locale_t locale_arg);#if defined(ULLONG_MAX)extern unsigned long long_stdlib_wcsto_ll_l(register const wchar_t * __restrict str, wchar_t ** __restrict endptr, int base, int sflag, __locale_t locale_arg);#endif#endif /* __UCLIBC_HAS_WCHAR__ */#endif /* __UCLIBC_HAS_XLOCALE__ */extern unsigned long_stdlib_strto_l(register const char * __restrict str, char ** __restrict endptr, int base, int sflag);#if defined(ULLONG_MAX)extern unsigned long long_stdlib_strto_ll(register const char * __restrict str, char ** __restrict endptr, int base, int sflag);#endif#ifdef __UCLIBC_HAS_WCHAR__extern unsigned long_stdlib_wcsto_l(register const wchar_t * __restrict str, wchar_t ** __restrict endptr, int base, int sflag);#if defined(ULLONG_MAX)extern unsigned long long_stdlib_wcsto_ll(register const wchar_t * __restrict str, wchar_t ** __restrict endptr, int base, int sflag);#endif#endif /* __UCLIBC_HAS_WCHAR__ *//**********************************************************************/#ifdef L_atofdouble atof(const char *nptr){ return strtod(nptr, (char **) NULL);}#endif/**********************************************************************/#ifdef L_abs#if INT_MAX < LONG_MAXint abs(int j){ return (j >= 0) ? j : -j;}#endif /* INT_MAX < LONG_MAX */#endif/**********************************************************************/#ifdef L_labs#if UINT_MAX == ULONG_MAXstrong_alias(labs,abs)#endif#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)strong_alias(labs,llabs)#endif#if ULONG_MAX == UINTMAX_MAXstrong_alias(labs,imaxabs)#endiflong int labs(long int j){ return (j >= 0) ? j : -j;}#endif/**********************************************************************/#ifdef L_llabs#if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)#if (ULLONG_MAX == UINTMAX_MAX)strong_alias(llabs,imaxabs)#endiflong long int llabs(long long int j){ return (j >= 0) ? j : -j;}#endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */#endif/**********************************************************************/#ifdef L_atoi#if INT_MAX < LONG_MAX int atoi(const char *nptr){ return (int) strtol(nptr, (char **) NULL, 10);}#endif /* INT_MAX < LONG_MAX */#endif/**********************************************************************/#ifdef L_atol#if UINT_MAX == ULONG_MAXstrong_alias(atol,atoi)#endif#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)strong_alias(atol,atoll)#endiflong atol(const char *nptr){ return strtol(nptr, (char **) NULL, 10);}#endif/**********************************************************************/#ifdef L_atoll#if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)long long atoll(const char *nptr){ return strtoll(nptr, (char **) NULL, 10);}#endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */#endif/**********************************************************************/#if defined(L_strtol) || defined(L_strtol_l)#if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtol_l)strong_alias(strtol,strtoimax)#endif#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)strong_alias(__XL(strtol),__XL(strtoll))#endiflong __XL(strtol)(const char * __restrict str, char ** __restrict endptr, int base __LOCALE_PARAM ){ return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 1 __LOCALE_ARG );}__XL_ALIAS(strtol)#endif/**********************************************************************/#if defined(L_strtoll) || defined(L_strtoll_l)#if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)#if !defined(L_strtoll_l)#if (ULLONG_MAX == UINTMAX_MAX)strong_alias(strtoll,strtoimax)#endifstrong_alias(strtoll,strtoq)#endiflong long __XL(strtoll)(const char * __restrict str, char ** __restrict endptr, int base __LOCALE_PARAM ){ return (long long) __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 1 __LOCALE_ARG );}__XL_ALIAS(strtoll)#endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */#endif/**********************************************************************/#if defined(L_strtoul) || defined(L_strtoul_l)#if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtoul_l)strong_alias(strtoul,strtoumax)#endif#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)strong_alias(__XL(strtoul),__XL(strtoull))#endifunsigned long __XL(strtoul)(const char * __restrict str, char ** __restrict endptr, int base __LOCALE_PARAM ){ return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 0 __LOCALE_ARG );}__XL_ALIAS(strtoul)#endif/**********************************************************************/#if defined(L_strtoull) || defined(L_strtoull_l)#if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)#if !defined(L_strtoull_l)#if (ULLONG_MAX == UINTMAX_MAX)strong_alias(strtoull,strtoumax)#endifstrong_alias(strtoull,strtouq)#endifunsigned long long __XL(strtoull)(const char * __restrict str, char ** __restrict endptr, int base __LOCALE_PARAM ){ return __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 0 __LOCALE_ARG );}__XL_ALIAS(strtoull)#endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */#endif/**********************************************************************//* Support routines follow *//**********************************************************************//* Set if we want errno set appropriately. *//* NOTE: Implies _STRTO_ENDPTR below */#define _STRTO_ERRNO 1/* Set if we want support for the endptr arg. *//* Implied by _STRTO_ERRNO. */#define _STRTO_ENDPTR 1#if _STRTO_ERRNO#undef _STRTO_ENDPTR#define _STRTO_ENDPTR 1#define SET_ERRNO(X) __set_errno(X)#else#define SET_ERRNO(X) ((void)(X)) /* keep side effects */#endif/**********************************************************************/#if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)#define L__stdlib_strto_l#endif#if defined(L__stdlib_strto_l) || defined(L__stdlib_strto_l_l)#if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)#define _stdlib_strto_l _stdlib_wcsto_l#define _stdlib_strto_l_l _stdlib_wcsto_l_l#define Wchar wchar_t#define Wuchar __uwchar_t#ifdef __UCLIBC_DO_XLOCALE#define ISSPACE(C) iswspace_l((C), locale_arg)#else#define ISSPACE(C) iswspace((C))#endif#else /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */#define Wchar char#define Wuchar unsigned char#ifdef __UCLIBC_DO_XLOCALE#define ISSPACE(C) isspace_l((C), locale_arg)#else#define ISSPACE(C) isspace((C))#endif#endif /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */#if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)unsigned long _stdlib_strto_l(register const Wchar * __restrict str, Wchar ** __restrict endptr, int base, int sflag){ return _stdlib_strto_l_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);}#else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) *//* This is the main work fuction which handles both strtol (sflag = 1) and * strtoul (sflag = 0). */unsigned long __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str, Wchar ** __restrict endptr, int base, int sflag __LOCALE_PARAM ){ unsigned long number, cutoff;#if _STRTO_ENDPTR const Wchar *fail_char;#define SET_FAIL(X) fail_char = (X)#else#define SET_FAIL(X) ((void)(X)) /* Keep side effects. */#endif unsigned char negative, digit, cutoff_digit; assert(((unsigned int)sflag) <= 1); SET_FAIL(str); while (ISSPACE(*str)) { /* Skip leading whitespace. */ ++str; } /* Handle optional sign. */ negative = 0; switch(*str) { case '-': negative = 1; /* Fall through to increment str. */ case '+': ++str; } if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */ base += 10; /* Default is 10 (26). */ if (*str == '0') { SET_FAIL(++str); base -= 2; /* Now base is 8 or 16 (24). */ if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */ ++str; base += base; /* Base is 16 (16 or 48). */ } } if (base > 16) { /* Adjust in case base wasn't dynamic. */ base = 16; } } number = 0; if (((unsigned)(base - 2)) < 35) { /* Legal base. */ cutoff_digit = ULONG_MAX % base; cutoff = ULONG_MAX / base; do { digit = (((Wuchar)(*str - '0')) <= 9) ? (*str - '0') : ((*str >= 'A') ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */ : 40); if (digit >= base) { break; } SET_FAIL(++str); if ((number > cutoff) || ((number == cutoff) && (digit > cutoff_digit))) { number = ULONG_MAX; negative &= sflag; SET_ERRNO(ERANGE); } else { number = number * base + digit; } } while (1); }#if _STRTO_ENDPTR if (endptr) { *endptr = (Wchar *) fail_char; }#endif { unsigned long tmp = ((negative)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -