1 1.6 joerg /* $NetBSD: _wcstol.h,v 1.6 2013/05/17 12:55:57 joerg Exp $ */ 2 1.1 tshiozak 3 1.1 tshiozak /*- 4 1.1 tshiozak * Copyright (c) 1990, 1993 5 1.1 tshiozak * The Regents of the University of California. All rights reserved. 6 1.1 tshiozak * 7 1.1 tshiozak * Redistribution and use in source and binary forms, with or without 8 1.1 tshiozak * modification, are permitted provided that the following conditions 9 1.1 tshiozak * are met: 10 1.1 tshiozak * 1. Redistributions of source code must retain the above copyright 11 1.1 tshiozak * notice, this list of conditions and the following disclaimer. 12 1.1 tshiozak * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 tshiozak * notice, this list of conditions and the following disclaimer in the 14 1.1 tshiozak * documentation and/or other materials provided with the distribution. 15 1.2 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 tshiozak * may be used to endorse or promote products derived from this software 17 1.1 tshiozak * without specific prior written permission. 18 1.1 tshiozak * 19 1.1 tshiozak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 tshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 tshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 tshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 tshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 tshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 tshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 tshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 tshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 tshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 tshiozak * SUCH DAMAGE. 30 1.1 tshiozak * 31 1.1 tshiozak * Original version ID: 32 1.1 tshiozak * @(#)strtol.c 8.1 (Berkeley) 6/4/93 33 1.1 tshiozak * NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp 34 1.1 tshiozak * Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstol.c,v 1.2 2001/09/21 16:11:41 yamt Exp 35 1.1 tshiozak */ 36 1.1 tshiozak 37 1.1 tshiozak /* 38 1.1 tshiozak * function template for wcstol, wcstoll and wcstoimax. 39 1.1 tshiozak * 40 1.1 tshiozak * parameters: 41 1.1 tshiozak * _FUNCNAME : function name 42 1.1 tshiozak * __INT : return type 43 1.1 tshiozak * __INT_MIN : lower limit of the return type 44 1.1 tshiozak * __INT_MAX : upper limit of the return type 45 1.1 tshiozak */ 46 1.1 tshiozak 47 1.5 joerg #include <locale.h> 48 1.5 joerg #include "setlocale_local.h" 49 1.5 joerg #define INT_FUNCNAME_(pre, name, post) pre ## name ## post 50 1.5 joerg #define INT_FUNCNAME(pre, name, post) INT_FUNCNAME_(pre, name, post) 51 1.5 joerg 52 1.5 joerg static __INT 53 1.5 joerg INT_FUNCNAME(_int_, _FUNCNAME, _l)(const wchar_t *nptr, wchar_t **endptr, 54 1.5 joerg int base, locale_t loc) 55 1.1 tshiozak { 56 1.1 tshiozak const wchar_t *s; 57 1.1 tshiozak __INT acc, cutoff; 58 1.1 tshiozak wint_t wc; 59 1.1 tshiozak int i; 60 1.1 tshiozak int neg, any, cutlim; 61 1.1 tshiozak 62 1.1 tshiozak _DIAGASSERT(nptr != NULL); 63 1.1 tshiozak /* endptr may be NULL */ 64 1.1 tshiozak 65 1.1 tshiozak #ifdef __GNUC__ 66 1.1 tshiozak (void)&acc; (void)&cutoff; 67 1.1 tshiozak #endif 68 1.1 tshiozak 69 1.1 tshiozak /* check base value */ 70 1.1 tshiozak if (base && (base < 2 || base > 36)) { 71 1.1 tshiozak errno = EINVAL; 72 1.1 tshiozak return 0; 73 1.1 tshiozak } 74 1.1 tshiozak 75 1.1 tshiozak /* 76 1.1 tshiozak * Skip white space and pick up leading +/- sign if any. 77 1.1 tshiozak * If base is 0, allow 0x for hex and 0 for octal, else 78 1.1 tshiozak * assume decimal; if base is already 16, allow 0x. 79 1.1 tshiozak */ 80 1.1 tshiozak s = nptr; 81 1.1 tshiozak do { 82 1.1 tshiozak wc = (wchar_t) *s++; 83 1.5 joerg } while (iswspace_l(wc, loc)); 84 1.1 tshiozak if (wc == L'-') { 85 1.1 tshiozak neg = 1; 86 1.1 tshiozak wc = *s++; 87 1.1 tshiozak } else { 88 1.1 tshiozak neg = 0; 89 1.1 tshiozak if (wc == L'+') 90 1.1 tshiozak wc = *s++; 91 1.1 tshiozak } 92 1.1 tshiozak if ((base == 0 || base == 16) && 93 1.1 tshiozak wc == L'0' && (*s == L'x' || *s == L'X')) { 94 1.1 tshiozak wc = s[1]; 95 1.1 tshiozak s += 2; 96 1.1 tshiozak base = 16; 97 1.1 tshiozak } 98 1.1 tshiozak if (base == 0) 99 1.1 tshiozak base = wc == L'0' ? 8 : 10; 100 1.1 tshiozak 101 1.1 tshiozak /* 102 1.1 tshiozak * See strtol for comments as to the logic used. 103 1.1 tshiozak */ 104 1.1 tshiozak cutoff = neg ? __INT_MIN : __INT_MAX; 105 1.1 tshiozak cutlim = (int)(cutoff % base); 106 1.1 tshiozak cutoff /= base; 107 1.1 tshiozak if (neg) { 108 1.1 tshiozak if (cutlim > 0) { 109 1.1 tshiozak cutlim -= base; 110 1.1 tshiozak cutoff += 1; 111 1.1 tshiozak } 112 1.1 tshiozak cutlim = -cutlim; 113 1.1 tshiozak } 114 1.1 tshiozak for (acc = 0, any = 0;; wc = (wchar_t) *s++) { 115 1.1 tshiozak i = __wctoint(wc); 116 1.1 tshiozak if (i == -1) 117 1.1 tshiozak break; 118 1.1 tshiozak if (i >= base) 119 1.1 tshiozak break; 120 1.1 tshiozak if (any < 0) 121 1.1 tshiozak continue; 122 1.1 tshiozak if (neg) { 123 1.1 tshiozak if (acc < cutoff || (acc == cutoff && i > cutlim)) { 124 1.1 tshiozak any = -1; 125 1.1 tshiozak acc = __INT_MIN; 126 1.1 tshiozak errno = ERANGE; 127 1.1 tshiozak } else { 128 1.1 tshiozak any = 1; 129 1.1 tshiozak acc *= base; 130 1.1 tshiozak acc -= i; 131 1.1 tshiozak } 132 1.1 tshiozak } else { 133 1.1 tshiozak if (acc > cutoff || (acc == cutoff && i > cutlim)) { 134 1.1 tshiozak any = -1; 135 1.1 tshiozak acc = __INT_MAX; 136 1.1 tshiozak errno = ERANGE; 137 1.1 tshiozak } else { 138 1.1 tshiozak any = 1; 139 1.1 tshiozak acc *= base; 140 1.1 tshiozak acc += i; 141 1.1 tshiozak } 142 1.1 tshiozak } 143 1.1 tshiozak } 144 1.1 tshiozak if (endptr != 0) 145 1.3 christos *endptr = __UNCONST(any ? s - 1 : nptr); 146 1.1 tshiozak return (acc); 147 1.1 tshiozak } 148 1.5 joerg 149 1.5 joerg __INT 150 1.5 joerg _FUNCNAME(const wchar_t *nptr, wchar_t **endptr, int base) 151 1.5 joerg { 152 1.5 joerg return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, 153 1.6 joerg _current_locale()); 154 1.5 joerg } 155 1.5 joerg 156 1.5 joerg __INT 157 1.5 joerg INT_FUNCNAME(, _FUNCNAME, _l)(const wchar_t *nptr, wchar_t **endptr, 158 1.5 joerg int base, locale_t loc) 159 1.5 joerg { 160 1.5 joerg return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc); 161 1.5 joerg } 162