1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11
12 #include <err.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 char *
getbsize(headerlenp,blocksizep)18 getbsize(headerlenp, blocksizep)
19 int *headerlenp;
20 long *blocksizep;
21 {
22 static char header[20];
23 long n, max, mul, blocksize;
24 char *ep, *p, *form;
25
26 #define KB (1024L)
27 #define MB (1024L * 1024L)
28 #define GB (1024L * 1024L * 1024L)
29 #define MAXB GB /* No tera, peta, nor exa. */
30 form = "";
31 if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
32 if ((n = strtol(p, &ep, 10)) < 0)
33 goto underflow;
34 if (n == 0)
35 n = 1;
36 if (*ep && ep[1])
37 goto fmterr;
38 switch (*ep) {
39 case 'G': case 'g':
40 form = "G";
41 max = MAXB / GB;
42 mul = GB;
43 break;
44 case 'K': case 'k':
45 form = "K";
46 max = MAXB / KB;
47 mul = KB;
48 break;
49 case 'M': case 'm':
50 form = "M";
51 max = MAXB / MB;
52 mul = MB;
53 break;
54 case '\0':
55 max = MAXB;
56 mul = 1;
57 break;
58 default:
59 fmterr: warnx("%s: unknown blocksize", p);
60 n = 512;
61 mul = 1;
62 break;
63 }
64 if (n > max) {
65 warnx("maximum blocksize is %dG", MAXB / GB);
66 n = max;
67 }
68 if ((blocksize = n * mul) < 512) {
69 underflow: warnx("minimum blocksize is 512");
70 form = "";
71 blocksize = n = 512;
72 }
73 } else
74 blocksize = n = 512;
75
76 (void)snprintf(header, sizeof(header), "%d%s-blocks", n, form);
77 *headerlenp = strlen(header);
78 *blocksizep = blocksize;
79 return (header);
80 }
81