1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char rcsid[] = "$OpenBSD: getbsize.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $"; 32 #endif /* not lint */ 33 34 #include <err.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 char * 40 getbsize(int *headerlenp, long *blocksizep) 41 { 42 static char header[20]; 43 long n, max, mul, blocksize; 44 char *ep, *p, *form; 45 46 #define KB (1024) 47 #define MB (1024 * 1024) 48 #define GB (1024 * 1024 * 1024) 49 #define MAXB GB /* No tera, peta, nor exa. */ 50 form = ""; 51 if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') { 52 if ((n = strtol(p, &ep, 10)) < 0) 53 goto underflow; 54 if (n == 0) 55 n = 1; 56 if (*ep && ep[1]) 57 goto fmterr; 58 switch (*ep) { 59 case 'G': case 'g': 60 form = "G"; 61 max = MAXB / GB; 62 mul = GB; 63 break; 64 case 'K': case 'k': 65 form = "K"; 66 max = MAXB / KB; 67 mul = KB; 68 break; 69 case 'M': case 'm': 70 form = "M"; 71 max = MAXB / MB; 72 mul = MB; 73 break; 74 case '\0': 75 max = MAXB; 76 mul = 1; 77 break; 78 default: 79 fmterr: _warnx("%s: unknown blocksize", p); 80 n = 512; 81 max = MAXB; 82 mul = 1; 83 break; 84 } 85 if (n > max) { 86 _warnx("maximum blocksize is %dG", MAXB / GB); 87 n = max; 88 } 89 if ((blocksize = n * mul) < 512) { 90 underflow: _warnx("%s: minimum blocksize is 512", p); 91 form = ""; 92 blocksize = n = 512; 93 } 94 } else 95 blocksize = n = 512; 96 97 *headerlenp = snprintf(header, sizeof(header), "%ld%s-blocks", n, form); 98 if (*headerlenp < 0) 99 *headerlenp = 0; 100 else if (*headerlenp >= sizeof(header)) 101 *headerlenp = sizeof(header) - 1; 102 *blocksizep = blocksize; 103 return (header); 104 } 105