whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | #include <stdio.h> |
| 33 | #include <sys/param.h> |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 34 | |
Marios Makris | 1475f67 | 2012-05-10 16:04:48 +0300 | [diff] [blame] | 35 | #include "bytes_by_prefix.h" |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 36 | |
| 37 | /**************************************************************************** |
Marios Makris | 1475f67 | 2012-05-10 16:04:48 +0300 | [diff] [blame] | 38 | * bytes_by_prefix(s) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 39 | * |
| 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 Makris | 1475f67 | 2012-05-10 16:04:48 +0300 | [diff] [blame] | 54 | * for instance, "1k" and "1024" would both cause bytes_by_prefix to return 1024 |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 55 | * |
| 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 */ |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 65 | #elif defined(__linux__) || defined(__sun) || defined(__hpux) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 66 | #define B_MULT DEV_BSIZE /* block size */ |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 67 | #elif defined(_AIX) |
| 68 | #define B_MULT UBSIZE |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 71 | #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 Makris | 1475f67 | 2012-05-10 16:04:48 +0300 | [diff] [blame] | 76 | int bytes_by_prefix(char *s) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 77 | { |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 78 | char mult, junk; |
| 79 | int nconv; |
| 80 | float num; |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 81 | int result; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 82 | |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 83 | nconv = sscanf(s, "%f%c%c", &num, &mult, &junk); |
| 84 | if (nconv == 0 || nconv == 3) |
| 85 | return -1; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 86 | |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 87 | if (nconv == 1) { |
| 88 | result = num; |
| 89 | return result < 0 ? -1 : result; |
| 90 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 91 | |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 92 | switch (mult) { |
| 93 | case 'b': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 94 | result = (int)(num * (float)B_MULT); |
| 95 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 96 | case 'k': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 97 | result = (int)(num * (float)K_MULT); |
| 98 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 99 | case 'K': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 100 | result = (int)((num * (float)K_MULT) * sizeof(long)); |
| 101 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 102 | case 'm': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 103 | result = (int)(num * (float)M_MULT); |
| 104 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 105 | case 'M': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 106 | result = (int)((num * (float)M_MULT) * sizeof(long)); |
| 107 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 108 | case 'g': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 109 | result = (int)(num * (float)G_MULT); |
| 110 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 111 | case 'G': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 112 | result = (int)((num * (float)G_MULT) * sizeof(long)); |
| 113 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 114 | default: |
| 115 | return -1; |
| 116 | } |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 117 | |
| 118 | if (result < 0) |
| 119 | return -1; |
| 120 | |
| 121 | return result; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Marios Makris | 1475f67 | 2012-05-10 16:04:48 +0300 | [diff] [blame] | 124 | long lbytes_by_prefix(char *s) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 125 | { |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 126 | char mult, junk; |
Marios Makris | 3d3e94e | 2012-05-10 16:04:49 +0300 | [diff] [blame] | 127 | int nconv; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 128 | float num; |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 129 | long result; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 130 | |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 131 | nconv = sscanf(s, "%f%c%c", &num, &mult, &junk); |
| 132 | if (nconv == 0 || nconv == 3) |
| 133 | return -1; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 134 | |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 135 | if (nconv == 1) { |
| 136 | result = (long)num; |
| 137 | return result < 0 ? -1 : result; |
| 138 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 139 | |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 140 | switch (mult) { |
| 141 | case 'b': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 142 | result = (long)(num * (float)B_MULT); |
| 143 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 144 | case 'k': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 145 | result = (long)(num * (float)K_MULT); |
| 146 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 147 | case 'K': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 148 | result = (long)((num * (float)K_MULT) * sizeof(long)); |
| 149 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 150 | case 'm': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 151 | result = (long)(num * (float)M_MULT); |
| 152 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 153 | case 'M': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 154 | result = (long)((num * (float)M_MULT) * sizeof(long)); |
| 155 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 156 | case 'g': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 157 | result = (long)(num * (float)G_MULT); |
| 158 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 159 | case 'G': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 160 | result = (long)((num * (float)G_MULT) * sizeof(long)); |
| 161 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 162 | default: |
| 163 | return -1; |
| 164 | } |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 165 | |
| 166 | if (result < 0) |
| 167 | return -1; |
| 168 | |
| 169 | return result; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Force 64 bits number when compiled as 32 IRIX binary. |
| 174 | * This allows for a number bigger than 2G. |
| 175 | */ |
Marios Makris | 1475f67 | 2012-05-10 16:04:48 +0300 | [diff] [blame] | 176 | long long llbytes_by_prefix(char *s) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 177 | { |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 178 | char mult, junk; |
Marios Makris | 3d3e94e | 2012-05-10 16:04:49 +0300 | [diff] [blame] | 179 | int nconv; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 180 | double num; |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 181 | long long result; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 182 | |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 183 | nconv = sscanf(s, "%lf%c%c", &num, &mult, &junk); |
| 184 | if (nconv == 0 || nconv == 3) |
| 185 | return -1; |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 186 | if (nconv == 1) { |
| 187 | result = (long long)num; |
| 188 | return result < 0 ? -1 : result; |
| 189 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 190 | |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 191 | switch (mult) { |
| 192 | case 'b': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 193 | result = (long long)(num * (float)B_MULT); |
| 194 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 195 | case 'k': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 196 | result = (long long)(num * (float)K_MULT); |
| 197 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 198 | case 'K': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 199 | result = (long long)((num * (float)K_MULT) * sizeof(long long)); |
| 200 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 201 | case 'm': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 202 | result = (long long)(num * (float)M_MULT); |
| 203 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 204 | case 'M': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 205 | result = (long long)((num * (float)M_MULT) * sizeof(long long)); |
| 206 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 207 | case 'g': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 208 | result = (long long)(num * (float)G_MULT); |
| 209 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 210 | case 'G': |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 211 | result = (long long)((num * (float)G_MULT) * sizeof(long long)); |
| 212 | break; |
Marios Makris | 9a769aa | 2012-05-10 16:04:47 +0300 | [diff] [blame] | 213 | default: |
| 214 | return -1; |
| 215 | } |
Marios Makris | 5f017a0 | 2012-05-31 20:03:52 +0300 | [diff] [blame] | 216 | |
| 217 | if (result < 0) |
| 218 | return -1; |
| 219 | |
| 220 | return result; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 221 | } |