getbsize.c revision 1.11
11.11Slukem/* $NetBSD: getbsize.c,v 1.11 1999/09/16 11:44:57 lukem 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.1Smycroft * 3. All advertising materials mentioning features or use of this software 161.1Smycroft * must display the following acknowledgement: 171.1Smycroft * This product includes software developed by the University of 181.1Smycroft * California, Berkeley and its contributors. 191.1Smycroft * 4. Neither the name of the University nor the names of its contributors 201.1Smycroft * may be used to endorse or promote products derived from this software 211.1Smycroft * without specific prior written permission. 221.1Smycroft * 231.1Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 241.1Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251.1Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261.1Smycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 271.1Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281.1Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291.1Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301.1Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311.1Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321.1Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331.1Smycroft * SUCH DAMAGE. 341.1Smycroft */ 351.1Smycroft 361.9Schristos#include <sys/cdefs.h> 371.7Sjtc#if defined(LIBC_SCCS) && !defined(lint) 381.6Scgd#if 0 391.6Scgdstatic char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93"; 401.6Scgd#else 411.11Slukem__RCSID("$NetBSD: getbsize.c,v 1.11 1999/09/16 11:44:57 lukem Exp $"); 421.6Scgd#endif 431.1Smycroft#endif /* not lint */ 441.1Smycroft 451.9Schristos#include "namespace.h" 461.11Slukem 471.11Slukem#include <assert.h> 481.2Scgd#include <err.h> 491.1Smycroft#include <stdio.h> 501.1Smycroft#include <stdlib.h> 511.2Scgd#include <string.h> 521.10Sjtc 531.10Sjtc#ifdef __weak_alias 541.10Sjtc__weak_alias(getbsize,_getbsize); 551.10Sjtc#endif 561.1Smycroft 571.1Smycroftchar * 581.3Scgdgetbsize(headerlenp, blocksizep) 591.1Smycroft int *headerlenp; 601.1Smycroft long *blocksizep; 611.1Smycroft{ 621.1Smycroft static char header[20]; 631.1Smycroft long n, max, mul, blocksize; 641.1Smycroft char *ep, *p, *form; 651.11Slukem 661.11Slukem _DIAGASSERT(headerlenp != NULL); 671.11Slukem _DIAGASSERT(blocksizep != NULL); 681.11Slukem#ifdef _DIAGNOSTIC 691.11Slukem if (headerlenp == NULL || blocksizep == NULL) 701.11Slukem return (NULL); 711.11Slukem#endif 721.1Smycroft 731.1Smycroft#define KB (1024L) 741.1Smycroft#define MB (1024L * 1024L) 751.1Smycroft#define GB (1024L * 1024L * 1024L) 761.1Smycroft#define MAXB GB /* No tera, peta, nor exa. */ 771.1Smycroft form = ""; 781.3Scgd if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') { 791.1Smycroft if ((n = strtol(p, &ep, 10)) < 0) 801.1Smycroft goto underflow; 811.1Smycroft if (n == 0) 821.1Smycroft n = 1; 831.1Smycroft if (*ep && ep[1]) 841.1Smycroft goto fmterr; 851.1Smycroft switch (*ep) { 861.1Smycroft case 'G': case 'g': 871.1Smycroft form = "G"; 881.1Smycroft max = MAXB / GB; 891.1Smycroft mul = GB; 901.1Smycroft break; 911.1Smycroft case 'K': case 'k': 921.1Smycroft form = "K"; 931.1Smycroft max = MAXB / KB; 941.1Smycroft mul = KB; 951.1Smycroft break; 961.1Smycroft case 'M': case 'm': 971.1Smycroft form = "M"; 981.1Smycroft max = MAXB / MB; 991.1Smycroft mul = MB; 1001.1Smycroft break; 1011.1Smycroft case '\0': 1021.1Smycroft max = MAXB; 1031.1Smycroft mul = 1; 1041.1Smycroft break; 1051.1Smycroft default: 1061.9Schristosfmterr: warnx("%s: unknown blocksize", p); 1071.1Smycroft n = 512; 1081.1Smycroft mul = 1; 1091.9Schristos max = 0; 1101.1Smycroft break; 1111.1Smycroft } 1121.1Smycroft if (n > max) { 1131.9Schristos warnx("maximum blocksize is %ldG", MAXB / GB); 1141.1Smycroft n = max; 1151.1Smycroft } 1161.1Smycroft if ((blocksize = n * mul) < 512) { 1171.9Schristosunderflow: warnx("%s: minimum blocksize is 512", p); 1181.1Smycroft form = ""; 1191.1Smycroft blocksize = n = 512; 1201.1Smycroft } 1211.1Smycroft } else 1221.1Smycroft blocksize = n = 512; 1231.1Smycroft 1241.9Schristos *headerlenp = snprintf(header, sizeof(header), "%ld%s-blocks", n, form); 1251.1Smycroft *blocksizep = blocksize; 1261.1Smycroft return (header); 1271.1Smycroft} 128