No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / getconf / getconf.c
blob11432658984cef15cf64696f48302ac8e93844a6
1 /* $NetBSD: getconf.c,v 1.33 2008/04/28 20:24:13 martin Exp $ */
3 /*-
4 * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by J.T. Conklin.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: getconf.c,v 1.33 2008/04/28 20:24:13 martin Exp $");
35 #endif /* not lint */
37 #include <err.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <locale.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
46 struct conf_variable
48 const char *name;
49 enum { SYSCONF, CONFSTR, PATHCONF, CONSTANT } type;
50 long value;
53 static void print_longvar(const char *, long);
54 static void print_strvar(const char *, const char *);
55 static void printvar(const struct conf_variable *, const char *);
56 static void usage(void) __dead;
58 static const struct conf_variable conf_table[] =
60 { "PATH", CONFSTR, _CS_PATH },
62 /* Utility Limit Minimum Values */
63 { "POSIX2_BC_BASE_MAX", CONSTANT, _POSIX2_BC_BASE_MAX },
64 { "POSIX2_BC_DIM_MAX", CONSTANT, _POSIX2_BC_DIM_MAX },
65 { "POSIX2_BC_SCALE_MAX", CONSTANT, _POSIX2_BC_SCALE_MAX },
66 { "POSIX2_BC_STRING_MAX", CONSTANT, _POSIX2_BC_STRING_MAX },
67 { "POSIX2_COLL_WEIGHTS_MAX", CONSTANT, _POSIX2_COLL_WEIGHTS_MAX },
68 { "POSIX2_EXPR_NEST_MAX", CONSTANT, _POSIX2_EXPR_NEST_MAX },
69 { "POSIX2_LINE_MAX", CONSTANT, _POSIX2_LINE_MAX },
70 { "POSIX2_RE_DUP_MAX", CONSTANT, _POSIX2_RE_DUP_MAX },
71 { "POSIX2_VERSION", CONSTANT, _POSIX2_VERSION },
73 /* POSIX.1 Minimum Values */
74 { "_POSIX_AIO_LISTIO_MAX", CONSTANT, _POSIX_AIO_LISTIO_MAX },
75 { "_POSIX_AIO_MAX", CONSTANT, _POSIX_AIO_MAX },
76 { "_POSIX_ARG_MAX", CONSTANT, _POSIX_ARG_MAX },
77 { "_POSIX_CHILD_MAX", CONSTANT, _POSIX_CHILD_MAX },
78 { "_POSIX_LINK_MAX", CONSTANT, _POSIX_LINK_MAX },
79 { "_POSIX_MAX_CANON", CONSTANT, _POSIX_MAX_CANON },
80 { "_POSIX_MAX_INPUT", CONSTANT, _POSIX_MAX_INPUT },
81 { "_POSIX_MQ_OPEN_MAX", CONSTANT, _POSIX_MQ_OPEN_MAX },
82 { "_POSIX_MQ_PRIO_MAX", CONSTANT, _POSIX_MQ_PRIO_MAX },
83 { "_POSIX_NAME_MAX", CONSTANT, _POSIX_NAME_MAX },
84 { "_POSIX_NGROUPS_MAX", CONSTANT, _POSIX_NGROUPS_MAX },
85 { "_POSIX_OPEN_MAX", CONSTANT, _POSIX_OPEN_MAX },
86 { "_POSIX_PATH_MAX", CONSTANT, _POSIX_PATH_MAX },
87 { "_POSIX_PIPE_BUF", CONSTANT, _POSIX_PIPE_BUF },
88 { "_POSIX_SSIZE_MAX", CONSTANT, _POSIX_SSIZE_MAX },
89 { "_POSIX_STREAM_MAX", CONSTANT, _POSIX_STREAM_MAX },
90 { "_POSIX_TZNAME_MAX", CONSTANT, _POSIX_TZNAME_MAX },
92 /* Symbolic Utility Limits */
93 { "BC_BASE_MAX", SYSCONF, _SC_BC_BASE_MAX },
94 { "BC_DIM_MAX", SYSCONF, _SC_BC_DIM_MAX },
95 { "BC_SCALE_MAX", SYSCONF, _SC_BC_SCALE_MAX },
96 { "BC_STRING_MAX", SYSCONF, _SC_BC_STRING_MAX },
97 { "COLL_WEIGHTS_MAX", SYSCONF, _SC_COLL_WEIGHTS_MAX },
98 { "EXPR_NEST_MAX", SYSCONF, _SC_EXPR_NEST_MAX },
99 { "LINE_MAX", SYSCONF, _SC_LINE_MAX },
100 { "RE_DUP_MAX", SYSCONF, _SC_RE_DUP_MAX },
102 /* Optional Facility Configuration Values */
103 { "_POSIX2_C_BIND", SYSCONF, _SC_2_C_BIND },
104 { "POSIX2_C_DEV", SYSCONF, _SC_2_C_DEV },
105 { "POSIX2_CHAR_TERM", SYSCONF, _SC_2_CHAR_TERM },
106 { "POSIX2_FORT_DEV", SYSCONF, _SC_2_FORT_DEV },
107 { "POSIX2_FORT_RUN", SYSCONF, _SC_2_FORT_RUN },
108 { "POSIX2_LOCALEDEF", SYSCONF, _SC_2_LOCALEDEF },
109 { "POSIX2_SW_DEV", SYSCONF, _SC_2_SW_DEV },
110 { "POSIX2_UPE", SYSCONF, _SC_2_UPE },
112 /* POSIX.1 Configurable System Variables */
113 { "AIO_LISTIO_MAX", SYSCONF, _SC_AIO_LISTIO_MAX },
114 { "AIO_MAX", SYSCONF, _SC_AIO_MAX },
115 { "ARG_MAX", SYSCONF, _SC_ARG_MAX },
116 { "CHILD_MAX", SYSCONF, _SC_CHILD_MAX },
117 { "CLK_TCK", SYSCONF, _SC_CLK_TCK },
118 { "MQ_OPEN_MAX", SYSCONF, _SC_MQ_OPEN_MAX },
119 { "MQ_PRIO_MAX", SYSCONF, _SC_MQ_PRIO_MAX },
120 { "NGROUPS_MAX", SYSCONF, _SC_NGROUPS_MAX },
121 { "OPEN_MAX", SYSCONF, _SC_OPEN_MAX },
122 { "STREAM_MAX", SYSCONF, _SC_STREAM_MAX },
123 { "TZNAME_MAX", SYSCONF, _SC_TZNAME_MAX },
124 { "_POSIX_JOB_CONTROL", SYSCONF, _SC_JOB_CONTROL },
125 { "_POSIX_SAVED_IDS", SYSCONF, _SC_SAVED_IDS },
126 { "_POSIX_VERSION", SYSCONF, _SC_VERSION },
128 { "LINK_MAX", PATHCONF, _PC_LINK_MAX },
129 { "MAX_CANON", PATHCONF, _PC_MAX_CANON },
130 { "MAX_INPUT", PATHCONF, _PC_MAX_INPUT },
131 { "NAME_MAX", PATHCONF, _PC_NAME_MAX },
132 { "PATH_MAX", PATHCONF, _PC_PATH_MAX },
133 { "PIPE_BUF", PATHCONF, _PC_PIPE_BUF },
134 { "_POSIX_CHOWN_RESTRICTED", PATHCONF, _PC_CHOWN_RESTRICTED },
135 { "_POSIX_NO_TRUNC", PATHCONF, _PC_NO_TRUNC },
136 { "_POSIX_VDISABLE", PATHCONF, _PC_VDISABLE },
138 /* POSIX.1b Configurable System Variables */
139 { "PAGESIZE", SYSCONF, _SC_PAGESIZE },
140 { "_POSIX_ASYNCHRONOUS_IO", SYSCONF, _SC_ASYNCHRONOUS_IO },
141 { "_POSIX_FSYNC", SYSCONF, _SC_FSYNC },
142 { "_POSIX_MAPPED_FILES", SYSCONF, _SC_MAPPED_FILES },
143 { "_POSIX_MEMLOCK", SYSCONF, _SC_MEMLOCK },
144 { "_POSIX_MEMLOCK_RANGE", SYSCONF, _SC_MEMLOCK_RANGE },
145 { "_POSIX_MEMORY_PROTECTION", SYSCONF, _SC_MEMORY_PROTECTION },
146 { "_POSIX_MESSAGE_PASSING", SYSCONF, _SC_MESSAGE_PASSING },
147 { "_POSIX_MONOTONIC_CLOCK", SYSCONF, _SC_MONOTONIC_CLOCK },
148 { "_POSIX_PRIORITY_SCHEDULING", SYSCONF, _SC_PRIORITY_SCHEDULING },
149 { "_POSIX_SEMAPHORES", SYSCONF, _SC_SEMAPHORES },
150 { "_POSIX_SYNCHRONIZED_IO", SYSCONF, _SC_SYNCHRONIZED_IO },
151 { "_POSIX_TIMERS", SYSCONF, _SC_TIMERS },
153 { "_POSIX_SYNC_IO", PATHCONF, _PC_SYNC_IO },
155 /* POSIX.1c Configurable System Variables */
156 { "LOGIN_NAME_MAX", SYSCONF, _SC_LOGIN_NAME_MAX },
157 { "_POSIX_THREADS", SYSCONF, _SC_THREADS },
159 /* POSIX.1j Configurable System Variables */
160 { "_POSIX_BARRIERS", SYSCONF, _SC_BARRIERS },
161 { "_POSIX_READER_WRITER_LOCKS", SYSCONF, _SC_READER_WRITER_LOCKS },
162 { "_POSIX_SPIN_LOCKS", SYSCONF, _SC_SPIN_LOCKS },
164 /* XPG4.2 Configurable System Variables */
165 { "IOV_MAX", SYSCONF, _SC_IOV_MAX },
166 { "PAGE_SIZE", SYSCONF, _SC_PAGE_SIZE },
167 { "_XOPEN_SHM", SYSCONF, _SC_XOPEN_SHM },
169 /* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */
170 { "FILESIZEBITS", PATHCONF, _PC_FILESIZEBITS },
172 /* POSIX.1-2001 XSI Option Group Configurable System Variables */
173 { "ATEXIT_MAX", SYSCONF, _SC_ATEXIT_MAX },
175 /* POSIX.1-2001 TSF Configurable System Variables */
176 { "GETGR_R_SIZE_MAX", SYSCONF, _SC_GETGR_R_SIZE_MAX },
177 { "GETPW_R_SIZE_MAX", SYSCONF, _SC_GETPW_R_SIZE_MAX },
179 #ifdef _NETBSD_SOURCE
180 /* Commonly provided extensions */
181 { "NPROCESSORS_CONF", SYSCONF, _SC_NPROCESSORS_CONF },
182 { "NPROCESSORS_ONLN", SYSCONF, _SC_NPROCESSORS_ONLN },
183 #endif /* _NETBSD_SOURCE */
185 { NULL, CONSTANT, 0L }
188 static int a_flag = 0; /* list all variables */
191 main(int argc, char **argv)
193 int ch;
194 const struct conf_variable *cp;
195 const char *varname, *pathname;
196 int found;
198 setprogname(argv[0]);
199 (void)setlocale(LC_ALL, "");
201 while ((ch = getopt(argc, argv, "a")) != -1) {
202 switch (ch) {
203 case 'a':
204 a_flag = 1;
205 break;
206 case '?':
207 default:
208 usage();
211 argc -= optind;
212 argv += optind;
214 if (!a_flag) {
215 if (argc == 0)
216 usage();
217 varname = argv[0];
218 argc--;
219 argv++;
220 } else
221 varname = NULL;
223 if (argc > 1)
224 usage();
225 pathname = argv[0]; /* may be NULL */
227 found = 0;
228 for (cp = conf_table; cp->name != NULL; cp++) {
229 if (a_flag || strcmp(varname, cp->name) == 0) {
230 /*LINTED weird expression*/
231 if ((cp->type == PATHCONF) == (pathname != NULL)) {
232 printvar(cp, pathname);
233 found = 1;
234 } else if (!a_flag)
235 errx(EXIT_FAILURE,
236 "%s: invalid variable type", cp->name);
240 if (!a_flag && !found)
241 errx(EXIT_FAILURE, "%s: unknown variable", varname);
243 (void)fflush(stdout);
244 return ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS;
247 static void
248 print_longvar(const char *name, long value)
250 if (a_flag)
251 (void)printf("%s = %ld\n", name, value);
252 else
253 (void)printf("%ld\n", value);
256 static void
257 print_strvar(const char *name, const char *sval)
259 if (a_flag)
260 (void)printf("%s = %s\n", name, sval);
261 else
262 (void)printf("%s\n", sval);
265 static void
266 printvar(const struct conf_variable *cp, const char *pathname)
268 size_t slen;
269 char *sval;
270 long val;
272 switch (cp->type) {
273 case CONSTANT:
274 print_longvar(cp->name, cp->value);
275 break;
277 case CONFSTR:
278 errno = 0;
279 slen = confstr((int)cp->value, NULL, 0);
280 if (slen == 0) {
281 if (errno != 0)
283 out: err(EXIT_FAILURE, "confstr(%ld)", cp->value);
284 else
285 print_strvar(cp->name, "undefined");
288 if ((sval = malloc(slen)) == NULL)
289 err(EXIT_FAILURE, "Can't allocate %zu bytes", slen);
291 errno = 0;
292 if (confstr((int)cp->value, sval, slen) == 0) {
293 if (errno != 0)
294 goto out;
295 else
296 print_strvar(cp->name, "undefined");
297 } else
298 print_strvar(cp->name, sval);
300 free(sval);
301 break;
303 case SYSCONF:
304 errno = 0;
305 if ((val = sysconf((int)cp->value)) == -1) {
306 if (errno != 0)
307 err(EXIT_FAILURE, "sysconf(%ld)", cp->value);
308 print_strvar(cp->name, "undefined");
309 } else
310 print_longvar(cp->name, val);
311 break;
313 case PATHCONF:
314 errno = 0;
315 if ((val = pathconf(pathname, (int)cp->value)) == -1) {
316 if (errno != 0) {
317 if (a_flag && errno == EINVAL) {
318 /* Just skip invalid variables */
319 return;
321 err(EXIT_FAILURE, "pathconf(%s, %ld)",
322 pathname, cp->value);
323 /* NOTREACHED */
326 print_strvar(cp->name, "undefined");
327 } else
328 print_longvar(cp->name, val);
329 break;
334 static void
335 usage(void)
337 const char *p = getprogname();
338 (void)fprintf(stderr, "Usage: %s system_var\n\t%s -a\n"
339 "\t%s path_var pathname\n\t%s -a pathname\n", p, p, p, p);
340 exit(EXIT_FAILURE);