1 /* $NetBSD: getconf.c,v 1.34 2008/08/27 08:56:49 christos Exp $ */
4 * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
34 __RCSID("$NetBSD: getconf.c,v 1.34 2008/08/27 08:56:49 christos Exp $");
49 enum { SYSCONF
, CONFSTR
, PATHCONF
, CONSTANT
} type
;
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) __dead2
;
58 static const struct conf_variable conf_table
[] =
61 { "PATH", CONFSTR
, _CS_PATH
},
64 /* Utility Limit Minimum Values */
65 #if defined(_POSIX2_BC_BASE_MAX)
66 { "POSIX2_BC_BASE_MAX", CONSTANT
, _POSIX2_BC_BASE_MAX
},
68 #if defined(_POSIX2_BC_DIM_MAX)
69 { "POSIX2_BC_DIM_MAX", CONSTANT
, _POSIX2_BC_DIM_MAX
},
71 #if defined(_POSIX2_BC_SCALE_MAX)
72 { "POSIX2_BC_SCALE_MAX", CONSTANT
, _POSIX2_BC_SCALE_MAX
},
74 #if defined(_POSIX2_BC_STRING_MAX)
75 { "POSIX2_BC_STRING_MAX", CONSTANT
, _POSIX2_BC_STRING_MAX
},
77 #if defined(_POSIX2_COLL_WEIGHTS_MAX)
78 { "POSIX2_COLL_WEIGHTS_MAX", CONSTANT
, _POSIX2_COLL_WEIGHTS_MAX
},
80 #if defined(_POSIX2_EXPR_NEST_MAX)
81 { "POSIX2_EXPR_NEST_MAX", CONSTANT
, _POSIX2_EXPR_NEST_MAX
},
83 #if defined(_POSIX2_LINE_MAX)
84 { "POSIX2_LINE_MAX", CONSTANT
, _POSIX2_LINE_MAX
},
86 #if defined(_POSIX2_RE_DUP_MAX)
87 { "POSIX2_RE_DUP_MAX", CONSTANT
, _POSIX2_RE_DUP_MAX
},
89 #if defined(_POSIX2_VERSION)
90 { "POSIX2_VERSION", CONSTANT
, _POSIX2_VERSION
},
93 /* POSIX.1 Minimum Values */
94 #if defined(_POSIX_AIO_LISTIO_MAX)
95 { "_POSIX_AIO_LISTIO_MAX", CONSTANT
, _POSIX_AIO_LISTIO_MAX
},
97 #if defined(_POSIX_AIO_MAX)
98 { "_POSIX_AIO_MAX", CONSTANT
, _POSIX_AIO_MAX
},
100 #if defined(_POSIX_ARG_MAX)
101 { "_POSIX_ARG_MAX", CONSTANT
, _POSIX_ARG_MAX
},
103 #if defined(_POSIX_CHILD_MAX)
104 { "_POSIX_CHILD_MAX", CONSTANT
, _POSIX_CHILD_MAX
},
106 #if defined(_POSIX_LINK_MAX)
107 { "_POSIX_LINK_MAX", CONSTANT
, _POSIX_LINK_MAX
},
109 #if defined(_POSIX_MAX_CANON)
110 { "_POSIX_MAX_CANON", CONSTANT
, _POSIX_MAX_CANON
},
112 #if defined(_POSIX_MAX_INPUT)
113 { "_POSIX_MAX_INPUT", CONSTANT
, _POSIX_MAX_INPUT
},
115 #if defined(_POSIX_MQ_OPEN_MAX)
116 { "_POSIX_MQ_OPEN_MAX", CONSTANT
, _POSIX_MQ_OPEN_MAX
},
118 #if defined(_POSIX_MQ_PRIO_MAX)
119 { "_POSIX_MQ_PRIO_MAX", CONSTANT
, _POSIX_MQ_PRIO_MAX
},
121 #if defined(_POSIX_NAME_MAX)
122 { "_POSIX_NAME_MAX", CONSTANT
, _POSIX_NAME_MAX
},
124 #if defined(_POSIX_NGROUPS_MAX)
125 { "_POSIX_NGROUPS_MAX", CONSTANT
, _POSIX_NGROUPS_MAX
},
127 #if defined(_POSIX_OPEN_MAX)
128 { "_POSIX_OPEN_MAX", CONSTANT
, _POSIX_OPEN_MAX
},
130 #if defined(_POSIX_PATH_MAX)
131 { "_POSIX_PATH_MAX", CONSTANT
, _POSIX_PATH_MAX
},
133 #if defined(_POSIX_PIPE_BUF)
134 { "_POSIX_PIPE_BUF", CONSTANT
, _POSIX_PIPE_BUF
},
136 #if defined(_POSIX_SSIZE_MAX)
137 { "_POSIX_SSIZE_MAX", CONSTANT
, _POSIX_SSIZE_MAX
},
139 #if defined(_POSIX_STREAM_MAX)
140 { "_POSIX_STREAM_MAX", CONSTANT
, _POSIX_STREAM_MAX
},
142 #if defined(_POSIX_TZNAME_MAX)
143 { "_POSIX_TZNAME_MAX", CONSTANT
, _POSIX_TZNAME_MAX
},
146 /* Symbolic Utility Limits */
147 #if defined(_SC_BC_BASE_MAX)
148 { "BC_BASE_MAX", SYSCONF
, _SC_BC_BASE_MAX
},
150 #if defined(_SC_BC_DIM_MAX)
151 { "BC_DIM_MAX", SYSCONF
, _SC_BC_DIM_MAX
},
153 #if defined(_SC_BC_SCALE_MAX)
154 { "BC_SCALE_MAX", SYSCONF
, _SC_BC_SCALE_MAX
},
156 #if defined(_SC_BC_STRING_MAX)
157 { "BC_STRING_MAX", SYSCONF
, _SC_BC_STRING_MAX
},
159 #if defined(_SC_COLL_WEIGHTS_MAX)
160 { "COLL_WEIGHTS_MAX", SYSCONF
, _SC_COLL_WEIGHTS_MAX
},
162 #if defined(_SC_EXPR_NEST_MAX)
163 { "EXPR_NEST_MAX", SYSCONF
, _SC_EXPR_NEST_MAX
},
165 #if defined(_SC_LINE_MAX)
166 { "LINE_MAX", SYSCONF
, _SC_LINE_MAX
},
168 #if defined(_SC_RE_DUP_MAX)
169 { "RE_DUP_MAX", SYSCONF
, _SC_RE_DUP_MAX
},
172 /* Optional Facility Configuration Values */
173 #if defined(_SC_2_C_BIND)
174 { "_POSIX2_C_BIND", SYSCONF
, _SC_2_C_BIND
},
176 #if defined(_SC_2_C_DEV)
177 { "POSIX2_C_DEV", SYSCONF
, _SC_2_C_DEV
},
179 #if defined(_SC_2_CHAR_TERM)
180 { "POSIX2_CHAR_TERM", SYSCONF
, _SC_2_CHAR_TERM
},
182 #if defined(_SC_2_FORT_DEV)
183 { "POSIX2_FORT_DEV", SYSCONF
, _SC_2_FORT_DEV
},
185 #if defined(_SC_2_FORT_RUN)
186 { "POSIX2_FORT_RUN", SYSCONF
, _SC_2_FORT_RUN
},
188 #if defined(_SC_2_LOCALEDEF)
189 { "POSIX2_LOCALEDEF", SYSCONF
, _SC_2_LOCALEDEF
},
191 #if defined(_SC_2_SW_DEV)
192 { "POSIX2_SW_DEV", SYSCONF
, _SC_2_SW_DEV
},
194 #if defined(_SC_2_UPE)
195 { "POSIX2_UPE", SYSCONF
, _SC_2_UPE
},
198 /* POSIX.1 Configurable System Variables */
199 #if defined(_SC_AIO_LISTIO_MAX)
200 { "AIO_LISTIO_MAX", SYSCONF
, _SC_AIO_LISTIO_MAX
},
202 #if defined(_SC_AIO_MAX)
203 { "AIO_MAX", SYSCONF
, _SC_AIO_MAX
},
205 #if defined(_SC_ARG_MAX)
206 { "ARG_MAX", SYSCONF
, _SC_ARG_MAX
},
208 #if defined(_SC_CHILD_MAX)
209 { "CHILD_MAX", SYSCONF
, _SC_CHILD_MAX
},
211 #if defined(_SC_CLK_TCK)
212 { "CLK_TCK", SYSCONF
, _SC_CLK_TCK
},
214 #if defined(_SC_MQ_OPEN_MAX)
215 { "MQ_OPEN_MAX", SYSCONF
, _SC_MQ_OPEN_MAX
},
217 #if defined(_SC_MQ_PRIO_MAX)
218 { "MQ_PRIO_MAX", SYSCONF
, _SC_MQ_PRIO_MAX
},
220 #if defined(_SC_NGROUPS_MAX)
221 { "NGROUPS_MAX", SYSCONF
, _SC_NGROUPS_MAX
},
223 #if defined(_SC_OPEN_MAX)
224 { "OPEN_MAX", SYSCONF
, _SC_OPEN_MAX
},
226 #if defined(_SC_STREAM_MAX)
227 { "STREAM_MAX", SYSCONF
, _SC_STREAM_MAX
},
229 #if defined(_SC_TZNAME_MAX)
230 { "TZNAME_MAX", SYSCONF
, _SC_TZNAME_MAX
},
232 #if defined(_SC_JOB_CONTROL)
233 { "_POSIX_JOB_CONTROL", SYSCONF
, _SC_JOB_CONTROL
},
235 #if defined(_SC_SAVED_IDS)
236 { "_POSIX_SAVED_IDS", SYSCONF
, _SC_SAVED_IDS
},
238 #if defined(_SC_VERSION)
239 { "_POSIX_VERSION", SYSCONF
, _SC_VERSION
},
242 #if defined(_PC_LINK_MAX)
243 { "LINK_MAX", PATHCONF
, _PC_LINK_MAX
},
245 #if defined(_PC_MAX_CANON)
246 { "MAX_CANON", PATHCONF
, _PC_MAX_CANON
},
248 #if defined(_PC_MAX_INPUT)
249 { "MAX_INPUT", PATHCONF
, _PC_MAX_INPUT
},
251 #if defined(_PC_NAME_MAX)
252 { "NAME_MAX", PATHCONF
, _PC_NAME_MAX
},
254 #if defined(_PC_PATH_MAX)
255 { "PATH_MAX", PATHCONF
, _PC_PATH_MAX
},
257 #if defined(_PC_PIPE_BUF)
258 { "PIPE_BUF", PATHCONF
, _PC_PIPE_BUF
},
260 #if defined(_PC_CHOWN_RESTRICTED)
261 { "_POSIX_CHOWN_RESTRICTED", PATHCONF
, _PC_CHOWN_RESTRICTED
},
263 #if defined(_PC_NO_TRUNC)
264 { "_POSIX_NO_TRUNC", PATHCONF
, _PC_NO_TRUNC
},
266 #if defined(_PC_VDISABLE)
267 { "_POSIX_VDISABLE", PATHCONF
, _PC_VDISABLE
},
270 /* POSIX.1b Configurable System Variables */
271 #if defined(_SC_PAGESIZE)
272 { "PAGESIZE", SYSCONF
, _SC_PAGESIZE
},
274 #if defined(_SC_ASYNCHRONOUS_IO)
275 { "_POSIX_ASYNCHRONOUS_IO", SYSCONF
, _SC_ASYNCHRONOUS_IO
},
277 #if defined(_SC_FSYNC)
278 { "_POSIX_FSYNC", SYSCONF
, _SC_FSYNC
},
280 #if defined(_SC_MAPPED_FILES)
281 { "_POSIX_MAPPED_FILES", SYSCONF
, _SC_MAPPED_FILES
},
283 #if defined(_SC_MEMLOCK)
284 { "_POSIX_MEMLOCK", SYSCONF
, _SC_MEMLOCK
},
286 #if defined(_SC_MEMLOCK_RANGE)
287 { "_POSIX_MEMLOCK_RANGE", SYSCONF
, _SC_MEMLOCK_RANGE
},
289 #if defined(_SC_MEMORY_PROTECTION)
290 { "_POSIX_MEMORY_PROTECTION", SYSCONF
, _SC_MEMORY_PROTECTION
},
292 #if defined(_SC_MESSAGE_PASSING)
293 { "_POSIX_MESSAGE_PASSING", SYSCONF
, _SC_MESSAGE_PASSING
},
295 #if defined(_SC_MONOTONIC_CLOCK)
296 { "_POSIX_MONOTONIC_CLOCK", SYSCONF
, _SC_MONOTONIC_CLOCK
},
298 #if defined(_SC_PRIORITY_SCHEDULING)
299 { "_POSIX_PRIORITY_SCHEDULING", SYSCONF
, _SC_PRIORITY_SCHEDULING
},
301 #if defined(_SC_SEMAPHORES)
302 { "_POSIX_SEMAPHORES", SYSCONF
, _SC_SEMAPHORES
},
304 #if defined(_SC_SYNCHRONIZED_IO)
305 { "_POSIX_SYNCHRONIZED_IO", SYSCONF
, _SC_SYNCHRONIZED_IO
},
307 #if defined(_SC_TIMERS)
308 { "_POSIX_TIMERS", SYSCONF
, _SC_TIMERS
},
311 #if defined(_PC_SYNC_IO)
312 { "_POSIX_SYNC_IO", PATHCONF
, _PC_SYNC_IO
},
315 /* POSIX.1c Configurable System Variables */
316 #if defined(_SC_LOGIN_NAME_MAX)
317 { "LOGIN_NAME_MAX", SYSCONF
, _SC_LOGIN_NAME_MAX
},
319 #if defined(_SC_THREADS)
320 { "_POSIX_THREADS", SYSCONF
, _SC_THREADS
},
323 /* POSIX.1j Configurable System Variables */
324 #if defined(_SC_BARRIERS)
325 { "_POSIX_BARRIERS", SYSCONF
, _SC_BARRIERS
},
327 #if defined(_SC_READER_WRITER_LOCKS)
328 { "_POSIX_READER_WRITER_LOCKS", SYSCONF
, _SC_READER_WRITER_LOCKS
},
330 #if defined(_SC_SPIN_LOCKS)
331 { "_POSIX_SPIN_LOCKS", SYSCONF
, _SC_SPIN_LOCKS
},
334 /* XPG4.2 Configurable System Variables */
335 #if defined(_SC_IOV_MAX)
336 { "IOV_MAX", SYSCONF
, _SC_IOV_MAX
},
338 #if defined(_SC_PAGE_SIZE)
339 { "PAGE_SIZE", SYSCONF
, _SC_PAGE_SIZE
},
341 #if defined(_SC_XOPEN_SHM)
342 { "_XOPEN_SHM", SYSCONF
, _SC_XOPEN_SHM
},
345 /* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */
346 #if defined(_PC_FILESIZEBITS)
347 { "FILESIZEBITS", PATHCONF
, _PC_FILESIZEBITS
},
350 /* POSIX.1-2001 XSI Option Group Configurable System Variables */
351 #if defined(_SC_ATEXIT_MAX)
352 { "ATEXIT_MAX", SYSCONF
, _SC_ATEXIT_MAX
},
355 /* POSIX.1-2001 TSF Configurable System Variables */
356 #if defined(_SC_GETGR_R_SIZE_MAX)
357 { "GETGR_R_SIZE_MAX", SYSCONF
, _SC_GETGR_R_SIZE_MAX
},
359 #if defined(_SC_GETPW_R_SIZE_MAX)
360 { "GETPW_R_SIZE_MAX", SYSCONF
, _SC_GETPW_R_SIZE_MAX
},
363 #ifdef _NETBSD_SOURCE
364 /* Commonly provided extensions */
365 { "NPROCESSORS_CONF", SYSCONF
, _SC_NPROCESSORS_CONF
},
366 { "NPROCESSORS_ONLN", SYSCONF
, _SC_NPROCESSORS_ONLN
},
367 #endif /* _NETBSD_SOURCE */
369 { NULL
, CONSTANT
, 0L }
372 static int a_flag
= 0; /* list all variables */
374 #if defined(__AROS__)
375 static size_t confstr(int name
, char *buf
, size_t len
)
383 main(int argc
, char **argv
)
386 const struct conf_variable
*cp
;
387 const char *varname
, *pathname
;
390 #if !defined(__AROS__)
391 setprogname(argv
[0]);
393 (void)setlocale(LC_ALL
, "");
395 while ((ch
= getopt(argc
, argv
, "a")) != -1) {
419 pathname
= argv
[0]; /* may be NULL */
422 for (cp
= conf_table
; cp
->name
!= NULL
; cp
++) {
423 if (a_flag
|| strcmp(varname
, cp
->name
) == 0) {
424 /*LINTED weird expression*/
425 if ((cp
->type
== PATHCONF
) == (pathname
!= NULL
)) {
426 printvar(cp
, pathname
);
430 "%s: invalid variable type", cp
->name
);
434 if (!a_flag
&& !found
)
435 errx(EXIT_FAILURE
, "%s: unknown variable", varname
);
437 (void)fflush(stdout
);
438 return ferror(stdout
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
442 print_longvar(const char *name
, long value
)
445 (void)printf("%s = %ld\n", name
, value
);
447 (void)printf("%ld\n", value
);
451 print_strvar(const char *name
, const char *sval
)
454 (void)printf("%s = %s\n", name
, sval
);
456 (void)printf("%s\n", sval
);
460 printvar(const struct conf_variable
*cp
, const char *pathname
)
468 print_longvar(cp
->name
, cp
->value
);
473 slen
= confstr((int)cp
->value
, NULL
, 0);
477 out
: err(EXIT_FAILURE
, "confstr(%ld)", cp
->value
);
479 print_strvar(cp
->name
, "undefined");
482 if ((sval
= malloc(slen
)) == NULL
)
483 err(EXIT_FAILURE
, "Can't allocate %zu bytes", slen
);
486 if (confstr((int)cp
->value
, sval
, slen
) == 0) {
490 print_strvar(cp
->name
, "undefined");
492 print_strvar(cp
->name
, sval
);
499 if ((val
= sysconf((int)cp
->value
)) == -1) {
501 err(EXIT_FAILURE
, "sysconf(%ld)", cp
->value
);
502 print_strvar(cp
->name
, "undefined");
504 print_longvar(cp
->name
, val
);
509 if ((val
= pathconf(pathname
, (int)cp
->value
)) == -1) {
511 if (a_flag
&& errno
== EINVAL
) {
512 /* Just skip invalid variables */
515 err(EXIT_FAILURE
, "pathconf(%s, %ld)",
516 pathname
, cp
->value
);
520 print_strvar(cp
->name
, "undefined");
522 print_longvar(cp
->name
, val
);
531 const char *p
= _getprogname();
532 (void)fprintf(stderr
, "Usage: %s system_var\n\t%s -a\n"
533 "\t%s path_var pathname\n\t%s -a pathname\n", p
, p
, p
, p
);