getconf.c revision 59632
1/*
2 * Copyright 2000 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/usr.bin/getconf/getconf.c 59632 2000-04-26 02:36:54Z wollman $
30 */
31
32#include <sys/types.h>
33
34#include <err.h>
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <sysexits.h>
39#include <unistd.h>
40
41#include "getconf.h"
42
43static void	do_confstr(const char *name, int key);
44static void	do_sysconf(const char *name, int key);
45static void	do_pathconf(const char *name, int key, const char *path);
46
47static void
48usage(void)
49{
50	fprintf(stderr, "usage:\n"
51		"\tgetconf [-v prog_model] system_var\n"
52		"\tgetconf [-v prog_model] path_var pathname\n");
53	exit(EX_USAGE);
54}
55
56int
57main(int argc, char **argv)
58{
59	int c;
60	const char *name, *vflag;
61	int key;
62
63	vflag = 0;
64
65	while ((c = getopt(argc, argv, "v:")) != -1) {
66		switch (c) {
67		case 'v':
68			vflag = optarg;
69			break;
70
71		default:
72			usage();
73		}
74	}
75
76	if (vflag)
77		warnx("-v %s ignored", vflag);
78
79	/* No arguments... */
80	if ((name = argv[optind]) == 0)
81		usage();
82
83	if (argv[optind + 1] == 0) { /* confstr or sysconf */
84		key = find_confstr(name);
85		if (key >= 0) {
86			do_confstr(name, key);
87		} else {
88			key = find_sysconf(name);
89			if (key >= 0)
90				do_sysconf(name, key);
91			else
92				errx(EX_USAGE,
93				     "no such configuration parameter `%s'",
94				     name);
95		}
96	} else {
97		key = find_pathconf(name);
98		if (key >= 0)
99			do_pathconf(name, key, argv[optind + 1]);
100		else
101			errx(EX_USAGE,
102			     "no such path configuration parameter `%s'",
103			     name);
104	}
105	return 0;
106}
107
108static void
109do_confstr(const char *name, int key)
110{
111	char *buf;
112	size_t len;
113
114	len = confstr(key, 0, 0);
115	if (len < 0)
116		err(EX_OSERR, "confstr: %s", name);
117
118	if (len == 0) {
119		printf("undefined\n");
120	} else {
121		buf = alloca(len);
122		confstr(key, buf, len);
123		printf("%s\n", buf);
124	}
125}
126
127static void
128do_sysconf(const char *name, int key)
129{
130	long value;
131
132	errno = 0;
133	value = sysconf(key);
134	if (value == -1 && errno != 0)
135		err(EX_OSERR, "sysconf: %s", name);
136	else if (value == -1)
137		printf("undefined\n");
138	else
139		/*
140		 * SUSv2 specifies that the value, if defined, is to be
141		 * printed using the format "%d\n".  This is clearly
142		 * erroneous, since sysconf is defined to return a long
143		 * and not an int.
144		 */
145		printf("%ld\n", value);
146}
147
148static void
149do_pathconf(const char *name, int key, const char *path)
150{
151	long value;
152
153	errno = 0;
154	value = pathconf(path, key);
155	if (value == -1 && errno != 0)
156		err(EX_OSERR, "pathconf: %s", name);
157	else if (value == -1)
158		printf("undefined\n");
159	else
160		/*
161		 * SUSv2 specifies that the value, if defined, is to be
162		 * printed using the format "%d\n".  This is clearly
163		 * erroneous, since sysconf is defined to return a long
164		 * and not an int.
165		 */
166		printf("%ld\n", value);
167}
168