xref: /freebsd/lib/libnetbsd/strsuftoll.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*	$NetBSD: strsuftoll.c,v 1.6 2004/03/05 05:58:29 lukem Exp $	*/
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause
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/types.h>
66 #include <sys/time.h>
67 
68 #include <assert.h>
69 #include <ctype.h>
70 #include <err.h>
71 #include <errno.h>
72 #include <limits.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 
77 #ifdef _LIBC
78 # ifdef __weak_alias
79 __weak_alias(strsuftoll, _strsuftoll)
80 __weak_alias(strsuftollx, _strsuftollx)
81 # endif
82 #endif /* LIBC */
83 
84 /*
85  * Convert an expression of the following forms to a (u)int64_t.
86  * 	1) A positive decimal number.
87  *	2) A positive decimal number followed by a b (mult by 512).
88  *	3) A positive decimal number followed by a k (mult by 1024).
89  *	4) A positive decimal number followed by a m (mult by 1048576).
90  *	5) A positive decimal number followed by a g (mult by 1073741824).
91  *	6) A positive decimal number followed by a t (mult by 1099511627776).
92  *	7) A positive decimal number followed by a w (mult by sizeof int)
93  *	8) Two or more positive decimal numbers (with/without k,b or w).
94  *	   separated by x (also * for backwards compatibility), specifying
95  *	   the product of the indicated values.
96  * Returns the result upon successful conversion, or exits with an
97  * appropriate error.
98  *
99  */
100 
101 /*
102  * As strsuftoll(), but returns the error message into the provided buffer
103  * rather than exiting with it.
104  */
105 /* LONGLONG */
106 long long
107 strsuftollx(const char *desc, const char *val,
108     long long min, long long max, char *ebuf, size_t ebuflen)
109 {
110 	long long num, t;
111 	char	*expr;
112 
113 	errno = 0;
114 	ebuf[0] = '\0';
115 
116 	while (isspace((unsigned char)*val))	/* Skip leading space */
117 		val++;
118 
119 	num = strtoll(val, &expr, 10);
120 	if (errno == ERANGE)
121 		goto erange;			/* Overflow */
122 
123 	if (expr == val)			/* No digits */
124 		goto badnum;
125 
126 	switch (*expr) {
127 	case 'b':
128 		t = num;
129 		num *= 512;			/* 1 block */
130 		if (t > num)
131 			goto erange;
132 		++expr;
133 		break;
134 	case 'k':
135 		t = num;
136 		num *= 1024;			/* 1 kilobyte */
137 		if (t > num)
138 			goto erange;
139 		++expr;
140 		break;
141 	case 'm':
142 		t = num;
143 		num *= 1048576;			/* 1 megabyte */
144 		if (t > num)
145 			goto erange;
146 		++expr;
147 		break;
148 	case 'g':
149 		t = num;
150 		num *= 1073741824;		/* 1 gigabyte */
151 		if (t > num)
152 			goto erange;
153 		++expr;
154 		break;
155 	case 't':
156 		t = num;
157 		num *= 1099511627776LL;		/* 1 terabyte */
158 		if (t > num)
159 			goto erange;
160 		++expr;
161 		break;
162 	case 'w':
163 		t = num;
164 		num *= sizeof(int);		/* 1 word */
165 		if (t > num)
166 			goto erange;
167 		++expr;
168 		break;
169 	}
170 
171 	switch (*expr) {
172 	case '\0':
173 		break;
174 	case '*':				/* Backward compatible */
175 	case 'x':
176 		t = num;
177 		num *= strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen);
178 		if (*ebuf != '\0')
179 			return (0);
180 		if (t > num) {
181  erange:
182 			snprintf(ebuf, ebuflen,
183 			    "%s: %s", desc, strerror(ERANGE));
184 			return (0);
185 		}
186 		break;
187 	default:
188  badnum:	snprintf(ebuf, ebuflen,
189 		    "%s `%s': illegal number", desc, val);
190 		return (0);
191 	}
192 	if (num < min) {
193 			/* LONGLONG */
194 		snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
195 		    desc, (long long)num, (long long)min);
196 		return (0);
197 	}
198 	if (num > max) {
199 			/* LONGLONG */
200 		snprintf(ebuf, ebuflen,
201 		    "%s %lld is greater than %lld.",
202 		    desc, (long long)num, (long long)max);
203 		return (0);
204 	}
205 	*ebuf = '\0';
206 	return (num);
207 }
208 
209 /* LONGLONG */
210 long long
211 strsuftoll(const char *desc, const char *val,
212     long long min, long long max)
213 {
214 	long long result;
215 	char	errbuf[100];
216 
217 	result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
218 	if (*errbuf != '\0')
219 		errx(1, "%s", errbuf);
220 	return (result);
221 }
222