]> git.cameronkatri.com Git - bsd-progress.git/blob - strsuftoll.c
progress: Port for Linux and Darwin
[bsd-progress.git] / strsuftoll.c
1 /* $NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $ */
2 /*-
3 * Copyright (c) 2001-2002,2004 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 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30 /*-
31 * Copyright (c) 1991, 1993, 1994
32 * The Regents of the University of California. All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley by
35 * Keith Muller of the University of California, San Diego and Lance
36 * Visser of Convex Computer Corporation.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63 #include <sys/cdefs.h>
64
65 #if defined(LIBC_SCCS) && !defined(lint)
66 __RCSID("$NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $");
67 #endif /* LIBC_SCCS and not lint */
68
69 #include <sys/types.h>
70 #include <sys/time.h>
71
72 #include <assert.h>
73 #include <ctype.h>
74 #include <err.h>
75 #include <errno.h>
76 #include <limits.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80
81 /* LONGLONG */
82 long long strsuftoll(const char *, const char *, long long, long long);
83 /* LONGLONG */
84 long long strsuftollx(const char *, const char *, long long, long long,
85 char *, size_t);
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 /* LONGLONG */
104 long long
105 strsuftoll(const char *desc, const char *val,
106 long long min, long long max)
107 {
108 long long result;
109 char errbuf[100];
110
111 result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
112 if (*errbuf != '\0')
113 errx(EXIT_FAILURE, "%s", errbuf);
114 return result;
115 }
116
117 /*
118 * As strsuftoll(), but returns the error message into the provided buffer
119 * rather than exiting with it.
120 */
121 /* LONGLONG */
122 static long long
123 __strsuftollx(const char *desc, const char *val,
124 long long min, long long max, char *ebuf, size_t ebuflen, size_t depth)
125 {
126 long long num, t;
127 char *expr;
128
129 (void)(desc != NULL);
130 (void)(val != NULL);
131 (void)(ebuf != NULL);
132
133 if (depth > 16) {
134 snprintf(ebuf, ebuflen, "%s: Recursion limit exceeded", desc);
135 return 0;
136 }
137
138 while (isspace((unsigned char)*val)) /* Skip leading space */
139 val++;
140
141 errno = 0;
142 num = strtoll(val, &expr, 10);
143 if (errno == ERANGE)
144 goto erange; /* Overflow */
145
146 if (expr == val) /* No digits */
147 goto badnum;
148
149 switch (*expr) {
150 case 'b':
151 t = num;
152 num *= 512; /* 1 block */
153 if (t > num)
154 goto erange;
155 ++expr;
156 break;
157 case 'k':
158 t = num;
159 num *= 1024; /* 1 kibibyte */
160 if (t > num)
161 goto erange;
162 ++expr;
163 break;
164 case 'm':
165 t = num;
166 num *= 1048576; /* 1 mebibyte */
167 if (t > num)
168 goto erange;
169 ++expr;
170 break;
171 case 'g':
172 t = num;
173 num *= 1073741824; /* 1 gibibyte */
174 if (t > num)
175 goto erange;
176 ++expr;
177 break;
178 case 't':
179 t = num;
180 num *= 1099511627776LL; /* 1 tebibyte */
181 if (t > num)
182 goto erange;
183 ++expr;
184 break;
185 case 'w':
186 t = num;
187 num *= sizeof(int); /* 1 word */
188 if (t > num)
189 goto erange;
190 ++expr;
191 break;
192 }
193
194 switch (*expr) {
195 case '\0':
196 break;
197 case '*': /* Backward compatible */
198 case 'x':
199 t = num;
200 num *= __strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen,
201 depth + 1);
202 if (*ebuf != '\0')
203 return 0;
204 if (t > num) {
205 erange:
206 errno = ERANGE;
207 snprintf(ebuf, ebuflen, "%s: %s", desc, strerror(errno));
208 return 0;
209 }
210 break;
211 default:
212 badnum:
213 snprintf(ebuf, ebuflen, "%s `%s': illegal number", desc, val);
214 return 0;
215 }
216 if (num < min) {
217 /* LONGLONG */
218 snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
219 desc, (long long)num, (long long)min);
220 return 0;
221 }
222 if (num > max) {
223 /* LONGLONG */
224 snprintf(ebuf, ebuflen, "%s %lld is greater than %lld.",
225 desc, (long long)num, (long long)max);
226 return 0;
227 }
228 *ebuf = '\0';
229 return num;
230 }
231
232 long long
233 strsuftollx(const char *desc, const char *val,
234 long long min, long long max, char *ebuf, size_t ebuflen)
235 {
236 return __strsuftollx(desc, val, min, max, ebuf, ebuflen, 0);
237 }