2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 #include <sys/param.h>
34 #include "str_to_bytes.h"
36 /****************************************************************************
39 * Computes the number of bytes described by string s. s is assumed to be
40 * a base 10 positive (ie. >= 0) number followed by an optional single
41 * character multiplier. The following multipliers are supported:
47 * K 1024 * sizeof(long)
49 * M 2^20 (1048576 * sizeof(long)
51 * G 2^30 (1073741824) * sizeof(long)
53 * for instance, "1k" and "1024" would both cause str_to_bytes to return 1024.
55 * Returns -1 if mult is an invalid character, or if the integer portion of
56 * s is not a positive integer.
58 ****************************************************************************/
61 #define B_MULT BSIZE /* block size */
63 #define B_MULT BBSIZE /* block size */
65 #define B_MULT DEV_BSIZE /* block size */
69 #define K_MULT 1024 /* Kilo or 2^10 */
70 #define M_MULT 1048576 /* Mega or 2^20 */
71 #define G_MULT 1073741824 /* Giga or 2^30 */
72 #define T_MULT 1099511627776 /* tera or 2^40 */
82 nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
83 if (nconv == 0 || nconv == 3 )
91 return (int)(num * (float)B_MULT);
93 return (int)(num * (float)K_MULT);
95 return (int)((num * (float)K_MULT) * sizeof(long));
97 return (int)(num * (float)M_MULT);
99 return (int)((num * (float)M_MULT) * sizeof(long));
101 return (int)(num * (float)G_MULT);
103 return (int)((num * (float)G_MULT) * sizeof(long));
117 nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
118 if (nconv == 0 || nconv == 3 )
126 return (long)(num * (float)B_MULT);
128 return (long)(num * (float)K_MULT);
130 return (long)((num * (float)K_MULT) * sizeof(long));
132 return (long)(num * (float)M_MULT);
134 return (long)((num * (float)M_MULT) * sizeof(long));
136 return (long)(num * (float)G_MULT);
138 return (long)((num * (float)G_MULT) * sizeof(long));
145 * Force 64 bits number when compiled as 32 IRIX binary.
146 * This allows for a number bigger than 2G.
157 nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk);
158 if (nconv == 0 || nconv == 3 )
162 return (long long)num;
166 return (long long)(num * (float)B_MULT);
168 return (long long)(num * (float)K_MULT);
170 return (long long)((num * (float)K_MULT) * sizeof(long long));
172 return (long long)(num * (float)M_MULT);
174 return (long long)((num * (float)M_MULT) * sizeof(long long));
176 return (long long)(num * (float)G_MULT);
178 return (long long)((num * (float)G_MULT) * sizeof(long long));
186 main(int argc, char **argv)
191 fprintf(stderr, "missing str_to_bytes() parameteres\n");
195 for (ind=1; ind<argc; ind++) {
197 printf("str_to_bytes(%s) returned %d\n",
198 argv[ind], str_to_bytes(argv[ind]));
200 printf("str_to_lbytes(%s) returned %ld\n",
201 argv[ind], str_to_lbytes(argv[ind]));
203 printf("str_to_llbytes(%s) returned %lld\n",
204 argv[ind], str_to_llbytes(argv[ind]));