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