xref: /csrg-svn/lib/libc/gen/getbsize.c (revision 61111)
152780Sbostic /*-
2*61111Sbostic  * Copyright (c) 1991, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
452780Sbostic  *
552780Sbostic  * %sccs.include.redist.c%
652780Sbostic  */
752780Sbostic 
852780Sbostic #ifndef lint
9*61111Sbostic static char sccsid[] = "@(#)getbsize.c	8.1 (Berkeley) 06/04/93";
1052780Sbostic #endif /* not lint */
1152780Sbostic 
1259470Sbostic #include <err.h>
1352780Sbostic #include <stdio.h>
1452780Sbostic #include <stdlib.h>
1559470Sbostic #include <string.h>
1652780Sbostic 
1752780Sbostic char *
getbsize(headerlenp,blocksizep)1859470Sbostic getbsize(headerlenp, blocksizep)
1952896Sbostic 	int *headerlenp;
2052896Sbostic 	long *blocksizep;
2152780Sbostic {
2252780Sbostic 	static char header[20];
2352897Sbostic 	long n, max, mul, blocksize;
2452897Sbostic 	char *ep, *p, *form;
2552780Sbostic 
2652897Sbostic #define	KB	(1024L)
2752897Sbostic #define	MB	(1024L * 1024L)
2852897Sbostic #define	GB	(1024L * 1024L * 1024L)
2952897Sbostic #define	MAXB	GB		/* No tera, peta, nor exa. */
3052897Sbostic 	form = "";
3152780Sbostic 	if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
3252897Sbostic 		if ((n = strtol(p, &ep, 10)) < 0)
3352897Sbostic 			goto underflow;
3452897Sbostic 		if (n == 0)
3552897Sbostic 			n = 1;
3652897Sbostic 		if (*ep && ep[1])
3752897Sbostic 			goto fmterr;
3852897Sbostic 		switch (*ep) {
3952780Sbostic 		case 'G': case 'g':
4052897Sbostic 			form = "G";
4152897Sbostic 			max = MAXB / GB;
4252897Sbostic 			mul = GB;
4352897Sbostic 			break;
4452897Sbostic 		case 'K': case 'k':
4552897Sbostic 			form = "K";
4652897Sbostic 			max = MAXB / KB;
4752897Sbostic 			mul = KB;
4852897Sbostic 			break;
4952780Sbostic 		case 'M': case 'm':
5052897Sbostic 			form = "M";
5152897Sbostic 			max = MAXB / MB;
5252897Sbostic 			mul = MB;
5352897Sbostic 			break;
5452780Sbostic 		case '\0':
5552897Sbostic 			max = MAXB;
5652897Sbostic 			mul = 1;
5752780Sbostic 			break;
5852780Sbostic 		default:
5959470Sbostic fmterr:			warnx("%s: unknown blocksize", p);
6052897Sbostic 			n = 512;
6152897Sbostic 			mul = 1;
6252780Sbostic 			break;
6352780Sbostic 		}
6452897Sbostic 		if (n > max) {
6559470Sbostic 			warnx("maximum blocksize is %dG", MAXB / GB);
6652897Sbostic 			n = max;
6752897Sbostic 		}
6852897Sbostic 		if ((blocksize = n * mul) < 512) {
6959470Sbostic underflow:		warnx("minimum blocksize is 512");
7052897Sbostic 			form = "";
7152897Sbostic 			blocksize = n = 512;
7252897Sbostic 		}
7352780Sbostic 	} else
7452897Sbostic 		blocksize = n = 512;
7552780Sbostic 
7656588Sbostic 	(void)snprintf(header, sizeof(header), "%d%s-blocks", n, form);
7756588Sbostic 	*headerlenp = strlen(header);
7852780Sbostic 	*blocksizep = blocksize;
7952780Sbostic 	return (header);
8052780Sbostic }
81