getbsize.c revision 1.2
11.1Smycroft/*-
21.1Smycroft * Copyright (c) 1991 The Regents of the University of California.
31.1Smycroft * All rights reserved.
41.1Smycroft *
51.1Smycroft * Redistribution and use in source and binary forms, with or without
61.1Smycroft * modification, are permitted provided that the following conditions
71.1Smycroft * are met:
81.1Smycroft * 1. Redistributions of source code must retain the above copyright
91.1Smycroft *    notice, this list of conditions and the following disclaimer.
101.1Smycroft * 2. Redistributions in binary form must reproduce the above copyright
111.1Smycroft *    notice, this list of conditions and the following disclaimer in the
121.1Smycroft *    documentation and/or other materials provided with the distribution.
131.1Smycroft * 3. All advertising materials mentioning features or use of this software
141.1Smycroft *    must display the following acknowledgement:
151.1Smycroft *	This product includes software developed by the University of
161.1Smycroft *	California, Berkeley and its contributors.
171.1Smycroft * 4. Neither the name of the University nor the names of its contributors
181.1Smycroft *    may be used to endorse or promote products derived from this software
191.1Smycroft *    without specific prior written permission.
201.1Smycroft *
211.1Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221.1Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231.1Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241.1Smycroft * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251.1Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261.1Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271.1Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281.1Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291.1Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301.1Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Smycroft * SUCH DAMAGE.
321.1Smycroft */
331.1Smycroft
341.1Smycroft#ifndef lint
351.1Smycroft/*static char sccsid[] = "from: @(#)getbsize.c	5.3 (Berkeley) 3/9/92";*/
361.2Scgdstatic char rcsid[] = "$Id: getbsize.c,v 1.2 1994/01/25 20:06:03 cgd Exp $";
371.1Smycroft#endif /* not lint */
381.1Smycroft
391.2Scgd#include <err.h>
401.1Smycroft#include <stdio.h>
411.1Smycroft#include <stdlib.h>
421.2Scgd#include <string.h>
431.1Smycroft
441.1Smycroftchar *
451.2Scgdgetbsize(headerlenp, blocksizep, force)
461.1Smycroft	int *headerlenp;
471.1Smycroft	long *blocksizep;
481.1Smycroft	int force;
491.1Smycroft{
501.1Smycroft	static char header[20];
511.1Smycroft	long n, max, mul, blocksize;
521.1Smycroft	char *ep, *p, *form;
531.1Smycroft
541.1Smycroft#define	KB	(1024L)
551.1Smycroft#define	MB	(1024L * 1024L)
561.1Smycroft#define	GB	(1024L * 1024L * 1024L)
571.1Smycroft#define	MAXB	GB		/* No tera, peta, nor exa. */
581.1Smycroft	form = "";
591.1Smycroft	if (force) {
601.1Smycroft		blocksize = *blocksizep;
611.1Smycroft		if ((blocksize % GB) == 0) {
621.1Smycroft			form = "G";
631.1Smycroft			n = blocksize / GB;
641.1Smycroft		} else if ((blocksize % MB) == 0) {
651.1Smycroft			form = "M";
661.1Smycroft			n = blocksize / MB;
671.1Smycroft		} else if ((blocksize % KB) == 0) {
681.1Smycroft			form = "K";
691.1Smycroft			n = blocksize / KB;
701.1Smycroft		} else {
711.1Smycroft			n = blocksize;
721.1Smycroft		}
731.1Smycroft	} else if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
741.1Smycroft		if ((n = strtol(p, &ep, 10)) < 0)
751.1Smycroft			goto underflow;
761.1Smycroft		if (n == 0)
771.1Smycroft			n = 1;
781.1Smycroft		if (*ep && ep[1])
791.1Smycroft			goto fmterr;
801.1Smycroft		switch (*ep) {
811.1Smycroft		case 'G': case 'g':
821.1Smycroft			form = "G";
831.1Smycroft			max = MAXB / GB;
841.1Smycroft			mul = GB;
851.1Smycroft			break;
861.1Smycroft		case 'K': case 'k':
871.1Smycroft			form = "K";
881.1Smycroft			max = MAXB / KB;
891.1Smycroft			mul = KB;
901.1Smycroft			break;
911.1Smycroft		case 'M': case 'm':
921.1Smycroft			form = "M";
931.1Smycroft			max = MAXB / MB;
941.1Smycroft			mul = MB;
951.1Smycroft			break;
961.1Smycroft		case '\0':
971.1Smycroft			max = MAXB;
981.1Smycroft			mul = 1;
991.1Smycroft			break;
1001.1Smycroft		default:
1011.2Scgdfmterr:			warnx("%s: unknown blocksize", p);
1021.1Smycroft			n = 512;
1031.1Smycroft			mul = 1;
1041.1Smycroft			break;
1051.1Smycroft		}
1061.1Smycroft		if (n > max) {
1071.2Scgd			warnx("maximum blocksize is %dG", MAXB / GB);
1081.1Smycroft			n = max;
1091.1Smycroft		}
1101.1Smycroft		if ((blocksize = n * mul) < 512) {
1111.2Scgdunderflow:		warnx("%s: minimum blocksize is 512");
1121.1Smycroft			form = "";
1131.1Smycroft			blocksize = n = 512;
1141.1Smycroft		}
1151.1Smycroft	} else
1161.1Smycroft		blocksize = n = 512;
1171.1Smycroft
1181.1Smycroft	*headerlenp = snprintf(header, sizeof(header), "%d%s-blocks", n, form);
1191.1Smycroft	*blocksizep = blocksize;
1201.1Smycroft	return (header);
1211.1Smycroft}
122