OSDN Git Service

Importing Egor's testsuite.
[pf3gnuchains/pf3gnuchains3x.git] / winsup / testsuite / libltp / 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 #elif __CYGWIN__
67 #include <sys/stat.h>
68 #define B_MULT S_BLKSIZE        /* block size */
69 #endif
70
71
72 #define K_MULT  1024            /* Kilo or 2^10 */
73 #define M_MULT  1048576         /* Mega or 2^20 */
74 #define G_MULT  1073741824      /* Giga or 2^30 */
75 #define T_MULT  1099511627776   /* tera or 2^40 */
76
77 int
78 str_to_bytes(s)
79 char    *s;
80 {
81     char    mult, junk;
82     int     nconv;
83     float   num;
84
85     nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
86     if (nconv == 0 || nconv == 3 )
87         return -1;
88
89     if (nconv == 1)
90         return num;
91
92     switch (mult) {
93     case 'b':
94                 return (int)(num * (float)B_MULT);
95     case 'k':
96                 return (int)(num * (float)K_MULT);
97     case 'K':
98                 return (int)((num * (float)K_MULT) * sizeof(long));
99     case 'm':
100                 return (int)(num * (float)M_MULT);
101     case 'M':
102                 return (int)((num * (float)M_MULT) * sizeof(long));
103     case 'g':
104                 return (int)(num * (float)G_MULT);
105     case 'G':   
106                 return (int)((num * (float)G_MULT) * sizeof(long));
107     default:
108         return -1;
109     }
110 }
111
112 long
113 str_to_lbytes(s)
114 char    *s;
115 {
116     char    mult, junk;
117     long    nconv;
118     float   num;
119
120     nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
121     if (nconv == 0 || nconv == 3 )
122         return -1;
123
124     if (nconv == 1)
125         return (long)num;
126
127     switch (mult) {
128     case 'b':
129                 return (long)(num * (float)B_MULT);
130     case 'k':
131                 return (long)(num * (float)K_MULT);
132     case 'K':
133                 return (long)((num * (float)K_MULT) * sizeof(long));
134     case 'm':
135                 return (long)(num * (float)M_MULT);
136     case 'M':
137                 return (long)((num * (float)M_MULT) * sizeof(long));
138     case 'g':
139                 return (long)(num * (float)G_MULT);
140     case 'G':   
141                 return (long)((num * (float)G_MULT) * sizeof(long));
142     default:
143         return -1;
144     }
145 }
146
147 /*
148  * Force 64 bits number when compiled as 32 IRIX binary.
149  * This allows for a number bigger than 2G.
150  */
151
152 long long
153 str_to_llbytes(s)
154 char    *s;
155 {
156     char    mult, junk;
157     long    nconv;
158     double  num;
159
160     nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk);
161     if (nconv == 0 || nconv == 3 )
162         return -1;
163
164     if (nconv == 1)
165         return (long long)num;
166
167     switch (mult) {
168     case 'b':
169                 return (long long)(num * (float)B_MULT);
170     case 'k':
171                 return (long long)(num * (float)K_MULT);
172     case 'K':
173                 return (long long)((num * (float)K_MULT) * sizeof(long long));
174     case 'm':
175                 return (long long)(num * (float)M_MULT);
176     case 'M':
177                 return (long long)((num * (float)M_MULT) * sizeof(long long));
178     case 'g':
179                 return (long long)(num * (float)G_MULT);
180     case 'G':   
181                 return (long long)((num * (float)G_MULT) * sizeof(long long));
182     default:
183         return -1;
184     }
185 }
186
187 #ifdef UNIT_TEST
188
189 main(int argc, char **argv)
190 {
191     int ind;
192
193     if (argc == 1 ) {
194         fprintf(stderr, "missing str_to_bytes() parameteres\n");
195         exit(1);
196     }
197    
198     for (ind=1; ind<argc; ind++) {
199
200         printf("str_to_bytes(%s) returned %d\n", 
201             argv[ind], str_to_bytes(argv[ind]));
202
203         printf("str_to_lbytes(%s) returned %ld\n", 
204             argv[ind], str_to_lbytes(argv[ind]));
205
206         printf("str_to_llbytes(%s) returned %lld\n", 
207             argv[ind], str_to_llbytes(argv[ind]));
208     }
209 }
210
211 #endif