2 * Copyright 2000 Massachusetts Institute of Technology
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
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
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
43 static void do_allsys(void);
44 static void do_allpath(const char *path);
45 static void do_confstr(const char *name, int key);
46 static void do_sysconf(const char *name, int key);
47 static void do_pathconf(const char *name, int key, const char *path);
53 "usage: getconf -a [pathname]\n"
54 " getconf [-v prog_env] system_var\n"
55 " getconf [-v prog_env] path_var pathname\n");
60 main(int argc, char **argv)
64 const char *name, *vflag, *alt_path;
70 while ((c = getopt(argc, argv, "av:")) != -1) {
87 if (argv[optind] == NULL)
90 do_allpath(argv[optind]);
94 if ((name = argv[optind]) == NULL)
98 if ((valid = find_progenv(vflag, &alt_path)) == 0)
99 errx(EX_USAGE, "invalid programming environment %s",
101 if (valid > 0 && alt_path != NULL) {
102 if (argv[optind + 1] == NULL)
103 execl(alt_path, "getconf", argv[optind],
106 execl(alt_path, "getconf", argv[optind],
107 argv[optind + 1], (char *)NULL);
109 err(EX_OSERR, "execl: %s", alt_path);
112 errx(EX_UNAVAILABLE, "environment %s is not available",
116 if (argv[optind + 1] == NULL) { /* confstr or sysconf */
117 if ((valid = find_unsigned_limit(name, &ulimitval)) != 0) {
119 printf("%" PRIuMAX "\n", ulimitval);
121 printf("undefined\n");
124 if ((valid = find_limit(name, &limitval)) != 0) {
126 printf("%" PRIdMAX "\n", limitval);
128 printf("undefined\n");
132 if ((valid = find_confstr(name, &key)) != 0) {
134 do_confstr(name, key);
136 printf("undefined\n");
138 valid = find_sysconf(name, &key);
140 do_sysconf(name, key);
141 } else if (valid < 0) {
142 printf("undefined\n");
145 "no such configuration parameter `%s'",
149 valid = find_pathconf(name, &key);
152 do_pathconf(name, key, argv[optind + 1]);
154 printf("undefined\n");
157 "no such path configuration parameter `%s'",
164 do_onestr(const char *name, int key)
169 len = confstr(key, 0, 0);
170 if (len == 0 && errno != 0) {
171 warn("confstr: %s", name);
174 printf("%s: ", name);
176 printf("undefined\n");
180 confstr(key, buf, len);
186 do_onesys(const char *name, int key)
191 value = sysconf(key);
192 if (value == -1 && errno != 0) {
193 warn("sysconf: %s", name);
196 printf("%s: ", name);
198 printf("undefined\n");
200 printf("%ld\n", value);
207 foreach_confstr(do_onestr);
208 foreach_sysconf(do_onesys);
212 do_onepath(const char *name, int key, const char *path)
217 value = pathconf(path, key);
218 if (value == -1 && errno != EINVAL && errno != 0)
219 warn("pathconf: %s", name);
220 printf("%s: ", name);
222 printf("undefined\n");
224 printf("%ld\n", value);
228 do_allpath(const char *path)
231 foreach_pathconf(do_onepath, path);
235 do_confstr(const char *name, int key)
242 len = confstr(key, 0, 0);
245 err(EX_OSERR, "confstr: %s", name);
247 printf("undefined\n");
251 confstr(key, buf, len);
258 do_sysconf(const char *name, int key)
263 value = sysconf(key);
264 if (value == -1 && errno != 0)
265 err(EX_OSERR, "sysconf: %s", name);
266 else if (value == -1)
267 printf("undefined\n");
269 printf("%ld\n", value);
273 do_pathconf(const char *name, int key, const char *path)
278 value = pathconf(path, key);
279 if (value == -1 && errno != 0)
280 err(EX_OSERR, "pathconf: %s", name);
281 else if (value == -1)
282 printf("undefined\n");
284 printf("%ld\n", value);