2 * Copyright (c) 2000 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/param.h>
20 #include "str_to_bytes.h"
22 /****************************************************************************
25 * Computes the number of bytes described by string s. s is assumed to be
26 * a base 10 positive (ie. >= 0) number followed by an optional single
27 * character multiplier. The following multipliers are supported:
33 * K 1024 * sizeof(long)
35 * M 2^20 (1048576 * sizeof(long)
37 * G 2^30 (1073741824) * sizeof(long)
39 * for instance, "1k" and "1024" would both cause str_to_bytes to return 1024.
41 * Returns -1 if mult is an invalid character, or if the integer portion of
42 * s is not a positive integer.
44 ****************************************************************************/
47 #define B_MULT DEV_BSIZE /* block size */
51 #define K_MULT 1024 /* Kilo or 2^10 */
52 #define M_MULT 1048576 /* Mega or 2^20 */
53 #define G_MULT 1073741824 /* Giga or 2^30 */
54 #define T_MULT 1099511627776 /* tera or 2^40 */
64 nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
65 if (nconv == 0 || nconv == 3 )
73 return (int)(num * (float)B_MULT);
75 return (int)(num * (float)K_MULT);
77 return (int)((num * (float)K_MULT) * sizeof(long));
79 return (int)(num * (float)M_MULT);
81 return (int)((num * (float)M_MULT) * sizeof(long));
83 return (int)(num * (float)G_MULT);
85 return (int)((num * (float)G_MULT) * sizeof(long));
99 nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
100 if (nconv == 0 || nconv == 3 )
108 return (long)(num * (float)B_MULT);
110 return (long)(num * (float)K_MULT);
112 return (long)((num * (float)K_MULT) * sizeof(long));
114 return (long)(num * (float)M_MULT);
116 return (long)((num * (float)M_MULT) * sizeof(long));
118 return (long)(num * (float)G_MULT);
120 return (long)((num * (float)G_MULT) * sizeof(long));
127 * Force 64 bits number when compiled as a 32-bit binary.
128 * This allows for a number bigger than 2G.
139 nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk);
140 if (nconv == 0 || nconv == 3 )
144 return (long long)num;
148 return (long long)(num * (float)B_MULT);
150 return (long long)(num * (float)K_MULT);
152 return (long long)((num * (float)K_MULT) * sizeof(long long));
154 return (long long)(num * (float)M_MULT);
156 return (long long)((num * (float)M_MULT) * sizeof(long long));
158 return (long long)(num * (float)G_MULT);
160 return (long long)((num * (float)G_MULT) * sizeof(long long));
168 main(int argc, char **argv)
173 fprintf(stderr, "missing str_to_bytes() parameteres\n");
177 for (ind=1; ind<argc; ind++) {
179 printf("str_to_bytes(%s) returned %d\n",
180 argv[ind], str_to_bytes(argv[ind]));
182 printf("str_to_lbytes(%s) returned %ld\n",
183 argv[ind], str_to_lbytes(argv[ind]));
185 printf("str_to_llbytes(%s) returned %lld\n",
186 argv[ind], str_to_llbytes(argv[ind]));