blob: f73b22787865670bae2870fa5a6c78b674933c7f [file] [log] [blame]
whrb973f2b2000-05-05 19:34:50 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
whrb973f2b2000-05-05 19:34:50 +00004 * 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.
vapier45a8ba02009-07-20 10:59:32 +00007 *
whrb973f2b2000-05-05 19:34:50 +00008 * 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.
vapier45a8ba02009-07-20 10:59:32 +000011 *
whrb973f2b2000-05-05 19:34:50 +000012 * 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.
vapier45a8ba02009-07-20 10:59:32 +000018 *
whrb973f2b2000-05-05 19:34:50 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
whrb973f2b2000-05-05 19:34:50 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
whrb973f2b2000-05-05 19:34:50 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32#include <stdio.h>
33#include <sys/param.h>
Marios Makris5f017a02012-05-31 20:03:52 +030034
Marios Makris1475f672012-05-10 16:04:48 +030035#include "bytes_by_prefix.h"
whrb973f2b2000-05-05 19:34:50 +000036
37/****************************************************************************
Marios Makris1475f672012-05-10 16:04:48 +030038 * bytes_by_prefix(s)
whrb973f2b2000-05-05 19:34:50 +000039 *
40 * Computes the number of bytes described by string s. s is assumed to be
41 * a base 10 positive (ie. >= 0) number followed by an optional single
42 * character multiplier. The following multipliers are supported:
43 *
44 * char mult
45 * -----------------
46 * b BSIZE or BBSIZE
47 * k 1024 bytes
48 * K 1024 * sizeof(long)
49 * m 2^20 (1048576)
50 * M 2^20 (1048576 * sizeof(long)
51 * g 2^30 (1073741824)
52 * G 2^30 (1073741824) * sizeof(long)
53 *
Marios Makris1475f672012-05-10 16:04:48 +030054 * for instance, "1k" and "1024" would both cause bytes_by_prefix to return 1024
whrb973f2b2000-05-05 19:34:50 +000055 *
56 * Returns -1 if mult is an invalid character, or if the integer portion of
57 * s is not a positive integer.
58 *
59 ****************************************************************************/
60
61#if CRAY
62#define B_MULT BSIZE /* block size */
63#elif sgi
64#define B_MULT BBSIZE /* block size */
mridgef7298c72005-12-05 19:14:37 +000065#elif defined(__linux__) || defined(__sun) || defined(__hpux)
whrb973f2b2000-05-05 19:34:50 +000066#define B_MULT DEV_BSIZE /* block size */
mridgee6508f82005-01-04 21:00:17 +000067#elif defined(_AIX)
68#define B_MULT UBSIZE
whrb973f2b2000-05-05 19:34:50 +000069#endif
70
whrb973f2b2000-05-05 19:34:50 +000071#define K_MULT 1024 /* Kilo or 2^10 */
72#define M_MULT 1048576 /* Mega or 2^20 */
73#define G_MULT 1073741824 /* Giga or 2^30 */
74#define T_MULT 1099511627776 /* tera or 2^40 */
75
Marios Makris1475f672012-05-10 16:04:48 +030076int bytes_by_prefix(char *s)
whrb973f2b2000-05-05 19:34:50 +000077{
Marios Makris9a769aa2012-05-10 16:04:47 +030078 char mult, junk;
79 int nconv;
80 float num;
Marios Makris5f017a02012-05-31 20:03:52 +030081 int result;
whrb973f2b2000-05-05 19:34:50 +000082
Marios Makris9a769aa2012-05-10 16:04:47 +030083 nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
84 if (nconv == 0 || nconv == 3)
85 return -1;
whrb973f2b2000-05-05 19:34:50 +000086
Marios Makris5f017a02012-05-31 20:03:52 +030087 if (nconv == 1) {
88 result = num;
89 return result < 0 ? -1 : result;
90 }
whrb973f2b2000-05-05 19:34:50 +000091
Marios Makris9a769aa2012-05-10 16:04:47 +030092 switch (mult) {
93 case 'b':
Marios Makris5f017a02012-05-31 20:03:52 +030094 result = (int)(num * (float)B_MULT);
95 break;
Marios Makris9a769aa2012-05-10 16:04:47 +030096 case 'k':
Marios Makris5f017a02012-05-31 20:03:52 +030097 result = (int)(num * (float)K_MULT);
98 break;
Marios Makris9a769aa2012-05-10 16:04:47 +030099 case 'K':
Marios Makris5f017a02012-05-31 20:03:52 +0300100 result = (int)((num * (float)K_MULT) * sizeof(long));
101 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300102 case 'm':
Marios Makris5f017a02012-05-31 20:03:52 +0300103 result = (int)(num * (float)M_MULT);
104 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300105 case 'M':
Marios Makris5f017a02012-05-31 20:03:52 +0300106 result = (int)((num * (float)M_MULT) * sizeof(long));
107 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300108 case 'g':
Marios Makris5f017a02012-05-31 20:03:52 +0300109 result = (int)(num * (float)G_MULT);
110 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300111 case 'G':
Marios Makris5f017a02012-05-31 20:03:52 +0300112 result = (int)((num * (float)G_MULT) * sizeof(long));
113 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300114 default:
115 return -1;
116 }
Marios Makris5f017a02012-05-31 20:03:52 +0300117
118 if (result < 0)
119 return -1;
120
121 return result;
whrb973f2b2000-05-05 19:34:50 +0000122}
123
Marios Makris1475f672012-05-10 16:04:48 +0300124long lbytes_by_prefix(char *s)
whrb973f2b2000-05-05 19:34:50 +0000125{
Marios Makris9a769aa2012-05-10 16:04:47 +0300126 char mult, junk;
Marios Makris3d3e94e2012-05-10 16:04:49 +0300127 int nconv;
Marios Makris9a769aa2012-05-10 16:04:47 +0300128 float num;
Marios Makris5f017a02012-05-31 20:03:52 +0300129 long result;
whrb973f2b2000-05-05 19:34:50 +0000130
Marios Makris9a769aa2012-05-10 16:04:47 +0300131 nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
132 if (nconv == 0 || nconv == 3)
133 return -1;
whrb973f2b2000-05-05 19:34:50 +0000134
Marios Makris5f017a02012-05-31 20:03:52 +0300135 if (nconv == 1) {
136 result = (long)num;
137 return result < 0 ? -1 : result;
138 }
whrb973f2b2000-05-05 19:34:50 +0000139
Marios Makris9a769aa2012-05-10 16:04:47 +0300140 switch (mult) {
141 case 'b':
Marios Makris5f017a02012-05-31 20:03:52 +0300142 result = (long)(num * (float)B_MULT);
143 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300144 case 'k':
Marios Makris5f017a02012-05-31 20:03:52 +0300145 result = (long)(num * (float)K_MULT);
146 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300147 case 'K':
Marios Makris5f017a02012-05-31 20:03:52 +0300148 result = (long)((num * (float)K_MULT) * sizeof(long));
149 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300150 case 'm':
Marios Makris5f017a02012-05-31 20:03:52 +0300151 result = (long)(num * (float)M_MULT);
152 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300153 case 'M':
Marios Makris5f017a02012-05-31 20:03:52 +0300154 result = (long)((num * (float)M_MULT) * sizeof(long));
155 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300156 case 'g':
Marios Makris5f017a02012-05-31 20:03:52 +0300157 result = (long)(num * (float)G_MULT);
158 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300159 case 'G':
Marios Makris5f017a02012-05-31 20:03:52 +0300160 result = (long)((num * (float)G_MULT) * sizeof(long));
161 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300162 default:
163 return -1;
164 }
Marios Makris5f017a02012-05-31 20:03:52 +0300165
166 if (result < 0)
167 return -1;
168
169 return result;
whrb973f2b2000-05-05 19:34:50 +0000170}
171
172/*
173 * Force 64 bits number when compiled as 32 IRIX binary.
174 * This allows for a number bigger than 2G.
175 */
Marios Makris1475f672012-05-10 16:04:48 +0300176long long llbytes_by_prefix(char *s)
whrb973f2b2000-05-05 19:34:50 +0000177{
Marios Makris9a769aa2012-05-10 16:04:47 +0300178 char mult, junk;
Marios Makris3d3e94e2012-05-10 16:04:49 +0300179 int nconv;
Marios Makris9a769aa2012-05-10 16:04:47 +0300180 double num;
Marios Makris5f017a02012-05-31 20:03:52 +0300181 long long result;
whrb973f2b2000-05-05 19:34:50 +0000182
Marios Makris9a769aa2012-05-10 16:04:47 +0300183 nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk);
184 if (nconv == 0 || nconv == 3)
185 return -1;
Marios Makris5f017a02012-05-31 20:03:52 +0300186 if (nconv == 1) {
187 result = (long long)num;
188 return result < 0 ? -1 : result;
189 }
whrb973f2b2000-05-05 19:34:50 +0000190
Marios Makris9a769aa2012-05-10 16:04:47 +0300191 switch (mult) {
192 case 'b':
Marios Makris5f017a02012-05-31 20:03:52 +0300193 result = (long long)(num * (float)B_MULT);
194 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300195 case 'k':
Marios Makris5f017a02012-05-31 20:03:52 +0300196 result = (long long)(num * (float)K_MULT);
197 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300198 case 'K':
Marios Makris5f017a02012-05-31 20:03:52 +0300199 result = (long long)((num * (float)K_MULT) * sizeof(long long));
200 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300201 case 'm':
Marios Makris5f017a02012-05-31 20:03:52 +0300202 result = (long long)(num * (float)M_MULT);
203 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300204 case 'M':
Marios Makris5f017a02012-05-31 20:03:52 +0300205 result = (long long)((num * (float)M_MULT) * sizeof(long long));
206 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300207 case 'g':
Marios Makris5f017a02012-05-31 20:03:52 +0300208 result = (long long)(num * (float)G_MULT);
209 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300210 case 'G':
Marios Makris5f017a02012-05-31 20:03:52 +0300211 result = (long long)((num * (float)G_MULT) * sizeof(long long));
212 break;
Marios Makris9a769aa2012-05-10 16:04:47 +0300213 default:
214 return -1;
215 }
Marios Makris5f017a02012-05-31 20:03:52 +0300216
217 if (result < 0)
218 return -1;
219
220 return result;
whrb973f2b2000-05-05 19:34:50 +0000221}