getbsize.c revision 1.3
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.3Scgdstatic char rcsid[] = "$Id: getbsize.c,v 1.3 1994/01/25 21:03:26 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.3Scgdgetbsize(headerlenp, blocksizep)
461.1Smycroft	int *headerlenp;
471.1Smycroft	long *blocksizep;
481.1Smycroft{
491.1Smycroft	static char header[20];
501.1Smycroft	long n, max, mul, blocksize;
511.1Smycroft	char *ep, *p, *form;
521.1Smycroft
531.1Smycroft#define	KB	(1024L)
541.1Smycroft#define	MB	(1024L * 1024L)
551.1Smycroft#define	GB	(1024L * 1024L * 1024L)
561.1Smycroft#define	MAXB	GB		/* No tera, peta, nor exa. */
571.1Smycroft	form = "";
581.3Scgd	if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
591.1Smycroft		if ((n = strtol(p, &ep, 10)) < 0)
601.1Smycroft			goto underflow;
611.1Smycroft		if (n == 0)
621.1Smycroft			n = 1;
631.1Smycroft		if (*ep && ep[1])
641.1Smycroft			goto fmterr;
651.1Smycroft		switch (*ep) {
661.1Smycroft		case 'G': case 'g':
671.1Smycroft			form = "G";
681.1Smycroft			max = MAXB / GB;
691.1Smycroft			mul = GB;
701.1Smycroft			break;
711.1Smycroft		case 'K': case 'k':
721.1Smycroft			form = "K";
731.1Smycroft			max = MAXB / KB;
741.1Smycroft			mul = KB;
751.1Smycroft			break;
761.1Smycroft		case 'M': case 'm':
771.1Smycroft			form = "M";
781.1Smycroft			max = MAXB / MB;
791.1Smycroft			mul = MB;
801.1Smycroft			break;
811.1Smycroft		case '\0':
821.1Smycroft			max = MAXB;
831.1Smycroft			mul = 1;
841.1Smycroft			break;
851.1Smycroft		default:
861.2Scgdfmterr:			warnx("%s: unknown blocksize", p);
871.1Smycroft			n = 512;
881.1Smycroft			mul = 1;
891.1Smycroft			break;
901.1Smycroft		}
911.1Smycroft		if (n > max) {
921.2Scgd			warnx("maximum blocksize is %dG", MAXB / GB);
931.1Smycroft			n = max;
941.1Smycroft		}
951.1Smycroft		if ((blocksize = n * mul) < 512) {
961.2Scgdunderflow:		warnx("%s: minimum blocksize is 512");
971.1Smycroft			form = "";
981.1Smycroft			blocksize = n = 512;
991.1Smycroft		}
1001.1Smycroft	} else
1011.1Smycroft		blocksize = n = 512;
1021.1Smycroft
1031.1Smycroft	*headerlenp = snprintf(header, sizeof(header), "%d%s-blocks", n, form);
1041.1Smycroft	*blocksizep = blocksize;
1051.1Smycroft	return (header);
1061.1Smycroft}
107