xref: /openbsd/usr.bin/getconf/getconf.c (revision d7259957)
1*d7259957Scheloha /*	$OpenBSD: getconf.c,v 1.23 2022/12/04 23:50:48 cheloha Exp $	*/
21258a77dSderaadt 
33b665a45Sderaadt /*-
43b665a45Sderaadt  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5df930be7Sderaadt  * All rights reserved.
6df930be7Sderaadt  *
73b665a45Sderaadt  * This code is derived from software contributed to The NetBSD Foundation
83b665a45Sderaadt  * by J.T. Conklin.
93b665a45Sderaadt  *
10df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
11df930be7Sderaadt  * modification, are permitted provided that the following conditions
12df930be7Sderaadt  * are met:
13df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
15df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
16df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
17df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
18df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
19df930be7Sderaadt  *    must display the following acknowledgement:
20df930be7Sderaadt  *      This product includes software developed by Winning Strategies, Inc.
21df930be7Sderaadt  * 4. The name of the author may not be used to endorse or promote products
22df930be7Sderaadt  *    derived from this software without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25df930be7Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26df930be7Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27df930be7Sderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28df930be7Sderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29df930be7Sderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30df930be7Sderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31df930be7Sderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32df930be7Sderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33df930be7Sderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34df930be7Sderaadt  */
35df930be7Sderaadt 
36df930be7Sderaadt /*
37df930be7Sderaadt  * POSIX.2 getconf utility
38df930be7Sderaadt  *
39df930be7Sderaadt  * Written by:
40df930be7Sderaadt  *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
41df930be7Sderaadt  */
42df930be7Sderaadt 
4384d7e2a2Sschwarze #include <err.h>
4484d7e2a2Sschwarze #include <errno.h>
4584d7e2a2Sschwarze #include <limits.h>
46df930be7Sderaadt #include <stdio.h>
47df930be7Sderaadt #include <stdlib.h>
4870ef01f6Sdavid #include <string.h>
49df930be7Sderaadt #include <unistd.h>
50df930be7Sderaadt 
5184d7e2a2Sschwarze static void __dead usage(void);
5259d1056eSguenther static void list_var(int);
5359d1056eSguenther static int compilation_spec_valid(const char *);
54df930be7Sderaadt 
55df930be7Sderaadt struct conf_variable
56df930be7Sderaadt {
57df930be7Sderaadt   const char *name;
58df930be7Sderaadt   enum { SYSCONF, CONFSTR, PATHCONF, CONSTANT } type;
59df930be7Sderaadt   long value;
60df930be7Sderaadt };
61df930be7Sderaadt 
6259d1056eSguenther 
6359d1056eSguenther #define constant_row(name)		 { #name, CONSTANT, name },
6459d1056eSguenther #define sysconf_row(name)		 { #name, SYSCONF,  _SC_##name },
6559d1056eSguenther #define pathconf_row(name)		 { #name, PATHCONF, _PC_##name },
6659d1056eSguenther #define confstr_row(name)		 { #name, CONFSTR,  _CS_##name },
6759d1056eSguenther #define posix_constant_row(name)	 { #name, CONSTANT, _POSIX_##name },
6859d1056eSguenther #define posix_confstr_row(name)		 { #name, CONFSTR,  _CS_POSIX_##name },
6959d1056eSguenther #define compat_posix2_sysconf_row(name)	 { #name, SYSCONF,  _SC_2_##name },
7059d1056eSguenther #define compat_posix2_constant_row(name) { #name, CONSTANT, _POSIX2_##name },
7159d1056eSguenther 
7259d1056eSguenther /* Some sysconf variables don't follow the pattern of the others */
7359d1056eSguenther #define posix2_sysconf_row(name) \
7459d1056eSguenther 			{ "_POSIX2_" #name, SYSCONF,  _SC_2_##name },
7559d1056eSguenther #define posix2_pathconf_row(name) \
7659d1056eSguenther 			{ "_POSIX2_" #name, PATHCONF,  _PC_2_##name },
7759d1056eSguenther #define pthread_sysconf_row(name) \
7859d1056eSguenther 			{ "_PTHREAD_" #name, SYSCONF,  _SC_THREAD_##name },
7959d1056eSguenther #define xopen_sysconf_row(name) \
8059d1056eSguenther 			{ "_XOPEN_" #name, SYSCONF,  _SC_XOPEN_##name },
8159d1056eSguenther 
82df930be7Sderaadt const struct conf_variable conf_table[] =
83df930be7Sderaadt {
8459d1056eSguenther   /* Configuration strings */
8559d1056eSguenther   confstr_row(PATH)
8659d1056eSguenther   confstr_row(V7_ENV)
8759d1056eSguenther   confstr_row(V6_ENV)
88df930be7Sderaadt 
89df930be7Sderaadt   /* Symbolic Utility Limits */
9059d1056eSguenther   sysconf_row(BC_BASE_MAX)
9159d1056eSguenther   sysconf_row(BC_DIM_MAX)
9259d1056eSguenther   sysconf_row(BC_SCALE_MAX)
9359d1056eSguenther   sysconf_row(BC_STRING_MAX)
9459d1056eSguenther   sysconf_row(COLL_WEIGHTS_MAX)
9559d1056eSguenther   sysconf_row(EXPR_NEST_MAX)
9659d1056eSguenther   sysconf_row(LINE_MAX)
9759d1056eSguenther   sysconf_row(RE_DUP_MAX)
98df930be7Sderaadt 
99df930be7Sderaadt   /* POSIX.1 Configurable System Variables */
10059d1056eSguenther   sysconf_row(AIO_LISTIO_MAX)
10159d1056eSguenther   sysconf_row(AIO_MAX)
10259d1056eSguenther   sysconf_row(AIO_PRIO_DELTA_MAX)
10359d1056eSguenther   sysconf_row(ARG_MAX)
10459d1056eSguenther   sysconf_row(CHILD_MAX)
10559d1056eSguenther   sysconf_row(CLK_TCK)
10659d1056eSguenther   sysconf_row(NGROUPS_MAX)
10759d1056eSguenther   sysconf_row(OPEN_MAX)
10859d1056eSguenther   sysconf_row(STREAM_MAX)
10959d1056eSguenther   sysconf_row(TZNAME_MAX)
11059d1056eSguenther   sysconf_row(PAGE_SIZE)
11159d1056eSguenther   sysconf_row(PAGESIZE)
112df930be7Sderaadt 
11359d1056eSguenther   sysconf_row(SEM_NSEMS_MAX)
11459d1056eSguenther   sysconf_row(SEM_VALUE_MAX)
11559d1056eSguenther   sysconf_row(HOST_NAME_MAX)
11659d1056eSguenther   sysconf_row(LOGIN_NAME_MAX)
11759d1056eSguenther 
11859d1056eSguenther   sysconf_row(ATEXIT_MAX)
11959d1056eSguenther   sysconf_row(DELAYTIMER_MAX)
12059d1056eSguenther   sysconf_row(IOV_MAX)
12159d1056eSguenther   sysconf_row(MQ_OPEN_MAX)
12259d1056eSguenther   sysconf_row(MQ_PRIO_MAX)
12359d1056eSguenther   sysconf_row(RTSIG_MAX)
12459d1056eSguenther   sysconf_row(SIGQUEUE_MAX)
12559d1056eSguenther   sysconf_row(SYMLOOP_MAX)
12659d1056eSguenther   sysconf_row(TIMER_MAX)
12759d1056eSguenther   sysconf_row(TTY_NAME_MAX)
12859d1056eSguenther 
12959d1056eSguenther   posix2_sysconf_row(PBS)
13059d1056eSguenther   posix2_sysconf_row(PBS_ACCOUNTING)
13159d1056eSguenther   posix2_sysconf_row(PBS_CHECKPOINT)
13259d1056eSguenther   posix2_sysconf_row(PBS_LOCATE)
13359d1056eSguenther   posix2_sysconf_row(PBS_MESSAGE)
13459d1056eSguenther   posix2_sysconf_row(PBS_TRACK)
13559d1056eSguenther 
13659d1056eSguenther   pthread_sysconf_row(DESTRUCTOR_ITERATIONS)
13759d1056eSguenther   pthread_sysconf_row(KEYS_MAX)
13859d1056eSguenther   pthread_sysconf_row(STACK_MIN)
13959d1056eSguenther   pthread_sysconf_row(THREADS_MAX)
14059d1056eSguenther 
14159d1056eSguenther   xopen_sysconf_row(SHM)
14259d1056eSguenther   xopen_sysconf_row(CRYPT)
14359d1056eSguenther   xopen_sysconf_row(ENH_I18N)
14459d1056eSguenther   xopen_sysconf_row(REALTIME)
14559d1056eSguenther   xopen_sysconf_row(REALTIME_THREADS)
14659d1056eSguenther   xopen_sysconf_row(STREAMS)
14759d1056eSguenther   xopen_sysconf_row(UNIX)
14859d1056eSguenther   xopen_sysconf_row(UUCP)
14959d1056eSguenther   xopen_sysconf_row(VERSION)
15059d1056eSguenther 
15159d1056eSguenther   pathconf_row(FILESIZEBITS)
15259d1056eSguenther   pathconf_row(LINK_MAX)
15359d1056eSguenther   pathconf_row(MAX_CANON)
15459d1056eSguenther   pathconf_row(MAX_INPUT)
15559d1056eSguenther   pathconf_row(NAME_MAX)
15659d1056eSguenther   pathconf_row(PATH_MAX)
15759d1056eSguenther   pathconf_row(PIPE_BUF)
15859d1056eSguenther   pathconf_row(SYMLINK_MAX)
15959d1056eSguenther 
16059d1056eSguenther   posix2_pathconf_row(SYMLINKS)
16159d1056eSguenther 
16259d1056eSguenther   constant_row(_POSIX2_CHARCLASS_NAME_MAX)
16359d1056eSguenther   constant_row(_XOPEN_IOV_MAX)
16459d1056eSguenther   constant_row(_XOPEN_NAME_MAX)
16559d1056eSguenther   constant_row(_XOPEN_PATH_MAX)
166df930be7Sderaadt 
1672a63b049Smillert   /* Extensions */
1682a63b049Smillert   sysconf_row(PHYS_PAGES)
1692a63b049Smillert   sysconf_row(AVPHYS_PAGES)
1702a63b049Smillert   sysconf_row(NPROCESSORS_CONF)
1712a63b049Smillert   sysconf_row(NPROCESSORS_ONLN)
1722a63b049Smillert 
173df930be7Sderaadt   { NULL }
174df930be7Sderaadt };
175df930be7Sderaadt 
17659d1056eSguenther /*
17759d1056eSguenther  * Lots of names have a leading "_POSIX_", so put them in a table with
17859d1056eSguenther  * that prefix trimmed
17959d1056eSguenther  */
18059d1056eSguenther const char uposix_prefix[] = "_POSIX_";
18159d1056eSguenther const struct conf_variable uposix_conf_table[] =
18259d1056eSguenther {
18359d1056eSguenther   /* POSIX.1 Maximum Values */
18459d1056eSguenther   posix_constant_row(CLOCKRES_MIN)
18559d1056eSguenther 
18659d1056eSguenther   /* POSIX.1 Minimum Values */
18759d1056eSguenther   /*posix_constant_row(AIO_LISTIO_MAX)*/
18859d1056eSguenther   /*posix_constant_row(AIO_MAX)*/
18959d1056eSguenther   posix_constant_row(ARG_MAX)
19059d1056eSguenther   posix_constant_row(CHILD_MAX)
19159d1056eSguenther   /*posix_constant_row(DELAYTIMER_MAX)*/
19259d1056eSguenther   posix_constant_row(HOST_NAME_MAX)
19359d1056eSguenther   posix_constant_row(LINK_MAX)
19459d1056eSguenther   posix_constant_row(LOGIN_NAME_MAX)
19559d1056eSguenther   posix_constant_row(MAX_CANON)
19659d1056eSguenther   posix_constant_row(MAX_INPUT)
19759d1056eSguenther   /*posix_constant_row(MQ_OPEN_MAX)*/
19859d1056eSguenther   /*posix_constant_row(MQ_PRIO_MAX)*/
19959d1056eSguenther   posix_constant_row(NAME_MAX)
20059d1056eSguenther   posix_constant_row(NGROUPS_MAX)
20159d1056eSguenther   posix_constant_row(OPEN_MAX)
20259d1056eSguenther   posix_constant_row(PATH_MAX)
20359d1056eSguenther   posix_constant_row(PIPE_BUF)
20459d1056eSguenther   posix_constant_row(RE_DUP_MAX)
20559d1056eSguenther   /*posix_constant_row(RTSIG_MAX)*/
20659d1056eSguenther   posix_constant_row(SEM_NSEMS_MAX)
20759d1056eSguenther   posix_constant_row(SEM_VALUE_MAX)
20859d1056eSguenther   /*posix_constant_row(SIGQUEUE_MAX)*/
20959d1056eSguenther   posix_constant_row(SSIZE_MAX)
21059d1056eSguenther   /*posix_constant_row(SS_REPL_MAX)*/
21159d1056eSguenther   posix_constant_row(STREAM_MAX)
21259d1056eSguenther   posix_constant_row(SYMLINK_MAX)
21359d1056eSguenther   posix_constant_row(SYMLOOP_MAX)
21459d1056eSguenther   posix_constant_row(THREAD_DESTRUCTOR_ITERATIONS)
21559d1056eSguenther   posix_constant_row(THREAD_KEYS_MAX)
21659d1056eSguenther   posix_constant_row(THREAD_THREADS_MAX)
21759d1056eSguenther   /*posix_constant_row(TIMER_MAX)*/
21859d1056eSguenther   posix_constant_row(TTY_NAME_MAX)
21959d1056eSguenther   posix_constant_row(TZNAME_MAX)
22059d1056eSguenther 
22159d1056eSguenther   /* POSIX.1 Configurable System Variables */
22259d1056eSguenther   sysconf_row(JOB_CONTROL)
22359d1056eSguenther   sysconf_row(SAVED_IDS)
22459d1056eSguenther   sysconf_row(VERSION)
22559d1056eSguenther   sysconf_row(FSYNC)
22659d1056eSguenther   sysconf_row(MONOTONIC_CLOCK)
22759d1056eSguenther   sysconf_row(THREAD_SAFE_FUNCTIONS)
22859d1056eSguenther   sysconf_row(ADVISORY_INFO)
22959d1056eSguenther   sysconf_row(BARRIERS)
23059d1056eSguenther   sysconf_row(ASYNCHRONOUS_IO)
23159d1056eSguenther   sysconf_row(CLOCK_SELECTION)
23259d1056eSguenther   sysconf_row(CPUTIME)
23359d1056eSguenther   sysconf_row(IPV6)
23459d1056eSguenther   sysconf_row(MAPPED_FILES)
23559d1056eSguenther   sysconf_row(MEMLOCK)
23659d1056eSguenther   sysconf_row(MEMLOCK_RANGE)
23759d1056eSguenther   sysconf_row(MEMORY_PROTECTION)
23859d1056eSguenther   sysconf_row(MESSAGE_PASSING)
23959d1056eSguenther   sysconf_row(PRIORITIZED_IO)
24059d1056eSguenther   sysconf_row(PRIORITY_SCHEDULING)
24159d1056eSguenther   sysconf_row(RAW_SOCKETS)
24259d1056eSguenther   sysconf_row(READER_WRITER_LOCKS)
24359d1056eSguenther   sysconf_row(REALTIME_SIGNALS)
24459d1056eSguenther   sysconf_row(REGEXP)
24559d1056eSguenther   sysconf_row(SEMAPHORES)
24659d1056eSguenther   sysconf_row(SHARED_MEMORY_OBJECTS)
24759d1056eSguenther   sysconf_row(SHELL)
24859d1056eSguenther   sysconf_row(SPAWN)
24959d1056eSguenther   sysconf_row(SPIN_LOCKS)
25059d1056eSguenther   sysconf_row(SPORADIC_SERVER)
25159d1056eSguenther   sysconf_row(SS_REPL_MAX)
25259d1056eSguenther   sysconf_row(SYNCHRONIZED_IO)
25359d1056eSguenther   sysconf_row(THREAD_ATTR_STACKADDR)
25459d1056eSguenther   sysconf_row(THREAD_ATTR_STACKSIZE)
25559d1056eSguenther   sysconf_row(THREAD_CPUTIME)
25659d1056eSguenther   sysconf_row(THREAD_PRIO_INHERIT)
25759d1056eSguenther   sysconf_row(THREAD_PRIO_PROTECT)
25859d1056eSguenther   sysconf_row(THREAD_PRIORITY_SCHEDULING)
25959d1056eSguenther   sysconf_row(THREAD_PROCESS_SHARED)
26059d1056eSguenther   sysconf_row(THREAD_ROBUST_PRIO_INHERIT)
26159d1056eSguenther   sysconf_row(THREAD_SPORADIC_SERVER)
26259d1056eSguenther   sysconf_row(THREADS)
26359d1056eSguenther   sysconf_row(TIMEOUTS)
26459d1056eSguenther   sysconf_row(TIMERS)
26559d1056eSguenther   sysconf_row(TRACE)
26659d1056eSguenther   sysconf_row(TRACE_EVENT_FILTER)
26759d1056eSguenther   sysconf_row(TRACE_EVENT_NAME_MAX)
26859d1056eSguenther   sysconf_row(TRACE_INHERIT)
26959d1056eSguenther   sysconf_row(TRACE_LOG)
27059d1056eSguenther   sysconf_row(TRACE_NAME_MAX)
27159d1056eSguenther   sysconf_row(TRACE_SYS_MAX)
27259d1056eSguenther   sysconf_row(TRACE_USER_EVENT_MAX)
27359d1056eSguenther   sysconf_row(TYPED_MEMORY_OBJECTS)
27459d1056eSguenther 
27559d1056eSguenther   /*
27659d1056eSguenther    * If new compilation specification are added (V8_*?) then add them
27759d1056eSguenther    * to the compilation_specs array below too
27859d1056eSguenther    */
27959d1056eSguenther   sysconf_row(V7_ILP32_OFF32)
28059d1056eSguenther   sysconf_row(V7_ILP32_OFFBIG)
28159d1056eSguenther   sysconf_row(V7_LP64_OFF64)
28259d1056eSguenther   sysconf_row(V7_LPBIG_OFFBIG)
28359d1056eSguenther   sysconf_row(V6_ILP32_OFF32)
28459d1056eSguenther   sysconf_row(V6_ILP32_OFFBIG)
28559d1056eSguenther   sysconf_row(V6_LP64_OFF64)
28659d1056eSguenther   sysconf_row(V6_LPBIG_OFFBIG)
28759d1056eSguenther 
28859d1056eSguenther   /* POSIX.1 Configurable Path Variables */
28959d1056eSguenther   pathconf_row(CHOWN_RESTRICTED)
29059d1056eSguenther   pathconf_row(NO_TRUNC)
29159d1056eSguenther   pathconf_row(VDISABLE)
29259d1056eSguenther   pathconf_row(ASYNC_IO)
29359d1056eSguenther   pathconf_row(PRIO_IO)
29459d1056eSguenther   pathconf_row(SYNC_IO)
295d4648cd6Sguenther   pathconf_row(TIMESTAMP_RESOLUTION)
29659d1056eSguenther 
29759d1056eSguenther   { NULL }
29859d1056eSguenther };
29959d1056eSguenther 
30059d1056eSguenther /*
30159d1056eSguenther  * Then there are the "POSIX_*" values
30259d1056eSguenther  */
30359d1056eSguenther const char posix_prefix[] = "POSIX_";
30459d1056eSguenther const struct conf_variable posix_conf_table[] =
30559d1056eSguenther {
30659d1056eSguenther   pathconf_row(ALLOC_SIZE_MIN)
30759d1056eSguenther   pathconf_row(REC_INCR_XFER_SIZE)
30859d1056eSguenther   pathconf_row(REC_MAX_XFER_SIZE)
30959d1056eSguenther   pathconf_row(REC_MIN_XFER_SIZE)
31059d1056eSguenther   pathconf_row(REC_XFER_ALIGN)
31159d1056eSguenther 
31259d1056eSguenther   posix_confstr_row(V7_ILP32_OFF32_CFLAGS)
31359d1056eSguenther   posix_confstr_row(V7_ILP32_OFF32_LDFLAGS)
31459d1056eSguenther   posix_confstr_row(V7_ILP32_OFF32_LIBS)
31559d1056eSguenther   posix_confstr_row(V7_ILP32_OFFBIG_CFLAGS)
31659d1056eSguenther   posix_confstr_row(V7_ILP32_OFFBIG_LDFLAGS)
31759d1056eSguenther   posix_confstr_row(V7_ILP32_OFFBIG_LIBS)
31859d1056eSguenther   posix_confstr_row(V7_LP64_OFF64_CFLAGS)
31959d1056eSguenther   posix_confstr_row(V7_LP64_OFF64_LDFLAGS)
32059d1056eSguenther   posix_confstr_row(V7_LP64_OFF64_LIBS)
32159d1056eSguenther   posix_confstr_row(V7_LPBIG_OFFBIG_CFLAGS)
32259d1056eSguenther   posix_confstr_row(V7_LPBIG_OFFBIG_LDFLAGS)
32359d1056eSguenther   posix_confstr_row(V7_LPBIG_OFFBIG_LIBS)
32459d1056eSguenther   posix_confstr_row(V7_THREADS_CFLAGS)
32559d1056eSguenther   posix_confstr_row(V7_THREADS_LDFLAGS)
32659d1056eSguenther   posix_confstr_row(V7_WIDTH_RESTRICTED_ENVS)
32759d1056eSguenther   posix_confstr_row(V6_ILP32_OFF32_CFLAGS)
32859d1056eSguenther   posix_confstr_row(V6_ILP32_OFF32_LDFLAGS)
32959d1056eSguenther   posix_confstr_row(V6_ILP32_OFF32_LIBS)
33059d1056eSguenther   posix_confstr_row(V6_ILP32_OFFBIG_CFLAGS)
33159d1056eSguenther   posix_confstr_row(V6_ILP32_OFFBIG_LDFLAGS)
33259d1056eSguenther   posix_confstr_row(V6_ILP32_OFFBIG_LIBS)
33359d1056eSguenther   posix_confstr_row(V6_LP64_OFF64_CFLAGS)
33459d1056eSguenther   posix_confstr_row(V6_LP64_OFF64_LDFLAGS)
33559d1056eSguenther   posix_confstr_row(V6_LP64_OFF64_LIBS)
33659d1056eSguenther   posix_confstr_row(V6_LPBIG_OFFBIG_CFLAGS)
33759d1056eSguenther   posix_confstr_row(V6_LPBIG_OFFBIG_LDFLAGS)
33859d1056eSguenther   posix_confstr_row(V6_LPBIG_OFFBIG_LIBS)
33959d1056eSguenther   posix_confstr_row(V6_WIDTH_RESTRICTED_ENVS)
34059d1056eSguenther 
34159d1056eSguenther   { NULL }
34259d1056eSguenther };
34359d1056eSguenther 
34459d1056eSguenther /*
34559d1056eSguenther  * Finally, there are variables that are accepted with a prefix
34659d1056eSguenther  * of either "_POSIX2_" or "POSIX2_"
34759d1056eSguenther  */
34859d1056eSguenther const char compat_posix2_prefix[] = "POSIX2_";
34959d1056eSguenther const struct conf_variable compat_posix2_conf_table[] =
35059d1056eSguenther {
35159d1056eSguenther   /* Optional Facility Configuration Values */
35259d1056eSguenther   compat_posix2_sysconf_row(VERSION)
35359d1056eSguenther   compat_posix2_sysconf_row(C_BIND)
35459d1056eSguenther   compat_posix2_sysconf_row(C_DEV)
35559d1056eSguenther   compat_posix2_sysconf_row(CHAR_TERM)
35659d1056eSguenther   compat_posix2_sysconf_row(FORT_DEV)
35759d1056eSguenther   compat_posix2_sysconf_row(FORT_RUN)
35859d1056eSguenther   compat_posix2_sysconf_row(LOCALEDEF)
35959d1056eSguenther   compat_posix2_sysconf_row(SW_DEV)
36059d1056eSguenther   compat_posix2_sysconf_row(UPE)
36159d1056eSguenther 
36259d1056eSguenther   /* Utility Limit Minimum Values */
36359d1056eSguenther   compat_posix2_constant_row(BC_BASE_MAX)
36459d1056eSguenther   compat_posix2_constant_row(BC_DIM_MAX)
36559d1056eSguenther   compat_posix2_constant_row(BC_SCALE_MAX)
36659d1056eSguenther   compat_posix2_constant_row(BC_STRING_MAX)
36759d1056eSguenther   compat_posix2_constant_row(COLL_WEIGHTS_MAX)
36859d1056eSguenther   compat_posix2_constant_row(EXPR_NEST_MAX)
36959d1056eSguenther   compat_posix2_constant_row(LINE_MAX)
37059d1056eSguenther   compat_posix2_constant_row(RE_DUP_MAX)
37159d1056eSguenther 
37259d1056eSguenther   { NULL }
37359d1056eSguenther };
37459d1056eSguenther 
37559d1056eSguenther #undef constant_row
37659d1056eSguenther #undef sysconf_row
37759d1056eSguenther #undef pathconf_row
37859d1056eSguenther #undef confstr_row
37959d1056eSguenther #undef posix_constant_row
38059d1056eSguenther #undef posix_confstr_row
38159d1056eSguenther #undef compat_posix2_sysconf_row
38259d1056eSguenther #undef compat_posix2_constant_row
38359d1056eSguenther 
38459d1056eSguenther 
38559d1056eSguenther /*
38659d1056eSguenther  * What values are possibly accepted by the -v option?
38759d1056eSguenther  * These are implied to have a prefix of posix_prefix
38859d1056eSguenther  */
38959d1056eSguenther const char *compilation_specs[] = {
39059d1056eSguenther   "V7_ILP32_OFF32",
39159d1056eSguenther   "V7_ILP32_OFFBIG",
39259d1056eSguenther   "V7_LP64_OFF64",
39359d1056eSguenther   "V7_LPBIG_OFFBIG",
39459d1056eSguenther   "V6_ILP32_OFF32",
39559d1056eSguenther   "V6_ILP32_OFFBIG",
39659d1056eSguenther   "V6_LP64_OFF64",
39759d1056eSguenther   "V6_LPBIG_OFFBIG",
39859d1056eSguenther   NULL
39959d1056eSguenther };
400df930be7Sderaadt 
401df930be7Sderaadt int
main(int argc,char * argv[])4021837a5caSderaadt main(int argc, char *argv[])
403df930be7Sderaadt {
404df930be7Sderaadt 	int ch;
405df930be7Sderaadt 	const struct conf_variable *cp;
406df930be7Sderaadt 
407df930be7Sderaadt 	long val;
408df930be7Sderaadt 	size_t slen;
409df930be7Sderaadt 	char * sval;
410df930be7Sderaadt 
41159d1056eSguenther 	while ((ch = getopt(argc, argv, "lLv:")) != -1) {
412df930be7Sderaadt 		switch (ch) {
41359d1056eSguenther 		case 'l':	/* nonstandard: list system variables */
41459d1056eSguenther 			list_var(0);
41559d1056eSguenther 			return (0);
41659d1056eSguenther 		case 'L':	/* nonstandard: list path variables */
41759d1056eSguenther 			list_var(1);
41859d1056eSguenther 			return (0);
41959d1056eSguenther 		case 'v':
42059d1056eSguenther 			if (! compilation_spec_valid(optarg))
42159d1056eSguenther 				errx(1, "%s: unknown specification", optarg);
42259d1056eSguenther 			break;
423df930be7Sderaadt 		default:
424df930be7Sderaadt 			usage();
425df930be7Sderaadt 		}
426df930be7Sderaadt 	}
427df930be7Sderaadt 	argc -= optind;
428df930be7Sderaadt 	argv += optind;
429df930be7Sderaadt 
430420a52b0Sguenther 	if (argc < 1 || argc > 2)
431df930be7Sderaadt 		usage();
432df930be7Sderaadt 
43359d1056eSguenther 	/* pick a table based on a possible prefix */
43459d1056eSguenther 	if (strncmp(*argv, uposix_prefix, sizeof(uposix_prefix) - 1) == 0) {
43559d1056eSguenther 		cp = uposix_conf_table;
43659d1056eSguenther 		slen = sizeof(uposix_prefix) - 1;
43759d1056eSguenther 	} else if (strncmp(*argv, posix_prefix,
43859d1056eSguenther 	    sizeof(posix_prefix) - 1) == 0) {
43959d1056eSguenther 		cp = posix_conf_table;
44059d1056eSguenther 		slen = sizeof(posix_prefix) - 1;
44159d1056eSguenther 	} else {
44259d1056eSguenther 		cp = conf_table;
44359d1056eSguenther 		slen = 0;
44459d1056eSguenther 	}
44559d1056eSguenther 
44659d1056eSguenther 	/* scan the table */
44759d1056eSguenther 	for (; cp->name != NULL; cp++)
44859d1056eSguenther 		if (strcmp(*argv + slen, cp->name) == 0)
44959d1056eSguenther 			break;
45059d1056eSguenther 
45159d1056eSguenther 	/*
45259d1056eSguenther 	 * If no match, then make a final check against
45359d1056eSguenther 	 * compat_posix2_conf_table, with special magic to accept/skip
45459d1056eSguenther 	 * a leading underbar
45559d1056eSguenther 	 */
45659d1056eSguenther 	slen = argv[0][0] == '_';
45759d1056eSguenther 	if (cp->name == NULL && strncmp(*argv + slen, compat_posix2_prefix,
45859d1056eSguenther 	    sizeof(compat_posix2_prefix) - 1) == 0) {
45959d1056eSguenther 		slen += sizeof(compat_posix2_prefix) - 1;
46059d1056eSguenther 		for (cp = compat_posix2_conf_table; cp->name != NULL; cp++) {
46159d1056eSguenther 			if (strcmp(*argv + slen, cp->name) == 0)
462df930be7Sderaadt 				break;
463df930be7Sderaadt 		}
46459d1056eSguenther 	}
46559d1056eSguenther 
466420a52b0Sguenther 	if (cp->name == NULL)
4673b665a45Sderaadt 		errx(1, "%s: unknown variable", *argv);
468df930be7Sderaadt 
469df930be7Sderaadt 	if (cp->type == PATHCONF) {
470df930be7Sderaadt 		if (argc != 2) usage();
471df930be7Sderaadt 	} else {
472df930be7Sderaadt 		if (argc != 1) usage();
473df930be7Sderaadt 	}
474df930be7Sderaadt 
475df930be7Sderaadt 	switch (cp->type) {
476df930be7Sderaadt 	case CONSTANT:
47773ef282fSjca 		if (pledge("stdio", NULL) == -1)
47873ef282fSjca 			err(1, "pledge");
479df930be7Sderaadt 		printf("%ld\n", cp->value);
480df930be7Sderaadt 		break;
481df930be7Sderaadt 
482df930be7Sderaadt 	case CONFSTR:
48373ef282fSjca 		if (pledge("stdio", NULL) == -1)
48473ef282fSjca 			err(1, "pledge");
48545986164Sotto 		errno = 0;
486420a52b0Sguenther 		if ((slen = confstr(cp->value, NULL, 0)) == 0) {
487420a52b0Sguenther 			if (errno != 0)
488420a52b0Sguenther 				err(1, NULL);
489df930be7Sderaadt 
490420a52b0Sguenther 			printf("undefined\n");
491420a52b0Sguenther 		} else {
492df930be7Sderaadt 			if ((sval = malloc(slen)) == NULL)
493df930be7Sderaadt 				err(1, NULL);
494df930be7Sderaadt 
495df930be7Sderaadt 			confstr(cp->value, sval, slen);
496df930be7Sderaadt 			printf("%s\n", sval);
497420a52b0Sguenther 		}
498df930be7Sderaadt 		break;
499df930be7Sderaadt 
500df930be7Sderaadt 	case SYSCONF:
501bbd19317Sclaudio 		if (pledge("stdio ps vminfo", NULL) == -1)
50273ef282fSjca 			err(1, "pledge");
503df930be7Sderaadt 		errno = 0;
504df930be7Sderaadt 		if ((val = sysconf(cp->value)) == -1) {
505420a52b0Sguenther 			if (errno != 0)
506df930be7Sderaadt 				err(1, NULL);
507df930be7Sderaadt 
508df930be7Sderaadt 			printf("undefined\n");
509df930be7Sderaadt 		} else {
510df930be7Sderaadt 			printf("%ld\n", val);
511df930be7Sderaadt 		}
512df930be7Sderaadt 		break;
513df930be7Sderaadt 
514df930be7Sderaadt 	case PATHCONF:
51539972dfbSmestre 		if (unveil(argv[1], "r") == -1)
516bc5a8259Sbeck 			err(1, "unveil %s", argv[1]);
51773ef282fSjca 		if (pledge("stdio rpath", NULL) == -1)
51873ef282fSjca 			err(1, "pledge");
519df930be7Sderaadt 		errno = 0;
520df930be7Sderaadt 		if ((val = pathconf(argv[1], cp->value)) == -1) {
521420a52b0Sguenther 			if (errno != 0)
522df930be7Sderaadt 				err(1, "%s", argv[1]);
523df930be7Sderaadt 
524df930be7Sderaadt 			printf("undefined\n");
525df930be7Sderaadt 		} else {
526df930be7Sderaadt 			printf("%ld\n", val);
527df930be7Sderaadt 		}
528df930be7Sderaadt 		break;
529df930be7Sderaadt 	}
530df930be7Sderaadt 
53184d7e2a2Sschwarze 	return ferror(stdout);
532df930be7Sderaadt }
533df930be7Sderaadt 
534df930be7Sderaadt 
53584d7e2a2Sschwarze static void __dead
usage(void)5361837a5caSderaadt usage(void)
537df930be7Sderaadt {
53816363961Saaron 	extern char *__progname;
53916363961Saaron 
540e2f8c590Sjmc 	(void)fprintf(stderr,
541e2f8c590Sjmc 	    "usage: %s [-Ll] [-v specification] name [pathname]\n",
54259d1056eSguenther 	    __progname);
543df930be7Sderaadt 	exit(1);
544df930be7Sderaadt }
54559d1056eSguenther 
54659d1056eSguenther static void
list_var(int do_pathconf)54759d1056eSguenther list_var(int do_pathconf)
54859d1056eSguenther {
54959d1056eSguenther 	const struct conf_variable *cp;
55059d1056eSguenther 
55159d1056eSguenther 	for (cp = uposix_conf_table; cp->name != NULL; cp++)
55259d1056eSguenther 		if ((cp->type == PATHCONF) == do_pathconf)
55359d1056eSguenther 			printf("%s%s\n", uposix_prefix, cp->name);
55459d1056eSguenther 	for (cp = posix_conf_table; cp->name != NULL; cp++)
55559d1056eSguenther 		if ((cp->type == PATHCONF) == do_pathconf)
55659d1056eSguenther 			printf("%s%s\n", posix_prefix, cp->name);
55759d1056eSguenther 	for (cp = conf_table; cp->name != NULL; cp++)
55859d1056eSguenther 		if ((cp->type == PATHCONF) == do_pathconf)
55959d1056eSguenther 			printf("%s\n", cp->name);
56059d1056eSguenther 	for (cp = compat_posix2_conf_table; cp->name != NULL; cp++)
56159d1056eSguenther 		if ((cp->type == PATHCONF) == do_pathconf)
56259d1056eSguenther 			printf("_%s%s\n", compat_posix2_prefix, cp->name);
56359d1056eSguenther }
56459d1056eSguenther 
56559d1056eSguenther static int
compilation_spec_valid(const char * spec)56659d1056eSguenther compilation_spec_valid(const char *spec)
56759d1056eSguenther {
56859d1056eSguenther 	const char **sp;
56959d1056eSguenther 	const struct conf_variable *cp;
57059d1056eSguenther 
57159d1056eSguenther 	if (strncmp(spec, posix_prefix, sizeof(posix_prefix) - 1) != 0)
57259d1056eSguenther 		return (0);
57359d1056eSguenther 
57459d1056eSguenther 	spec += sizeof(posix_prefix) - 1;
57559d1056eSguenther 	for (sp = compilation_specs; *sp != NULL; sp++)
57659d1056eSguenther 		if (strcmp(spec, *sp) == 0)
57759d1056eSguenther 			break;
57859d1056eSguenther 	if (*sp == NULL)
57959d1056eSguenther 		return (0);
58059d1056eSguenther 
58159d1056eSguenther 	for (cp = uposix_conf_table; cp->name != NULL; cp++)
58259d1056eSguenther 		if (strcmp(spec, cp->name) == 0 && cp->type == SYSCONF)
58359d1056eSguenther 			return (sysconf(cp->value) != -1);
58459d1056eSguenther 
58559d1056eSguenther 	return (0);
58659d1056eSguenther }
587