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