xfstests updates - rework build to be like other xfs packages, revive some old fs...
[xfstests-dev.git] / lib / str_to_bytes.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
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.
7  * 
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.
11  * 
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.
18  * 
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.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31  */
32 #include <stdio.h>
33 #include <sys/param.h>
34 #include "str_to_bytes.h"
35
36 /****************************************************************************
37  * str_to_bytes(s)
38  *
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:
42  *
43  *              char    mult
44  *              -----------------
45  *              b       BSIZE  or BBSIZE
46  *              k       1024 bytes
47  *              K       1024 * sizeof(long)
48  *              m       2^20 (1048576)
49  *              M       2^20 (1048576 * sizeof(long)
50  *              g       2^30 (1073741824)
51  *              G       2^30 (1073741824) * sizeof(long)
52  *
53  * for instance, "1k" and "1024" would both cause str_to_bytes to return 1024.
54  *
55  * Returns -1 if mult is an invalid character, or if the integer portion of
56  * s is not a positive integer.
57  *
58  ****************************************************************************/
59
60 #if CRAY
61 #define B_MULT  BSIZE           /* block size */
62 #elif sgi
63 #define B_MULT  BBSIZE          /* block size */
64 #elif linux
65 #define B_MULT  DEV_BSIZE       /* block size */
66 #endif
67
68
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 */
73
74 int
75 str_to_bytes(s)
76 char    *s;
77 {
78     char    mult, junk;
79     int     nconv;
80     float   num;
81
82     nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
83     if (nconv == 0 || nconv == 3 )
84         return -1;
85
86     if (nconv == 1)
87         return num;
88
89     switch (mult) {
90     case 'b':
91                 return (int)(num * (float)B_MULT);
92     case 'k':
93                 return (int)(num * (float)K_MULT);
94     case 'K':
95                 return (int)((num * (float)K_MULT) * sizeof(long));
96     case 'm':
97                 return (int)(num * (float)M_MULT);
98     case 'M':
99                 return (int)((num * (float)M_MULT) * sizeof(long));
100     case 'g':
101                 return (int)(num * (float)G_MULT);
102     case 'G':   
103                 return (int)((num * (float)G_MULT) * sizeof(long));
104     default:
105         return -1;
106     }
107 }
108
109 long
110 str_to_lbytes(s)
111 char    *s;
112 {
113     char    mult, junk;
114     long    nconv;
115     float   num;
116
117     nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
118     if (nconv == 0 || nconv == 3 )
119         return -1;
120
121     if (nconv == 1)
122         return (long)num;
123
124     switch (mult) {
125     case 'b':
126                 return (long)(num * (float)B_MULT);
127     case 'k':
128                 return (long)(num * (float)K_MULT);
129     case 'K':
130                 return (long)((num * (float)K_MULT) * sizeof(long));
131     case 'm':
132                 return (long)(num * (float)M_MULT);
133     case 'M':
134                 return (long)((num * (float)M_MULT) * sizeof(long));
135     case 'g':
136                 return (long)(num * (float)G_MULT);
137     case 'G':   
138                 return (long)((num * (float)G_MULT) * sizeof(long));
139     default:
140         return -1;
141     }
142 }
143
144 /*
145  * Force 64 bits number when compiled as 32 IRIX binary.
146  * This allows for a number bigger than 2G.
147  */
148
149 long long
150 str_to_llbytes(s)
151 char    *s;
152 {
153     char    mult, junk;
154     long    nconv;
155     double  num;
156
157     nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk);
158     if (nconv == 0 || nconv == 3 )
159         return -1;
160
161     if (nconv == 1)
162         return (long long)num;
163
164     switch (mult) {
165     case 'b':
166                 return (long long)(num * (float)B_MULT);
167     case 'k':
168                 return (long long)(num * (float)K_MULT);
169     case 'K':
170                 return (long long)((num * (float)K_MULT) * sizeof(long long));
171     case 'm':
172                 return (long long)(num * (float)M_MULT);
173     case 'M':
174                 return (long long)((num * (float)M_MULT) * sizeof(long long));
175     case 'g':
176                 return (long long)(num * (float)G_MULT);
177     case 'G':   
178                 return (long long)((num * (float)G_MULT) * sizeof(long long));
179     default:
180         return -1;
181     }
182 }
183
184 #ifdef UNIT_TEST
185
186 main(int argc, char **argv)
187 {
188     int ind;
189
190     if (argc == 1 ) {
191         fprintf(stderr, "missing str_to_bytes() parameteres\n");
192         exit(1);
193     }
194    
195     for (ind=1; ind<argc; ind++) {
196
197         printf("str_to_bytes(%s) returned %d\n", 
198             argv[ind], str_to_bytes(argv[ind]));
199
200         printf("str_to_lbytes(%s) returned %ld\n", 
201             argv[ind], str_to_lbytes(argv[ind]));
202
203         printf("str_to_llbytes(%s) returned %lld\n", 
204             argv[ind], str_to_llbytes(argv[ind]));
205     }
206 }
207
208 #endif