xref: /csrg-svn/lib/libc/gen/getbsize.c (revision 56588)
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*56588Sbostic static char sccsid[] = "@(#)getbsize.c	5.4 (Berkeley) 10/17/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];
2252897Sbostic 	long n, max, mul, blocksize;
2352897Sbostic 	char *ep, *p, *form;
2452780Sbostic 
2552897Sbostic #define	KB	(1024L)
2652897Sbostic #define	MB	(1024L * 1024L)
2752897Sbostic #define	GB	(1024L * 1024L * 1024L)
2852897Sbostic #define	MAXB	GB		/* No tera, peta, nor exa. */
2952897Sbostic 	form = "";
3052780Sbostic 	if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
3152897Sbostic 		if ((n = strtol(p, &ep, 10)) < 0)
3252897Sbostic 			goto underflow;
3352897Sbostic 		if (n == 0)
3452897Sbostic 			n = 1;
3552897Sbostic 		if (*ep && ep[1])
3652897Sbostic 			goto fmterr;
3752897Sbostic 		switch (*ep) {
3852780Sbostic 		case 'G': case 'g':
3952897Sbostic 			form = "G";
4052897Sbostic 			max = MAXB / GB;
4152897Sbostic 			mul = GB;
4252897Sbostic 			break;
4352897Sbostic 		case 'K': case 'k':
4452897Sbostic 			form = "K";
4552897Sbostic 			max = MAXB / KB;
4652897Sbostic 			mul = KB;
4752897Sbostic 			break;
4852780Sbostic 		case 'M': case 'm':
4952897Sbostic 			form = "M";
5052897Sbostic 			max = MAXB / MB;
5152897Sbostic 			mul = MB;
5252897Sbostic 			break;
5352780Sbostic 		case '\0':
5452897Sbostic 			max = MAXB;
5552897Sbostic 			mul = 1;
5652780Sbostic 			break;
5752780Sbostic 		default:
5852780Sbostic fmterr:			(void)fprintf(stderr,
5952780Sbostic 			    "%s: %s: unknown blocksize\n", prog, p);
6052897Sbostic 			n = 512;
6152897Sbostic 			mul = 1;
6252780Sbostic 			break;
6352780Sbostic 		}
6452897Sbostic 		if (n > max) {
6552897Sbostic 			(void)fprintf(stderr,
6652897Sbostic 			    "%s: maximum blocksize is %dG\n", prog, MAXB / GB);
6752897Sbostic 			n = max;
6852897Sbostic 		}
6952897Sbostic 		if ((blocksize = n * mul) < 512) {
7052897Sbostic underflow:		(void)fprintf(stderr,
7152897Sbostic 			    "%s: minimum blocksize is 512\n", prog);
7252897Sbostic 			form = "";
7352897Sbostic 			blocksize = n = 512;
7452897Sbostic 		}
7552780Sbostic 	} else
7652897Sbostic 		blocksize = n = 512;
7752780Sbostic 
78*56588Sbostic 	(void)snprintf(header, sizeof(header), "%d%s-blocks", n, form);
79*56588Sbostic 	*headerlenp = strlen(header);
8052780Sbostic 	*blocksizep = blocksize;
8152780Sbostic 	return (header);
8252780Sbostic }
83