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