xref: /csrg-svn/lib/libc/gen/getbsize.c (revision 52897)
152780Sbostic /*-
252780Sbostic  * Copyright (c) 1991 The Regents of the University of California.
352780Sbostic  * All rights reserved.
452780Sbostic  *
552780Sbostic  * %sccs.include.redist.c%
652780Sbostic  */
752780Sbostic 
852780Sbostic #ifndef lint
9*52897Sbostic static char sccsid[] = "@(#)getbsize.c	5.3 (Berkeley) 03/09/92";
1052780Sbostic #endif /* not lint */
1152780Sbostic 
1252780Sbostic #include <stdio.h>
1352780Sbostic #include <stdlib.h>
1452780Sbostic 
1552780Sbostic char *
1652780Sbostic getbsize(prog, headerlenp, blocksizep)
1752780Sbostic 	char *prog;
1852896Sbostic 	int *headerlenp;
1952896Sbostic 	long *blocksizep;
2052780Sbostic {
2152780Sbostic 	static char header[20];
22*52897Sbostic 	long n, max, mul, blocksize;
23*52897Sbostic 	char *ep, *p, *form;
2452780Sbostic 
25*52897Sbostic #define	KB	(1024L)
26*52897Sbostic #define	MB	(1024L * 1024L)
27*52897Sbostic #define	GB	(1024L * 1024L * 1024L)
28*52897Sbostic #define	MAXB	GB		/* No tera, peta, nor exa. */
29*52897Sbostic 	form = "";
3052780Sbostic 	if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
31*52897Sbostic 		if ((n = strtol(p, &ep, 10)) < 0)
32*52897Sbostic 			goto underflow;
33*52897Sbostic 		if (n == 0)
34*52897Sbostic 			n = 1;
35*52897Sbostic 		if (*ep && ep[1])
36*52897Sbostic 			goto fmterr;
37*52897Sbostic 		switch (*ep) {
3852780Sbostic 		case 'G': case 'g':
39*52897Sbostic 			form = "G";
40*52897Sbostic 			max = MAXB / GB;
41*52897Sbostic 			mul = GB;
42*52897Sbostic 			break;
43*52897Sbostic 		case 'K': case 'k':
44*52897Sbostic 			form = "K";
45*52897Sbostic 			max = MAXB / KB;
46*52897Sbostic 			mul = KB;
47*52897Sbostic 			break;
4852780Sbostic 		case 'M': case 'm':
49*52897Sbostic 			form = "M";
50*52897Sbostic 			max = MAXB / MB;
51*52897Sbostic 			mul = MB;
52*52897Sbostic 			break;
5352780Sbostic 		case '\0':
54*52897Sbostic 			max = MAXB;
55*52897Sbostic 			mul = 1;
5652780Sbostic 			break;
5752780Sbostic 		default:
5852780Sbostic fmterr:			(void)fprintf(stderr,
5952780Sbostic 			    "%s: %s: unknown blocksize\n", prog, p);
60*52897Sbostic 			n = 512;
61*52897Sbostic 			mul = 1;
6252780Sbostic 			break;
6352780Sbostic 		}
64*52897Sbostic 		if (n > max) {
65*52897Sbostic 			(void)fprintf(stderr,
66*52897Sbostic 			    "%s: maximum blocksize is %dG\n", prog, MAXB / GB);
67*52897Sbostic 			n = max;
68*52897Sbostic 		}
69*52897Sbostic 		if ((blocksize = n * mul) < 512) {
70*52897Sbostic underflow:		(void)fprintf(stderr,
71*52897Sbostic 			    "%s: minimum blocksize is 512\n", prog);
72*52897Sbostic 			form = "";
73*52897Sbostic 			blocksize = n = 512;
74*52897Sbostic 		}
7552780Sbostic 	} else
76*52897Sbostic 		blocksize = n = 512;
7752780Sbostic 
78*52897Sbostic 	*headerlenp = snprintf(header, sizeof(header), "%d%s-blocks", n, form);
7952780Sbostic 	*blocksizep = blocksize;
8052780Sbostic 	return (header);
8152780Sbostic }
82