getbsize.c revision 1.16
11.16Schristos/*	$NetBSD: getbsize.c,v 1.16 2005/11/29 03:11:59 christos Exp $	*/
21.6Scgd
31.1Smycroft/*-
41.6Scgd * Copyright (c) 1991, 1993
51.6Scgd *	The Regents of the University of California.  All rights reserved.
61.1Smycroft *
71.1Smycroft * Redistribution and use in source and binary forms, with or without
81.1Smycroft * modification, are permitted provided that the following conditions
91.1Smycroft * are met:
101.1Smycroft * 1. Redistributions of source code must retain the above copyright
111.1Smycroft *    notice, this list of conditions and the following disclaimer.
121.1Smycroft * 2. Redistributions in binary form must reproduce the above copyright
131.1Smycroft *    notice, this list of conditions and the following disclaimer in the
141.1Smycroft *    documentation and/or other materials provided with the distribution.
151.15Sagc * 3. Neither the name of the University nor the names of its contributors
161.1Smycroft *    may be used to endorse or promote products derived from this software
171.1Smycroft *    without specific prior written permission.
181.1Smycroft *
191.1Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Smycroft * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Smycroft * SUCH DAMAGE.
301.1Smycroft */
311.1Smycroft
321.9Schristos#include <sys/cdefs.h>
331.7Sjtc#if defined(LIBC_SCCS) && !defined(lint)
341.6Scgd#if 0
351.6Scgdstatic char sccsid[] = "@(#)getbsize.c	8.1 (Berkeley) 6/4/93";
361.6Scgd#else
371.16Schristos__RCSID("$NetBSD: getbsize.c,v 1.16 2005/11/29 03:11:59 christos Exp $");
381.6Scgd#endif
391.1Smycroft#endif /* not lint */
401.1Smycroft
411.9Schristos#include "namespace.h"
421.11Slukem
431.11Slukem#include <assert.h>
441.2Scgd#include <err.h>
451.1Smycroft#include <stdio.h>
461.1Smycroft#include <stdlib.h>
471.2Scgd#include <string.h>
481.10Sjtc
491.10Sjtc#ifdef __weak_alias
501.13Smycroft__weak_alias(getbsize,_getbsize)
511.10Sjtc#endif
521.1Smycroft
531.1Smycroftchar *
541.3Scgdgetbsize(headerlenp, blocksizep)
551.1Smycroft	int *headerlenp;
561.1Smycroft	long *blocksizep;
571.1Smycroft{
581.1Smycroft	static char header[20];
591.1Smycroft	long n, max, mul, blocksize;
601.16Schristos	char *ep, *p;
611.16Schristos	const char *form;
621.11Slukem
631.1Smycroft#define	KB	(1024L)
641.1Smycroft#define	MB	(1024L * 1024L)
651.1Smycroft#define	GB	(1024L * 1024L * 1024L)
661.1Smycroft#define	MAXB	GB		/* No tera, peta, nor exa. */
671.1Smycroft	form = "";
681.3Scgd	if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
691.1Smycroft		if ((n = strtol(p, &ep, 10)) < 0)
701.1Smycroft			goto underflow;
711.1Smycroft		if (n == 0)
721.1Smycroft			n = 1;
731.1Smycroft		if (*ep && ep[1])
741.1Smycroft			goto fmterr;
751.1Smycroft		switch (*ep) {
761.1Smycroft		case 'G': case 'g':
771.1Smycroft			form = "G";
781.1Smycroft			max = MAXB / GB;
791.1Smycroft			mul = GB;
801.1Smycroft			break;
811.1Smycroft		case 'K': case 'k':
821.1Smycroft			form = "K";
831.1Smycroft			max = MAXB / KB;
841.1Smycroft			mul = KB;
851.1Smycroft			break;
861.1Smycroft		case 'M': case 'm':
871.1Smycroft			form = "M";
881.1Smycroft			max = MAXB / MB;
891.1Smycroft			mul = MB;
901.1Smycroft			break;
911.1Smycroft		case '\0':
921.1Smycroft			max = MAXB;
931.1Smycroft			mul = 1;
941.1Smycroft			break;
951.1Smycroft		default:
961.9Schristosfmterr:			warnx("%s: unknown blocksize", p);
971.1Smycroft			n = 512;
981.1Smycroft			mul = 1;
991.9Schristos			max = 0;
1001.1Smycroft			break;
1011.1Smycroft		}
1021.1Smycroft		if (n > max) {
1031.9Schristos			warnx("maximum blocksize is %ldG", MAXB / GB);
1041.1Smycroft			n = max;
1051.1Smycroft		}
1061.1Smycroft		if ((blocksize = n * mul) < 512) {
1071.9Schristosunderflow:		warnx("%s: minimum blocksize is 512", p);
1081.1Smycroft			form = "";
1091.1Smycroft			blocksize = n = 512;
1101.1Smycroft		}
1111.1Smycroft	} else
1121.1Smycroft		blocksize = n = 512;
1131.1Smycroft
1141.14Ssimonb	if (headerlenp)
1151.14Ssimonb		*headerlenp =
1161.14Ssimonb		    snprintf(header, sizeof(header), "%ld%s-blocks", n, form);
1171.14Ssimonb	if (blocksizep)
1181.14Ssimonb		*blocksizep = blocksize;
1191.1Smycroft	return (header);
1201.1Smycroft}
121