getbsize.c revision 92986
122650Smpp/*- 222650Smpp * Copyright (c) 1991, 1993 322650Smpp * The Regents of the University of California. All rights reserved. 422650Smpp * 522650Smpp * Redistribution and use in source and binary forms, with or without 622650Smpp * modification, are permitted provided that the following conditions 722650Smpp * are met: 822650Smpp * 1. Redistributions of source code must retain the above copyright 922650Smpp * notice, this list of conditions and the following disclaimer. 1022650Smpp * 2. Redistributions in binary form must reproduce the above copyright 1122650Smpp * notice, this list of conditions and the following disclaimer in the 1222650Smpp * documentation and/or other materials provided with the distribution. 1322650Smpp * 3. All advertising materials mentioning features or use of this software 1422650Smpp * must display the following acknowledgement: 1522650Smpp * This product includes software developed by the University of 1622650Smpp * California, Berkeley and its contributors. 1722650Smpp * 4. Neither the name of the University nor the names of its contributors 1822650Smpp * may be used to endorse or promote products derived from this software 1922650Smpp * without specific prior written permission. 2022650Smpp * 2122650Smpp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2222650Smpp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2322650Smpp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2422650Smpp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2522650Smpp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2622650Smpp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2722650Smpp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2822650Smpp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2922650Smpp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3022650Smpp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3122650Smpp * SUCH DAMAGE. 3250476Speter */ 3348795Snik 34283676Smarkj#if defined(LIBC_SCCS) && !defined(lint) 3522650Smppstatic char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93"; 3622650Smpp#endif /* LIBC_SCCS and not lint */ 3722650Smpp#include <sys/cdefs.h> 3822650Smpp__FBSDID("$FreeBSD: head/lib/libc/gen/getbsize.c 92986 2002-03-22 21:53:29Z obrien $"); 3975670Sru 4022650Smpp#include <err.h> 41121151Sbde#include <stdio.h> 42121151Sbde#include <stdlib.h> 4322650Smpp#include <string.h> 4471895Sru 45283676Smarkjchar * 46283676Smarkjgetbsize(headerlenp, blocksizep) 4722650Smpp int *headerlenp; 4822650Smpp long *blocksizep; 4922650Smpp{ 50283676Smarkj static char header[20]; 51283676Smarkj long n, max, mul, blocksize; 52283676Smarkj char *ep, *p, *form; 53121151Sbde 5422650Smpp#define KB (1024L) 5522650Smpp#define MB (1024L * 1024L) 5622650Smpp#define GB (1024L * 1024L * 1024L) 57121151Sbde#define MAXB GB /* No tera, peta, nor exa. */ 58121151Sbde form = ""; 5979727Sschweikh if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') { 6022650Smpp if ((n = strtol(p, &ep, 10)) < 0) 6122650Smpp goto underflow; 6222650Smpp if (n == 0) 6322650Smpp n = 1; 6422650Smpp if (*ep && ep[1]) 6522650Smpp goto fmterr; 6622650Smpp switch (*ep) { 6722650Smpp case 'G': case 'g': 6822650Smpp form = "G"; 6922650Smpp max = MAXB / GB; 7022650Smpp mul = GB; 7122650Smpp break; 7222650Smpp case 'K': case 'k': 7322650Smpp form = "K"; 7422650Smpp max = MAXB / KB; 75 mul = KB; 76 break; 77 case 'M': case 'm': 78 form = "M"; 79 max = MAXB / MB; 80 mul = MB; 81 break; 82 case '\0': 83 max = MAXB; 84 mul = 1; 85 break; 86 default: 87fmterr: warnx("%s: unknown blocksize", p); 88 n = 512; 89 mul = 1; 90 break; 91 } 92 if (n > max) { 93 warnx("maximum blocksize is %ldG", MAXB / GB); 94 n = max; 95 } 96 if ((blocksize = n * mul) < 512) { 97underflow: warnx("minimum blocksize is 512"); 98 form = ""; 99 blocksize = n = 512; 100 } 101 } else 102 blocksize = n = 512; 103 104 (void)snprintf(header, sizeof(header), "%ld%s-blocks", n, form); 105 *headerlenp = strlen(header); 106 *blocksizep = blocksize; 107 return (header); 108} 109