getbsize.c revision 1.1
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.1Smycroftstatic char rcsid[] = "$Id: getbsize.c,v 1.1 1993/08/06 17:03:55 mycroft Exp $"; 371.1Smycroft#endif /* not lint */ 381.1Smycroft 391.1Smycroft#include <stdio.h> 401.1Smycroft#include <stdlib.h> 411.1Smycroft 421.1Smycroftchar * 431.1Smycroftgetbsize(prog, headerlenp, blocksizep, force) 441.1Smycroft char *prog; 451.1Smycroft int *headerlenp; 461.1Smycroft long *blocksizep; 471.1Smycroft int force; 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.1Smycroft if (force) { 591.1Smycroft blocksize = *blocksizep; 601.1Smycroft if ((blocksize % GB) == 0) { 611.1Smycroft form = "G"; 621.1Smycroft n = blocksize / GB; 631.1Smycroft } else if ((blocksize % MB) == 0) { 641.1Smycroft form = "M"; 651.1Smycroft n = blocksize / MB; 661.1Smycroft } else if ((blocksize % KB) == 0) { 671.1Smycroft form = "K"; 681.1Smycroft n = blocksize / KB; 691.1Smycroft } else { 701.1Smycroft n = blocksize; 711.1Smycroft } 721.1Smycroft } else if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') { 731.1Smycroft if ((n = strtol(p, &ep, 10)) < 0) 741.1Smycroft goto underflow; 751.1Smycroft if (n == 0) 761.1Smycroft n = 1; 771.1Smycroft if (*ep && ep[1]) 781.1Smycroft goto fmterr; 791.1Smycroft switch (*ep) { 801.1Smycroft case 'G': case 'g': 811.1Smycroft form = "G"; 821.1Smycroft max = MAXB / GB; 831.1Smycroft mul = GB; 841.1Smycroft break; 851.1Smycroft case 'K': case 'k': 861.1Smycroft form = "K"; 871.1Smycroft max = MAXB / KB; 881.1Smycroft mul = KB; 891.1Smycroft break; 901.1Smycroft case 'M': case 'm': 911.1Smycroft form = "M"; 921.1Smycroft max = MAXB / MB; 931.1Smycroft mul = MB; 941.1Smycroft break; 951.1Smycroft case '\0': 961.1Smycroft max = MAXB; 971.1Smycroft mul = 1; 981.1Smycroft break; 991.1Smycroft default: 1001.1Smycroftfmterr: (void)fprintf(stderr, 1011.1Smycroft "%s: %s: unknown blocksize\n", prog, p); 1021.1Smycroft n = 512; 1031.1Smycroft mul = 1; 1041.1Smycroft break; 1051.1Smycroft } 1061.1Smycroft if (n > max) { 1071.1Smycroft (void)fprintf(stderr, 1081.1Smycroft "%s: maximum blocksize is %dG\n", prog, MAXB / GB); 1091.1Smycroft n = max; 1101.1Smycroft } 1111.1Smycroft if ((blocksize = n * mul) < 512) { 1121.1Smycroftunderflow: (void)fprintf(stderr, 1131.1Smycroft "%s: minimum blocksize is 512\n", prog); 1141.1Smycroft form = ""; 1151.1Smycroft blocksize = n = 512; 1161.1Smycroft } 1171.1Smycroft } else 1181.1Smycroft blocksize = n = 512; 1191.1Smycroft 1201.1Smycroft *headerlenp = snprintf(header, sizeof(header), "%d%s-blocks", n, form); 1211.1Smycroft *blocksizep = blocksize; 1221.1Smycroft return (header); 1231.1Smycroft} 124