xref: /netbsd-src/lib/libc/stdlib/strsuftoll.c (revision 73704c4ce4ee2a60eb617e693ce7e9f03902613e)
1 /*	$NetBSD: strsuftoll.c,v 1.3 2003/08/07 16:43:44 agc Exp $	*/
2 /*-
3  * Copyright (c) 2001-2002 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Luke Mewburn.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Copyright (c) 1991, 1993, 1994
39  *	The Regents of the University of California.  All rights reserved.
40  *
41  * This code is derived from software contributed to Berkeley by
42  * Keith Muller of the University of California, San Diego and Lance
43  * Visser of Convex Computer Corporation.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 
72 #if defined(LIBC_SCCS) && !defined(lint)
73 __RCSID("$NetBSD: strsuftoll.c,v 1.3 2003/08/07 16:43:44 agc Exp $");
74 #endif /* LIBC_SCCS and not lint */
75 
76 #ifdef _LIBC
77 #include "namespace.h"
78 #endif
79 
80 #if HAVE_CONFIG_H
81 #include "config.h"
82 #endif
83 
84 #if !HAVE_STRSUFTOLL
85 
86 #include <sys/types.h>
87 #include <sys/time.h>
88 
89 #include <assert.h>
90 #include <ctype.h>
91 #include <err.h>
92 #include <errno.h>
93 #include <limits.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 
98 #ifdef _LIBC
99 # ifdef __weak_alias
100 __weak_alias(strsuftoll, _strsuftoll)
101 __weak_alias(strsuftollx, _strsuftollx)
102 # endif
103 #endif /* LIBC */
104 
105 /*
106  * Convert an expression of the following forms to a (u)int64_t.
107  * 	1) A positive decimal number.
108  *	2) A positive decimal number followed by a b (mult by 512).
109  *	3) A positive decimal number followed by a k (mult by 1024).
110  *	4) A positive decimal number followed by a m (mult by 1048576).
111  *	5) A positive decimal number followed by a w (mult by sizeof int)
112  *	6) Two or more positive decimal numbers (with/without k,b or w).
113  *	   separated by x (also * for backwards compatibility), specifying
114  *	   the product of the indicated values.
115  * Returns the result upon successful conversion, or exits with an
116  * appropriate error.
117  *
118  */
119 /* LONGLONG */
120 long long
121 strsuftoll(const char *desc, const char *val,
122     long long min, long long max)
123 {
124 	long long result;
125 	char	errbuf[100];
126 
127 	result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
128 	if (*errbuf != '\0')
129 		errx(1, "%s", errbuf);
130 	return (result);
131 }
132 
133 /*
134  * As strsuftoll(), but returns the error message into the provided buffer
135  * rather than exiting with it.
136  */
137 /* LONGLONG */
138 long long
139 strsuftollx(const char *desc, const char *val,
140     long long min, long long max, char *ebuf, size_t ebuflen)
141 {
142 	long long num, t;
143 	char	*expr;
144 
145 	_DIAGASSERT(desc != NULL);
146 	_DIAGASSERT(val != NULL);
147 	_DIAGASSERT(ebuf != NULL);
148 
149 	errno = 0;
150 	ebuf[0] = '\0';
151 
152 	while (isspace((unsigned char)*val))	/* Skip leading space */
153 		val++;
154 
155 	num = strtoll(val, &expr, 0);
156 	if (errno == ERANGE)
157 		goto erange;			/* Overflow */
158 
159 	if (expr == val)			/* No digits */
160 		goto badnum;
161 
162 	switch (*expr) {
163 	case 'b':
164 		t = num;
165 		num *= 512;			/* 1 block */
166 		if (t > num)
167 			goto erange;
168 		++expr;
169 		break;
170 	case 'k':
171 		t = num;
172 		num *= 1024;			/* 1 kilobyte */
173 		if (t > num)
174 			goto erange;
175 		++expr;
176 		break;
177 	case 'm':
178 		t = num;
179 		num *= 1048576;			/* 1 megabyte */
180 		if (t > num)
181 			goto erange;
182 		++expr;
183 		break;
184 	case 'g':
185 		t = num;
186 		num *= 1073741824;		/* 1 gigabyte */
187 		if (t > num)
188 			goto erange;
189 		++expr;
190 		break;
191 	case 't':
192 		t = num;
193 		num *= 1099511627776LL;		/* 1 terabyte */
194 		if (t > num)
195 			goto erange;
196 		++expr;
197 		break;
198 	case 'w':
199 		t = num;
200 		num *= sizeof(int);		/* 1 word */
201 		if (t > num)
202 			goto erange;
203 		++expr;
204 		break;
205 	}
206 
207 	switch (*expr) {
208 	case '\0':
209 		break;
210 	case '*':				/* Backward compatible */
211 	case 'x':
212 		t = num;
213 		num *= strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen);
214 		if (*ebuf != '\0')
215 			return (0);
216 		if (t > num) {
217  erange:
218 			snprintf(ebuf, ebuflen,
219 			    "%s: %s", desc, strerror(ERANGE));
220 			return (0);
221 		}
222 		break;
223 	default:
224  badnum:	snprintf(ebuf, ebuflen,
225 		    "%s `%s': illegal number", desc, val);
226 		return (0);
227 	}
228 	if (num < min) {
229 			/* LONGLONG */
230 		snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
231 		    desc, (long long)num, (long long)min);
232 		return (0);
233 	}
234 	if (num > max) {
235 			/* LONGLONG */
236 		snprintf(ebuf, ebuflen,
237 		    "%s %lld is greater than %lld.",
238 		    desc, (long long)num, (long long)min);
239 		return (0);
240 	}
241 	*ebuf = '\0';
242 	return (num);
243 }
244 
245 #endif /* !HAVE_STRSUFTOLL */
246