1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Further, this software is distributed without any warranty that it is
13f08c3bdfSopenharmony_ci * free of the rightful claim of any third person regarding infringement
14f08c3bdfSopenharmony_ci * or the like.  Any license provided herein, whether implied or
15f08c3bdfSopenharmony_ci * otherwise, applies only to this software file.  Patent licenses, if
16f08c3bdfSopenharmony_ci * any, provided herein do not apply to combinations of this program with
17f08c3bdfSopenharmony_ci * other software, or any other product whatsoever.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
20f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
21f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24f08c3bdfSopenharmony_ci * Mountain View, CA  94043, or:
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * http://www.sgi.com
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * For further information regarding this notice, see:
29f08c3bdfSopenharmony_ci *
30f08c3bdfSopenharmony_ci * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31f08c3bdfSopenharmony_ci */
32f08c3bdfSopenharmony_ci#include <stdio.h>
33f08c3bdfSopenharmony_ci#include <sys/param.h>
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci#include "bytes_by_prefix.h"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci/****************************************************************************
38f08c3bdfSopenharmony_ci * bytes_by_prefix(s)
39f08c3bdfSopenharmony_ci *
40f08c3bdfSopenharmony_ci * Computes the number of bytes described by string s.  s is assumed to be
41f08c3bdfSopenharmony_ci * a base 10 positive (ie. >= 0) number followed by an optional single
42f08c3bdfSopenharmony_ci * character multiplier.  The following multipliers are supported:
43f08c3bdfSopenharmony_ci *
44f08c3bdfSopenharmony_ci *              char    mult
45f08c3bdfSopenharmony_ci *              -----------------
46f08c3bdfSopenharmony_ci *              b       BSIZE  or BBSIZE
47f08c3bdfSopenharmony_ci *              k       1024 bytes
48f08c3bdfSopenharmony_ci *              K       1024 * sizeof(long)
49f08c3bdfSopenharmony_ci *              m       2^20 (1048576)
50f08c3bdfSopenharmony_ci *              M       2^20 (1048576 * sizeof(long)
51f08c3bdfSopenharmony_ci *              g       2^30 (1073741824)
52f08c3bdfSopenharmony_ci *              G       2^30 (1073741824) * sizeof(long)
53f08c3bdfSopenharmony_ci *
54f08c3bdfSopenharmony_ci * for instance, "1k" and "1024" would both cause bytes_by_prefix to return 1024
55f08c3bdfSopenharmony_ci *
56f08c3bdfSopenharmony_ci * Returns -1 if mult is an invalid character, or if the integer portion of
57f08c3bdfSopenharmony_ci * s is not a positive integer.
58f08c3bdfSopenharmony_ci *
59f08c3bdfSopenharmony_ci ****************************************************************************/
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci#ifdef DEV_BSIZE
62f08c3bdfSopenharmony_ci#define B_MULT	DEV_BSIZE	/* block size */
63f08c3bdfSopenharmony_ci#else
64f08c3bdfSopenharmony_ci#warning DEV_BSIZE is not defined, defaulting to 512
65f08c3bdfSopenharmony_ci#define B_MULT	512
66f08c3bdfSopenharmony_ci#endif
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci#define K_MULT	1024		/* Kilo or 2^10 */
69f08c3bdfSopenharmony_ci#define M_MULT	1048576		/* Mega or 2^20 */
70f08c3bdfSopenharmony_ci#define G_MULT	1073741824	/* Giga or 2^30 */
71f08c3bdfSopenharmony_ci#define T_MULT	1099511627776	/* tera or 2^40 */
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ciint bytes_by_prefix(char *s)
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	char mult, junk;
76f08c3bdfSopenharmony_ci	int nconv;
77f08c3bdfSopenharmony_ci	float num;
78f08c3bdfSopenharmony_ci	int result;
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
81f08c3bdfSopenharmony_ci	if (nconv == 0 || nconv == 3)
82f08c3bdfSopenharmony_ci		return -1;
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	if (nconv == 1) {
85f08c3bdfSopenharmony_ci		result = num;
86f08c3bdfSopenharmony_ci		return result < 0 ? -1 : result;
87f08c3bdfSopenharmony_ci	}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	switch (mult) {
90f08c3bdfSopenharmony_ci	case 'b':
91f08c3bdfSopenharmony_ci		result = (int)(num * (float)B_MULT);
92f08c3bdfSopenharmony_ci		break;
93f08c3bdfSopenharmony_ci	case 'k':
94f08c3bdfSopenharmony_ci		result = (int)(num * (float)K_MULT);
95f08c3bdfSopenharmony_ci		break;
96f08c3bdfSopenharmony_ci	case 'K':
97f08c3bdfSopenharmony_ci		result = (int)((num * (float)K_MULT) * sizeof(long));
98f08c3bdfSopenharmony_ci		break;
99f08c3bdfSopenharmony_ci	case 'm':
100f08c3bdfSopenharmony_ci		result = (int)(num * (float)M_MULT);
101f08c3bdfSopenharmony_ci		break;
102f08c3bdfSopenharmony_ci	case 'M':
103f08c3bdfSopenharmony_ci		result = (int)((num * (float)M_MULT) * sizeof(long));
104f08c3bdfSopenharmony_ci		break;
105f08c3bdfSopenharmony_ci	case 'g':
106f08c3bdfSopenharmony_ci		result = (int)(num * (float)G_MULT);
107f08c3bdfSopenharmony_ci		break;
108f08c3bdfSopenharmony_ci	case 'G':
109f08c3bdfSopenharmony_ci		result = (int)((num * (float)G_MULT) * sizeof(long));
110f08c3bdfSopenharmony_ci		break;
111f08c3bdfSopenharmony_ci	default:
112f08c3bdfSopenharmony_ci		return -1;
113f08c3bdfSopenharmony_ci	}
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	if (result < 0)
116f08c3bdfSopenharmony_ci		return -1;
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	return result;
119f08c3bdfSopenharmony_ci}
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_cilong lbytes_by_prefix(char *s)
122f08c3bdfSopenharmony_ci{
123f08c3bdfSopenharmony_ci	char mult, junk;
124f08c3bdfSopenharmony_ci	int nconv;
125f08c3bdfSopenharmony_ci	float num;
126f08c3bdfSopenharmony_ci	long result;
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_ci	nconv = sscanf(s, "%f%c%c", &num, &mult, &junk);
129f08c3bdfSopenharmony_ci	if (nconv == 0 || nconv == 3)
130f08c3bdfSopenharmony_ci		return -1;
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	if (nconv == 1) {
133f08c3bdfSopenharmony_ci		result = (long)num;
134f08c3bdfSopenharmony_ci		return result < 0 ? -1 : result;
135f08c3bdfSopenharmony_ci	}
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	switch (mult) {
138f08c3bdfSopenharmony_ci	case 'b':
139f08c3bdfSopenharmony_ci		result = (long)(num * (float)B_MULT);
140f08c3bdfSopenharmony_ci		break;
141f08c3bdfSopenharmony_ci	case 'k':
142f08c3bdfSopenharmony_ci		result = (long)(num * (float)K_MULT);
143f08c3bdfSopenharmony_ci		break;
144f08c3bdfSopenharmony_ci	case 'K':
145f08c3bdfSopenharmony_ci		result = (long)((num * (float)K_MULT) * sizeof(long));
146f08c3bdfSopenharmony_ci		break;
147f08c3bdfSopenharmony_ci	case 'm':
148f08c3bdfSopenharmony_ci		result = (long)(num * (float)M_MULT);
149f08c3bdfSopenharmony_ci		break;
150f08c3bdfSopenharmony_ci	case 'M':
151f08c3bdfSopenharmony_ci		result = (long)((num * (float)M_MULT) * sizeof(long));
152f08c3bdfSopenharmony_ci		break;
153f08c3bdfSopenharmony_ci	case 'g':
154f08c3bdfSopenharmony_ci		result = (long)(num * (float)G_MULT);
155f08c3bdfSopenharmony_ci		break;
156f08c3bdfSopenharmony_ci	case 'G':
157f08c3bdfSopenharmony_ci		result = (long)((num * (float)G_MULT) * sizeof(long));
158f08c3bdfSopenharmony_ci		break;
159f08c3bdfSopenharmony_ci	default:
160f08c3bdfSopenharmony_ci		return -1;
161f08c3bdfSopenharmony_ci	}
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	if (result < 0)
164f08c3bdfSopenharmony_ci		return -1;
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci	return result;
167f08c3bdfSopenharmony_ci}
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_ci/*
170f08c3bdfSopenharmony_ci * Force 64 bits number when compiled as 32 IRIX binary.
171f08c3bdfSopenharmony_ci * This allows for a number bigger than 2G.
172f08c3bdfSopenharmony_ci */
173f08c3bdfSopenharmony_cilong long llbytes_by_prefix(char *s)
174f08c3bdfSopenharmony_ci{
175f08c3bdfSopenharmony_ci	char mult, junk;
176f08c3bdfSopenharmony_ci	int nconv;
177f08c3bdfSopenharmony_ci	double num;
178f08c3bdfSopenharmony_ci	long long result;
179f08c3bdfSopenharmony_ci
180f08c3bdfSopenharmony_ci	nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk);
181f08c3bdfSopenharmony_ci	if (nconv == 0 || nconv == 3)
182f08c3bdfSopenharmony_ci		return -1;
183f08c3bdfSopenharmony_ci	if (nconv == 1) {
184f08c3bdfSopenharmony_ci		result = (long long)num;
185f08c3bdfSopenharmony_ci		return result < 0 ? -1 : result;
186f08c3bdfSopenharmony_ci	}
187f08c3bdfSopenharmony_ci
188f08c3bdfSopenharmony_ci	switch (mult) {
189f08c3bdfSopenharmony_ci	case 'b':
190f08c3bdfSopenharmony_ci		result = (long long)(num * (float)B_MULT);
191f08c3bdfSopenharmony_ci		break;
192f08c3bdfSopenharmony_ci	case 'k':
193f08c3bdfSopenharmony_ci		result = (long long)(num * (float)K_MULT);
194f08c3bdfSopenharmony_ci		break;
195f08c3bdfSopenharmony_ci	case 'K':
196f08c3bdfSopenharmony_ci		result = (long long)((num * (float)K_MULT) * sizeof(long long));
197f08c3bdfSopenharmony_ci		break;
198f08c3bdfSopenharmony_ci	case 'm':
199f08c3bdfSopenharmony_ci		result = (long long)(num * (float)M_MULT);
200f08c3bdfSopenharmony_ci		break;
201f08c3bdfSopenharmony_ci	case 'M':
202f08c3bdfSopenharmony_ci		result = (long long)((num * (float)M_MULT) * sizeof(long long));
203f08c3bdfSopenharmony_ci		break;
204f08c3bdfSopenharmony_ci	case 'g':
205f08c3bdfSopenharmony_ci		result = (long long)(num * (float)G_MULT);
206f08c3bdfSopenharmony_ci		break;
207f08c3bdfSopenharmony_ci	case 'G':
208f08c3bdfSopenharmony_ci		result = (long long)((num * (float)G_MULT) * sizeof(long long));
209f08c3bdfSopenharmony_ci		break;
210f08c3bdfSopenharmony_ci	default:
211f08c3bdfSopenharmony_ci		return -1;
212f08c3bdfSopenharmony_ci	}
213f08c3bdfSopenharmony_ci
214f08c3bdfSopenharmony_ci	if (result < 0)
215f08c3bdfSopenharmony_ci		return -1;
216f08c3bdfSopenharmony_ci
217f08c3bdfSopenharmony_ci	return result;
218f08c3bdfSopenharmony_ci}
219