/
bcal.c
2301 lines (1853 loc) · 43.5 KB
/
bcal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Byte CALculator
*
* Author: Arun Prakash Jana <engineerarun@gmail.com>
* Copyright (C) 2016 by Arun Prakash Jana <engineerarun@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bcal. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <readline/history.h>
#include <readline/readline.h>
#include "dslib.h"
#include "log.h"
#define TRUE 1
#define FALSE !TRUE
#define SECTOR_SIZE 512 /* 0x200 */
#define MAX_HEAD 16 /* 0x10 */
#define MAX_SECTOR 63 /* 0x3f */
#define UINT_BUF_LEN 40 /* log10(1 << 128) + '\0' */
#define FLOAT_BUF_LEN 128
#define FLOAT_WIDTH 40
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define MAX_BITS 128
#define ALIGNMENT_MASK_4BIT 0xF
#define ELEMENTS(x) (sizeof(x) / sizeof(*(x)))
typedef unsigned char bool;
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long double maxfloat_t;
#ifdef __SIZEOF_INT128__
typedef __uint128_t maxuint_t;
#else
typedef __uint64_t maxuint_t;
#endif
/* CHS representation */
typedef struct {
ulong c;
ulong h;
ulong s;
} t_chs;
/* Settings */
typedef struct {
uchar bcmode : 1;
uchar minimal : 1;
uchar repl : 1;
uchar calc : 1;
uchar rsvd : 2; /* Reserved for future usage */
uchar loglvl : 2;
} settings;
static char *VERSION = "2.4";
static char *units[] = {"b", "kib", "mib", "gib", "tib", "kb", "mb", "gb", "tb"};
static char *logarr[] = {"ERROR", "WARNING", "INFO", "DEBUG"};
static char *FAILED = "1";
static char *PASSED = "\0";
static char *curexpr = NULL;
static char prompt[8] = "bcal> ";
static char uint_buf[UINT_BUF_LEN];
static char float_buf[FLOAT_BUF_LEN];
static Data lastres = {"\0", 0};
static settings cfg = {0, 0, 0, 0, 0, INFO};
static const char* const error_strings[] = {
"is undefined",
"Missing operator"
};
static void debug_log(const char *func, int level, const char *format, ...)
{
va_list ap;
if (level < 0 || level > DEBUG)
return;
va_start(ap, format);
if (level <= cfg.loglvl) {
if (cfg.loglvl == DEBUG) {
fprintf(stderr, "%s(), %s: ", func, logarr[level]);
vfprintf(stderr, format, ap);
} else {
fprintf(stderr, "%s: ", logarr[level]);
vfprintf(stderr, format, ap);
}
}
va_end(ap);
}
/*
* Just a safe strncpy(3)
* Always null ('\0') terminates if both src and dest are valid pointers.
* Returns the number of bytes copied including terminating null byte.
*/
static size_t bstrlcpy(char *dest, const char *src, size_t n)
{
static ulong *s, *d;
static size_t len, blocks;
static const uint lsize = sizeof(ulong);
static const uint WORD_SHIFT = (sizeof(ulong) == 8) ? 3 : 2;
if (!src || !dest || !n)
return 0;
len = strlen(src) + 1;
if (n > len)
n = len;
else if (len > n)
/* Save total number of bytes to copy in len */
len = n;
/*
* To enable -O3 ensure src and dest are 16-byte aligned
* More info: http://www.felixcloutier.com/x86/MOVDQA.html
*/
if ((n >= lsize) && (((ulong)src & ALIGNMENT_MASK_4BIT) == 0
&& ((ulong)dest & ALIGNMENT_MASK_4BIT) == 0)) {
s = (ulong *)src;
d = (ulong *)dest;
blocks = n >> WORD_SHIFT;
n &= lsize - 1;
while (blocks) {
*d = *s;
++d, ++s;
--blocks;
}
if (!n) {
dest = (char *)d;
*--dest = '\0';
return len;
}
src = (char *)s;
dest = (char *)d;
}
while (--n && (*dest = *src))
++dest, ++src;
if (!n)
*dest = '\0';
return len;
}
static bool program_exit(const char *str)
{
if (!strcmp(str, "exit") || !strcmp(str, "quit"))
return TRUE;
return FALSE;
}
static void remove_commas(char *str)
{
if (!str || !*str)
return;
char *iter1, *iter2;
for (iter1 = iter2 = str; *iter2; iter2++)
if (*iter2 != ',')
*iter1++ = *iter2;
*iter1 = '\0';
}
/*
* Try to evaluate en expression using bc
* If argument is NULL, global curexpr is picked
*/
static int try_bc(char *expr)
{
pid_t pid;
int pipe_pc[2], pipe_cp[2];
size_t len;
ssize_t ret;
char *ptr = cfg.calc ? "calc" : "bc";
remove_commas(expr);
if (!expr) {
if (curexpr)
expr = curexpr;
else
return -1;
}
log(DEBUG, "expression: \"%s\"\n", expr);
if (program_exit(expr))
exit(0);
if (pipe(pipe_pc) == -1 || pipe(pipe_cp) == -1) {
log(ERROR, "pipe()!\n");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
log(ERROR, "fork() failed!\n");
return -1;
}
if (pid == 0) { /* child */
close(STDOUT_FILENO);
close(STDIN_FILENO);
close(pipe_pc[1]); // Close writing end
close(pipe_cp[0]); // Close reading end
dup2(pipe_pc[0], STDIN_FILENO); // Take stdin from parent
dup2(pipe_cp[1], STDOUT_FILENO); // Give stdout to parent
dup2(pipe_cp[1], STDERR_FILENO); // Give stderr to parent
int ret = execlp(ptr, ptr, (char*) NULL);
log(ERROR, "execlp() failed!\n");
exit(ret);
}
if (!cfg.calc) {
/* parent */
if (write(pipe_pc[1], "scale=10\n", 9) != 9) {
log(ERROR, "write(1)! [%s]\n", strerror(errno));
exit(-1);
}
}
if (write(pipe_pc[1], "r=", 2) != 2) {
log(ERROR, "write(2)! [%s]\n", strerror(errno));
exit(-1);
}
if (lastres.p[0]) {
ret = (ssize_t)strlen(lastres.p);
if (write(pipe_pc[1], lastres.p, ret) != ret) {
log(ERROR, "write(3)! [%s]\n", strerror(errno));
exit(-1);
}
} else {
if (write(pipe_pc[1], "0", 1) != 1) {
log(ERROR, "write(4)! [%s]\n", strerror(errno));
exit(-1);
}
}
if (write(pipe_pc[1], "\n", 1) != 1) {
log(ERROR, "write(5)! [%s]\n", strerror(errno));
exit(-1);
}
ret = (ssize_t)strlen(expr);
if (write(pipe_pc[1], expr, ret) != ret) {
log(ERROR, "write(6)! [%s]\n", strerror(errno));
exit(-1);
}
if (write(pipe_pc[1], "\n", 1) != 1) {
log(ERROR, "write(7)! [%s]\n", strerror(errno));
exit(-1);
}
static char buffer[128];
ret = read(pipe_cp[0], buffer, sizeof(buffer) - 1);
if (ret == -1) {
log(ERROR, "read()! [%s]\n", strerror(errno));
exit(-1);
}
if (write(pipe_pc[1], "quit\n", 5) != 5) {
log(ERROR, "write(7)! [%s]\n", strerror(errno));
exit(-1);
}
if (cfg.calc)
kill(pid, SIGTERM);
close(pipe_cp[0]);
close(pipe_pc[1]);
buffer[ret] = '\0';
if ((buffer[0] != '(') && (strncmp(buffer, "Warning", 7) != 0) && (strncmp(buffer, "Missing", 7) != 0)) {
ptr = buffer;
while (isspace(*ptr)) /* calc results have space before them */
++ptr;
printf("%s", ptr); /* Print the result/error */
/* Detect common error conditions for calc and stop */
if (cfg.calc)
for (size_t r = 0; r < ELEMENTS(error_strings); ++r)
if (strstr(ptr, error_strings[r]))
return -1;
/* Store the result in 'r' for next usage */
len = bstrlcpy(lastres.p, ptr, NUM_LEN);
/* remove newline appended at the end of result by bc */
(len >= 2) ? (len -= 2) : (len = 0);
lastres.p[len] = '\0';
#ifdef TRIM_DECIMAL
/* Trim the decimal part, if any */
size_t count = 0;
while (count < len) {
if (lastres.p[count] == '.') {
lastres.p[count] = '\0';
break;
}
++count;
}
#endif
lastres.unit = 0;
log(DEBUG, "result: %s %d\n", lastres.p, lastres.unit);
return 0;
}
log(ERROR, "invalid expression\n");
return -1;
}
static void binprint(maxuint_t n)
{
int count = MAX_BITS - 1;
int pos = MAX_BITS + (MAX_BITS >> 2) - 1;
char binstr[MAX_BITS + (MAX_BITS >> 2) + 1] = {0};
if (!n) {
printf("0");
return;
}
while (n && count >= 0) {
binstr[pos] = "01"[n & 1];
--pos;
n >>= 1;
if (n && count && !(count & 7)) {
binstr[pos] = ' ';
--pos;
}
--count;
}
++pos;
printf("%s", binstr + pos);
}
static char *getstr_u128(maxuint_t n, char *buf)
{
if (n == 0) {
buf[0] = '0';
buf[1] = '\0';
return buf;
}
memset(buf, 0, UINT_BUF_LEN);
char *loc = buf + UINT_BUF_LEN - 1; /* start at the end */
while (n != 0) {
if (loc == buf)
return NULL; /* should not happen */
*--loc = "0123456789"[n % 10]; /* save the last digit */
n /= 10; /* drop the last digit */
}
return loc;
}
static char *getstr_f128(maxfloat_t val, char *buf)
{
int n = snprintf(buf, FLOAT_BUF_LEN, "%#*.10Le", FLOAT_WIDTH, val);
buf[n] = '\0';
return buf;
}
static void printval(maxfloat_t val, char *unit)
{
if (val - (maxuint_t)val == 0) // NOLINT
printf("%40s %s\n", getstr_u128((maxuint_t)val, uint_buf), unit);
else
printf("%s %s\n", getstr_f128(val, float_buf), unit);
}
static void printhex_u128(maxuint_t n)
{
ull high = (ull)(n >> (sizeof(maxuint_t) << 2));
if (high)
printf("0x%llx%llx", high, (ull)n);
else
printf("0x%llx", (ull)n);
}
/* This function adds check for binary input to strtoul() */
static ulong strtoul_b(char *token)
{
int base = 0;
/* NOTE: no NULL check here! */
if (strlen(token) > 2 && token[0] == '0' &&
(token[1] == 'b' || token[1] == 'B')) {
base = 2;
}
return strtoul(token + base, NULL, base);
}
/* This function adds check for binary input to strtoull() */
static ull strtoull_b(char *token)
{
int base = 0;
/* NOTE: no NULL check here! */
if (strlen(token) > 2 && token[0] == '0' &&
(token[1] == 'b' || token[1] == 'B')) {
base = 2;
}
return strtoull(token + base, NULL, base);
}
/* Converts a char to unsigned int according to base */
static bool ischarvalid(char ch, uint base, uint *val)
{
if (base == 2)
{
if (ch == '0' || ch == '1') {
*val = ch - '0';
return TRUE;
}
} else if (base == 16) {
if (ch >= '0' && ch <= '9') {
*val = ch - '0';
return TRUE;
}
if (ch >= 'a' && ch <= 'f') {
*val = (ch - 'a') + 10;
return TRUE;
}
if (ch >= 'A' && ch <= 'F') {
*val = (ch - 'A') + 10;
return TRUE;
}
} else if (base == 10) {
if (ch >= '0' && ch <= '9') {
*val = ch - '0';
return TRUE;
}
}
return FALSE;
}
/*
* Converts a non-floating representing string to maxuint_t
*/
static maxuint_t strtouquad(char *token, char **pch)
{
*pch = PASSED;
if (!token || !*token) {
*pch = FAILED;
return 0;
}
char *ptr;
maxuint_t val = 0, prevval = 0;
uint base = 10, multiplier = 0, digit, bits_used = 0;
uint max_bit_len = sizeof(maxuint_t) << 3;
if (token[0] == '0') {
if (token[1] == 'b' || token[1] == 'B') { /* binary */
base = 2;
multiplier = 1;
} else if (token[1] == 'x' || token[1] == 'X') { /* hex */
base = 16;
multiplier = 4;
}
}
if (base == 2 || base == 16) {
ptr = token + 2;
if (!*ptr) {
*pch = FAILED;
return 0;
}
while (*ptr && *ptr == '0')
++ptr;
if (!*ptr)
return 0;
while (*ptr) {
if (bits_used == max_bit_len || !ischarvalid(*ptr, base, &digit)) {
*pch = FAILED;
return 0;
}
val = (val << multiplier) + digit;
++bits_used;
++ptr;
}
return val;
}
/* Try base 10 for any other pattern */
ptr = token;
while (*ptr && *ptr == '0')
++ptr;
if (!*ptr)
return 0;
while (*ptr) {
if (!ischarvalid(*ptr, base, &digit)) {
*pch = FAILED;
return 0;
}
val = (val * 10) + digit;
/* Try to to detect overflow */
if (val < prevval) {
*pch = FAILED;
return 0;
}
prevval = val;
++ptr;
}
return val;
}
static maxuint_t convertbyte(char *buf, int *ret)
{
maxfloat_t val;
char *pch;
/* Convert and print in bytes (cannot be in float) */
maxuint_t bytes = strtouquad(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
/* Convert and print in IEC standard units */
printf("\n IEC standard (base 2)\n\n");
val = (maxfloat_t)bytes / 1024;
printval(val, "KiB");
val = (maxfloat_t)bytes / (1 << 20);
printval(val, "MiB");
val = (maxfloat_t)bytes / (1 << 30);
printval(val, "GiB");
val = (maxfloat_t)bytes / ((unsigned long long)1 << 40);
printval(val, "TiB");
/* Convert and print in SI standard values */
printf("\n SI standard (base 10)\n\n");
val = (maxfloat_t)bytes / 1000;
printval(val, "kB");
val = (maxfloat_t)bytes / 1000000;
printval(val, "MB");
val = (maxfloat_t)bytes / 1000000000;
printval(val, "GB");
val = (maxfloat_t)bytes / 1000000000000;
printval(val, "TB");
return bytes;
}
static maxuint_t convertkib(char *buf, int *ret)
{
char *pch;
maxfloat_t val, kib = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(kib * 1024);
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
printval(kib, "KiB");
val = kib / 1024;
printval(val, "MiB");
val = kib / (1 << 20);
printval(val, "GiB");
val = kib / (1 << 30);
printval(val, "TiB");
printf("\n SI standard (base 10)\n\n");
val = kib * 1024 / 1000;
printval(val, "kB");
val = kib * 1024 / 1000000;
printval(val, "MB");
val = kib * 1024 / 1000000000;
printval(val, "GB");
val = kib * 1024 / 1000000000000;
printval(val, "TB");
return bytes;
}
static maxuint_t convertmib(char *buf, int *ret)
{
char *pch;
maxfloat_t val, mib = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(mib * (1 << 20));
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = mib * 1024;
printval(val, "KiB");
printval(mib, "MiB");
val = mib / 1024;
printval(val, "GiB");
val = mib / (1 << 20);
printval(val, "TiB");
printf("\n SI standard (base 10)\n\n");
val = mib * (1 << 20) / 1000;
printval(val, "kB");
val = mib * (1 << 20) / 1000000;
printval(val, "MB");
val = mib * (1 << 20) / 1000000000;
printval(val, "GB");
val = mib * (1 << 20) / 1000000000000;
printval(val, "TB");
return bytes;
}
static maxuint_t convertgib(char *buf, int *ret)
{
char *pch;
maxfloat_t val, gib = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(gib * (1 << 30));
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = gib * (1 << 20);
printval(val, "KiB");
val = gib * 1024;
printval(val, "MiB");
printval(gib, "GiB");
val = gib / 1024;
printval(val, "TiB");
printf("\n SI standard (base 10)\n\n");
val = gib * (1 << 30) / 1000;
printval(val, "kB");
val = gib * (1 << 30) / 1000000;
printval(val, "MB");
val = gib * (1 << 30) / 1000000000;
printval(val, "GB");
val = gib * (1 << 30) / 1000000000000;
printval(val, "TB");
return bytes;
}
static maxuint_t converttib(char *buf, int *ret)
{
char *pch;
maxfloat_t val, tib = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(tib * ((maxuint_t)1 << 40));
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = tib * (1 << 30);
printval(val, "KiB");
val = tib * (1 << 20);
printval(val, "MiB");
val = tib * 1024;
printval(val, "GiB");
printval(tib, "TiB");
printf("\n SI standard (base 10)\n\n");
val = tib * ((maxuint_t)1 << 40) / 1000;
printval(val, "kB");
val = tib * ((maxuint_t)1 << 40) / 1000000;
printval(val, "MB");
val = tib * ((maxuint_t)1 << 40) / 1000000000;
printval(val, "GB");
val = tib * ((maxuint_t)1 << 40) / 1000000000000;
printval(val, "TB");
return bytes;
}
static maxuint_t convertkb(char *buf, int *ret)
{
char *pch;
maxfloat_t val, kb = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(kb * 1000);
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = kb * 1000 / 1024;
printval(val, "KiB");
val = kb * 1000 / (1 << 20);
printval(val, "MiB");
val = kb * 1000 / (1 << 30);
printval(val, "GiB");
val = kb * 1000 / ((maxuint_t)1 << 40);
printval(val, "TiB");
printf("\n SI standard (base 10)\n\n");
printval(kb, "kB");
val = kb / 1000;
printval(val, "MB");
val = kb / 1000000;
printval(val, "GB");
val = kb / 1000000000;
printval(val, "TB");
return bytes;
}
static maxuint_t convertmb(char *buf, int *ret)
{
char *pch;
maxfloat_t val, mb = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(mb * 1000000);
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = mb * 1000000 / 1024;
printval(val, "KiB");
val = mb * 1000000 / (1 << 20);
printval(val, "MiB");
val = mb * 1000000 / (1 << 30);
printval(val, "GiB");
val = mb * 1000000 / ((maxuint_t)1 << 40);
printval(val, "TiB");
printf("\n SI standard (base 10)\n\n");
val = mb * 1000;
printval(val, "kB");
printval(mb, "MB");
val = mb / 1000;
printval(val, "GB");
val = mb / 1000000;
printval(val, "TB");
return bytes;
}
static maxuint_t convertgb(char *buf, int *ret)
{
char *pch;
maxfloat_t val, gb = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (maxuint_t)(gb * 1000000000);
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = gb * 1000000000 / 1024;
printval(val, "KiB");
val = gb * 1000000000 / (1 << 20);
printval(val, "MiB");
val = gb * 1000000000 / (1 << 30);
printval(val, "GiB");
val = gb * 1000000000 / ((maxuint_t)1 << 40);
printval(val, "TiB");
printf("\n SI standard (base 10)\n\n");
val = gb * 1000000;
printval(val, "kB");
val = gb * 1000;
printval(val, "MB");
printval(gb, "GB");
val = gb / 1000;
printval(val, "TB");
return bytes;
}
static maxuint_t converttb(char *buf, int *ret)
{
char *pch;
maxfloat_t val, tb = strtold(buf, &pch);
if (*pch) {
*ret = -1;
return 0;
}
*ret = 0;
maxuint_t bytes = (__uint128_t)(tb * 1000000000000);
if (cfg.minimal) {
printf("%s B\n", getstr_u128(bytes, uint_buf));
return bytes;
}
printf("%40s B\n", getstr_u128(bytes, uint_buf));
printf("\n IEC standard (base 2)\n\n");
val = tb * 1000000000000 / 1024;
printval(val, "KiB");
val = tb * 1000000000000 / (1 << 20);
printval(val, "MiB");
val = tb * 1000000000000 / (1 << 30);
printval(val, "GiB");
val = tb * 1000000000000 / ((maxuint_t)1 << 40);