getbsize.c revision 181280
1164408Sceri/*-
2208291Suqs * Copyright (c) 1991, 1993
3164408Sceri *	The Regents of the University of California.  All rights reserved.
4164408Sceri *
5164408Sceri * Redistribution and use in source and binary forms, with or without
6164408Sceri * modification, are permitted provided that the following conditions
7164408Sceri * are met:
8164408Sceri * 1. Redistributions of source code must retain the above copyright
9164408Sceri *    notice, this list of conditions and the following disclaimer.
10164408Sceri * 2. Redistributions in binary form must reproduce the above copyright
11164408Sceri *    notice, this list of conditions and the following disclaimer in the
12164408Sceri *    documentation and/or other materials provided with the distribution.
13164408Sceri * 4. Neither the name of the University nor the names of its contributors
14164408Sceri *    may be used to endorse or promote products derived from this software
15164408Sceri *    without specific prior written permission.
16164408Sceri *
17164408Sceri * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18164408Sceri * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19164408Sceri * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20164408Sceri * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21164408Sceri * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22164408Sceri * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23164408Sceri * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24164408Sceri * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25164408Sceri * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26164408Sceri * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27164408Sceri * SUCH DAMAGE.
28164408Sceri */
29164408Sceri
30206622Suqs#if defined(LIBC_SCCS) && !defined(lint)
31164408Sceristatic char sccsid[] = "@(#)getbsize.c	8.1 (Berkeley) 6/4/93";
32164408Sceri#endif /* LIBC_SCCS and not lint */
33164408Sceri#include <sys/cdefs.h>
34164408Sceri__FBSDID("$FreeBSD: head/lib/libc/gen/getbsize.c 181280 2008-08-04 06:53:13Z cperciva $");
35164408Sceri
36164408Sceri#include <err.h>
37164408Sceri#include <stdio.h>
38164408Sceri#include <stdlib.h>
39164408Sceri#include <string.h>
40164408Sceri
41164408Scerichar *
42164408Scerigetbsize(headerlenp, blocksizep)
43164408Sceri	int *headerlenp;
44164408Sceri	long *blocksizep;
45164408Sceri{
46164408Sceri	static char header[20];
47164408Sceri	long n, max, mul, blocksize;
48164408Sceri	char *ep, *p;
49164408Sceri	const char *form;
50164408Sceri
51164408Sceri#define	KB	(1024L)
52164408Sceri#define	MB	(1024L * 1024L)
53164408Sceri#define	GB	(1024L * 1024L * 1024L)
54164408Sceri#define	MAXB	GB		/* No tera, peta, nor exa. */
55164408Sceri	form = "";
56164408Sceri	if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
57164408Sceri		if ((n = strtol(p, &ep, 10)) < 0)
58164408Sceri			goto underflow;
59164408Sceri		if (n == 0)
60164408Sceri			n = 1;
61164408Sceri		if (*ep && ep[1])
62164408Sceri			goto fmterr;
63164408Sceri		switch (*ep) {
64164408Sceri		case 'G': case 'g':
65164408Sceri			form = "G";
66164408Sceri			max = MAXB / GB;
67164408Sceri			mul = GB;
68164408Sceri			break;
69164408Sceri		case 'K': case 'k':
70164408Sceri			form = "K";
71164408Sceri			max = MAXB / KB;
72164408Sceri			mul = KB;
73164408Sceri			break;
74164408Sceri		case 'M': case 'm':
75164408Sceri			form = "M";
76164408Sceri			max = MAXB / MB;
77164408Sceri			mul = MB;
78164408Sceri			break;
79164408Sceri		case '\0':
80164408Sceri			max = MAXB;
81164408Sceri			mul = 1;
82164408Sceri			break;
83164408Sceri		default:
84164408Scerifmterr:			warnx("%s: unknown blocksize", p);
85164408Sceri			n = 512;
86164408Sceri			max = MAXB;
87164408Sceri			mul = 1;
88164408Sceri			break;
89164408Sceri		}
90164408Sceri		if (n > max) {
91164408Sceri			warnx("maximum blocksize is %ldG", MAXB / GB);
92164408Sceri			n = max;
93164408Sceri		}
94		if ((blocksize = n * mul) < 512) {
95underflow:		warnx("minimum blocksize is 512");
96			form = "";
97			blocksize = n = 512;
98		}
99	} else
100		blocksize = n = 512;
101
102	(void)snprintf(header, sizeof(header), "%ld%s-blocks", n, form);
103	*headerlenp = strlen(header);
104	*blocksizep = blocksize;
105	return (header);
106}
107