1 /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published
6    by the Free Software Foundation; version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
16 
17 #include <unistd.h>
18 #include <errno.h>
19 #include <error.h>
20 #include <libintl.h>
21 #include <locale.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 
26 #include "../version.h"
27 #define PACKAGE _libc_intl_domainname
28 
29 #define NEED_SPEC_ARRAY 1
30 #include <posix-conf-vars.h>
31 
32 /* If all of the environments are defined in environments.h, then we don't need
33    to bother with doing a runtime check for a specific environment.  */
34 #if (defined _SC_V6_ILP32_OFF32 \
35      && defined _SC_V7_LPBIG_OFFBIG \
36      && defined _SC_XBS5_LP64_OFF64 \
37      && defined _SC_V6_LP64_OFF64 \
38      && defined _SC_V7_ILP32_OFFBIG \
39      && defined _SC_V6_LPBIG_OFFBIG \
40      && defined _SC_V7_LP64_OFF64 \
41      && defined _SC_V7_ILP32_OFF32 \
42      && defined _SC_XBS5_LPBIG_OFFBIG \
43      && defined _SC_XBS5_ILP32_OFFBIG \
44      && defined _SC_V6_ILP32_OFFBIG \
45      && defined _SC_XBS5_ILP32_OFF32)
46 # define ALL_ENVIRONMENTS_DEFINED 1
47 #endif
48 
49 struct conf
50   {
51     const char *name;
52     const int call_name;
53     const enum { SYSCONF, CONFSTR, PATHCONF } call;
54   };
55 
56 static const struct conf vars[] =
57   {
58     { "LINK_MAX", _PC_LINK_MAX, PATHCONF },
59     { "_POSIX_LINK_MAX", _PC_LINK_MAX, PATHCONF },
60     { "MAX_CANON", _PC_MAX_CANON, PATHCONF },
61     { "_POSIX_MAX_CANON", _PC_MAX_CANON, PATHCONF },
62     { "MAX_INPUT", _PC_MAX_INPUT, PATHCONF },
63     { "_POSIX_MAX_INPUT", _PC_MAX_INPUT, PATHCONF },
64     { "NAME_MAX", _PC_NAME_MAX, PATHCONF },
65     { "_POSIX_NAME_MAX", _PC_NAME_MAX, PATHCONF },
66     { "PATH_MAX", _PC_PATH_MAX, PATHCONF },
67     { "_POSIX_PATH_MAX", _PC_PATH_MAX, PATHCONF },
68     { "PIPE_BUF", _PC_PIPE_BUF, PATHCONF },
69     { "_POSIX_PIPE_BUF", _PC_PIPE_BUF, PATHCONF },
70     { "SOCK_MAXBUF", _PC_SOCK_MAXBUF, PATHCONF },
71     { "_POSIX_ASYNC_IO", _PC_ASYNC_IO, PATHCONF },
72     { "_POSIX_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED, PATHCONF },
73     { "_POSIX_NO_TRUNC", _PC_NO_TRUNC, PATHCONF },
74     { "_POSIX_PRIO_IO", _PC_PRIO_IO, PATHCONF },
75     { "_POSIX_SYNC_IO", _PC_SYNC_IO, PATHCONF },
76     { "_POSIX_VDISABLE", _PC_VDISABLE, PATHCONF },
77 
78     { "ARG_MAX", _SC_ARG_MAX, SYSCONF },
79     { "ATEXIT_MAX", _SC_ATEXIT_MAX, SYSCONF },
80     { "CHAR_BIT", _SC_CHAR_BIT, SYSCONF },
81     { "CHAR_MAX", _SC_CHAR_MAX, SYSCONF },
82     { "CHAR_MIN", _SC_CHAR_MIN, SYSCONF },
83     { "CHILD_MAX", _SC_CHILD_MAX, SYSCONF },
84     { "CLK_TCK", _SC_CLK_TCK, SYSCONF },
85     { "INT_MAX", _SC_INT_MAX, SYSCONF },
86     { "INT_MIN", _SC_INT_MIN, SYSCONF },
87     { "IOV_MAX", _SC_UIO_MAXIOV, SYSCONF },
88     { "LOGNAME_MAX", _SC_LOGIN_NAME_MAX, SYSCONF },
89     { "LONG_BIT", _SC_LONG_BIT, SYSCONF },
90     { "MB_LEN_MAX", _SC_MB_LEN_MAX, SYSCONF },
91     { "NGROUPS_MAX", _SC_NGROUPS_MAX, SYSCONF },
92     { "NL_ARGMAX", _SC_NL_ARGMAX, SYSCONF },
93     { "NL_LANGMAX", _SC_NL_LANGMAX, SYSCONF },
94     { "NL_MSGMAX", _SC_NL_MSGMAX, SYSCONF },
95     { "NL_NMAX", _SC_NL_NMAX, SYSCONF },
96     { "NL_SETMAX", _SC_NL_SETMAX, SYSCONF },
97     { "NL_TEXTMAX", _SC_NL_TEXTMAX, SYSCONF },
98     { "NSS_BUFLEN_GROUP", _SC_GETGR_R_SIZE_MAX, SYSCONF },
99     { "NSS_BUFLEN_PASSWD", _SC_GETPW_R_SIZE_MAX, SYSCONF },
100     { "NZERO", _SC_NZERO, SYSCONF },
101     { "OPEN_MAX", _SC_OPEN_MAX, SYSCONF },
102     { "PAGESIZE", _SC_PAGESIZE, SYSCONF },
103     { "PAGE_SIZE", _SC_PAGESIZE, SYSCONF },
104     { "PASS_MAX", _SC_PASS_MAX, SYSCONF },
105     { "PTHREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS, SYSCONF },
106     { "PTHREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX, SYSCONF },
107     { "PTHREAD_STACK_MIN", _SC_THREAD_STACK_MIN, SYSCONF },
108     { "PTHREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX, SYSCONF },
109     { "SCHAR_MAX", _SC_SCHAR_MAX, SYSCONF },
110     { "SCHAR_MIN", _SC_SCHAR_MIN, SYSCONF },
111     { "SHRT_MAX", _SC_SHRT_MAX, SYSCONF },
112     { "SHRT_MIN", _SC_SHRT_MIN, SYSCONF },
113     { "SSIZE_MAX", _SC_SSIZE_MAX, SYSCONF },
114     { "TTY_NAME_MAX", _SC_TTY_NAME_MAX, SYSCONF },
115     { "TZNAME_MAX", _SC_TZNAME_MAX, SYSCONF },
116     { "UCHAR_MAX", _SC_UCHAR_MAX, SYSCONF },
117     { "UINT_MAX", _SC_UINT_MAX, SYSCONF },
118     { "UIO_MAXIOV", _SC_UIO_MAXIOV, SYSCONF },
119     { "ULONG_MAX", _SC_ULONG_MAX, SYSCONF },
120     { "USHRT_MAX", _SC_USHRT_MAX, SYSCONF },
121     { "WORD_BIT", _SC_WORD_BIT, SYSCONF },
122     { "_AVPHYS_PAGES", _SC_AVPHYS_PAGES, SYSCONF },
123     { "_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF, SYSCONF },
124     { "_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN, SYSCONF },
125     { "_PHYS_PAGES", _SC_PHYS_PAGES, SYSCONF },
126     { "_POSIX_ARG_MAX", _SC_ARG_MAX, SYSCONF },
127     { "_POSIX_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO, SYSCONF },
128     { "_POSIX_CHILD_MAX", _SC_CHILD_MAX, SYSCONF },
129     { "_POSIX_FSYNC", _SC_FSYNC, SYSCONF },
130     { "_POSIX_JOB_CONTROL", _SC_JOB_CONTROL, SYSCONF },
131     { "_POSIX_MAPPED_FILES", _SC_MAPPED_FILES, SYSCONF },
132     { "_POSIX_MEMLOCK", _SC_MEMLOCK, SYSCONF },
133     { "_POSIX_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE, SYSCONF },
134     { "_POSIX_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION, SYSCONF },
135     { "_POSIX_MESSAGE_PASSING", _SC_MESSAGE_PASSING, SYSCONF },
136     { "_POSIX_NGROUPS_MAX", _SC_NGROUPS_MAX, SYSCONF },
137     { "_POSIX_OPEN_MAX", _SC_OPEN_MAX, SYSCONF },
138     { "_POSIX_PII", _SC_PII, SYSCONF },
139     { "_POSIX_PII_INTERNET", _SC_PII_INTERNET, SYSCONF },
140     { "_POSIX_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM, SYSCONF },
141     { "_POSIX_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM, SYSCONF },
142     { "_POSIX_PII_OSI", _SC_PII_OSI, SYSCONF },
143     { "_POSIX_PII_OSI_CLTS", _SC_PII_OSI_CLTS, SYSCONF },
144     { "_POSIX_PII_OSI_COTS", _SC_PII_OSI_COTS, SYSCONF },
145     { "_POSIX_PII_OSI_M", _SC_PII_OSI_M, SYSCONF },
146     { "_POSIX_PII_SOCKET", _SC_PII_SOCKET, SYSCONF },
147     { "_POSIX_PII_XTI", _SC_PII_XTI, SYSCONF },
148     { "_POSIX_POLL", _SC_POLL, SYSCONF },
149     { "_POSIX_PRIORITIZED_IO", _SC_PRIORITIZED_IO, SYSCONF },
150     { "_POSIX_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING, SYSCONF },
151     { "_POSIX_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS, SYSCONF },
152     { "_POSIX_SAVED_IDS", _SC_SAVED_IDS, SYSCONF },
153     { "_POSIX_SELECT", _SC_SELECT, SYSCONF },
154     { "_POSIX_SEMAPHORES", _SC_SEMAPHORES, SYSCONF },
155     { "_POSIX_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS, SYSCONF },
156     { "_POSIX_SSIZE_MAX", _SC_SSIZE_MAX, SYSCONF },
157     { "_POSIX_STREAM_MAX", _SC_STREAM_MAX, SYSCONF },
158     { "_POSIX_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO, SYSCONF },
159     { "_POSIX_THREADS", _SC_THREADS, SYSCONF },
160     { "_POSIX_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR, SYSCONF },
161     { "_POSIX_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE, SYSCONF },
162     { "_POSIX_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING, SYSCONF },
163     { "_POSIX_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT, SYSCONF },
164     { "_POSIX_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT, SYSCONF },
165     { "_POSIX_THREAD_ROBUST_PRIO_INHERIT", _SC_THREAD_ROBUST_PRIO_INHERIT,
166       SYSCONF },
167     { "_POSIX_THREAD_ROBUST_PRIO_PROTECT", _SC_THREAD_ROBUST_PRIO_PROTECT,
168       SYSCONF },
169     { "_POSIX_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED, SYSCONF },
170     { "_POSIX_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS, SYSCONF },
171     { "_POSIX_TIMERS", _SC_TIMERS, SYSCONF },
172     { "TIMER_MAX", _SC_TIMER_MAX, SYSCONF },
173     { "_POSIX_TZNAME_MAX", _SC_TZNAME_MAX, SYSCONF },
174     { "_POSIX_VERSION", _SC_VERSION, SYSCONF },
175     { "_T_IOV_MAX", _SC_T_IOV_MAX, SYSCONF },
176     { "_XOPEN_CRYPT", _SC_XOPEN_CRYPT, SYSCONF },
177     { "_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N, SYSCONF },
178     { "_XOPEN_LEGACY", _SC_XOPEN_LEGACY, SYSCONF },
179     { "_XOPEN_REALTIME", _SC_XOPEN_REALTIME, SYSCONF },
180     { "_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS, SYSCONF },
181     { "_XOPEN_SHM", _SC_XOPEN_SHM, SYSCONF },
182     { "_XOPEN_UNIX", _SC_XOPEN_UNIX, SYSCONF },
183     { "_XOPEN_VERSION", _SC_XOPEN_VERSION, SYSCONF },
184     { "_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION, SYSCONF },
185     { "_XOPEN_XPG2", _SC_XOPEN_XPG2, SYSCONF },
186     { "_XOPEN_XPG3", _SC_XOPEN_XPG3, SYSCONF },
187     { "_XOPEN_XPG4", _SC_XOPEN_XPG4, SYSCONF },
188     /* POSIX.2  */
189     { "BC_BASE_MAX", _SC_BC_BASE_MAX, SYSCONF },
190     { "BC_DIM_MAX", _SC_BC_DIM_MAX, SYSCONF },
191     { "BC_SCALE_MAX", _SC_BC_SCALE_MAX, SYSCONF },
192     { "BC_STRING_MAX", _SC_BC_STRING_MAX, SYSCONF },
193     { "CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX, SYSCONF },
194     { "COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX, SYSCONF },
195     { "EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX, SYSCONF },
196     { "EXPR_NEST_MAX", _SC_EXPR_NEST_MAX, SYSCONF },
197     { "LINE_MAX", _SC_LINE_MAX, SYSCONF },
198     { "POSIX2_BC_BASE_MAX", _SC_BC_BASE_MAX, SYSCONF },
199     { "POSIX2_BC_DIM_MAX", _SC_BC_DIM_MAX, SYSCONF },
200     { "POSIX2_BC_SCALE_MAX", _SC_BC_SCALE_MAX, SYSCONF },
201     { "POSIX2_BC_STRING_MAX", _SC_BC_STRING_MAX, SYSCONF },
202     { "POSIX2_CHAR_TERM", _SC_2_CHAR_TERM, SYSCONF },
203     { "POSIX2_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX, SYSCONF },
204     { "POSIX2_C_BIND", _SC_2_C_BIND, SYSCONF },
205     { "POSIX2_C_DEV", _SC_2_C_DEV, SYSCONF },
206     { "POSIX2_C_VERSION", _SC_2_C_VERSION, SYSCONF },
207     { "POSIX2_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX, SYSCONF },
208     { "POSIX2_FORT_DEV", _SC_2_FORT_DEV, SYSCONF },
209     { "POSIX2_FORT_RUN", _SC_2_FORT_RUN, SYSCONF },
210     { "_POSIX2_LINE_MAX", _SC_LINE_MAX, SYSCONF },
211     { "POSIX2_LINE_MAX", _SC_LINE_MAX, SYSCONF },
212     { "POSIX2_LOCALEDEF", _SC_2_LOCALEDEF, SYSCONF },
213     { "POSIX2_RE_DUP_MAX", _SC_RE_DUP_MAX, SYSCONF },
214     { "POSIX2_SW_DEV", _SC_2_SW_DEV, SYSCONF },
215     { "POSIX2_UPE", _SC_2_UPE, SYSCONF },
216     { "POSIX2_VERSION", _SC_2_VERSION, SYSCONF },
217     { "RE_DUP_MAX", _SC_RE_DUP_MAX, SYSCONF },
218 
219     { "PATH", _CS_PATH, CONFSTR },
220     { "CS_PATH", _CS_PATH, CONFSTR },
221 
222     /* LFS */
223     { "LFS_CFLAGS", _CS_LFS_CFLAGS, CONFSTR },
224     { "LFS_LDFLAGS", _CS_LFS_LDFLAGS, CONFSTR },
225     { "LFS_LIBS", _CS_LFS_LIBS, CONFSTR },
226     { "LFS_LINTFLAGS", _CS_LFS_LINTFLAGS, CONFSTR },
227     { "LFS64_CFLAGS", _CS_LFS64_CFLAGS, CONFSTR },
228     { "LFS64_LDFLAGS", _CS_LFS64_LDFLAGS, CONFSTR },
229     { "LFS64_LIBS", _CS_LFS64_LIBS, CONFSTR },
230     { "LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS, CONFSTR },
231 
232     /* Programming environments.  */
233     { "_XBS5_WIDTH_RESTRICTED_ENVS", _CS_V5_WIDTH_RESTRICTED_ENVS, CONFSTR },
234     { "XBS5_WIDTH_RESTRICTED_ENVS", _CS_V5_WIDTH_RESTRICTED_ENVS, CONFSTR },
235 
236     { "_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32, SYSCONF },
237     { "XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS, CONFSTR },
238     { "XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS, CONFSTR },
239     { "XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS, CONFSTR },
240     { "XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS, CONFSTR },
241 
242     { "_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG, SYSCONF },
243     { "XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS, CONFSTR },
244     { "XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS, CONFSTR },
245     { "XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS, CONFSTR },
246     { "XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS, CONFSTR },
247 
248     { "_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64, SYSCONF },
249     { "XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS, CONFSTR },
250     { "XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS, CONFSTR },
251     { "XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS, CONFSTR },
252     { "XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS, CONFSTR },
253 
254     { "_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG, SYSCONF },
255     { "XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS, CONFSTR },
256     { "XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS, CONFSTR },
257     { "XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS, CONFSTR },
258     { "XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS, CONFSTR },
259 
260     { "_POSIX_V6_ILP32_OFF32", _SC_V6_ILP32_OFF32, SYSCONF },
261     { "POSIX_V6_ILP32_OFF32_CFLAGS", _CS_POSIX_V6_ILP32_OFF32_CFLAGS, CONFSTR },
262     { "POSIX_V6_ILP32_OFF32_LDFLAGS", _CS_POSIX_V6_ILP32_OFF32_LDFLAGS, CONFSTR },
263     { "POSIX_V6_ILP32_OFF32_LIBS", _CS_POSIX_V6_ILP32_OFF32_LIBS, CONFSTR },
264     { "POSIX_V6_ILP32_OFF32_LINTFLAGS", _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS, CONFSTR },
265 
266     { "_POSIX_V6_WIDTH_RESTRICTED_ENVS", _CS_V6_WIDTH_RESTRICTED_ENVS, CONFSTR },
267     { "POSIX_V6_WIDTH_RESTRICTED_ENVS", _CS_V6_WIDTH_RESTRICTED_ENVS, CONFSTR },
268 
269     { "_POSIX_V6_ILP32_OFFBIG", _SC_V6_ILP32_OFFBIG, SYSCONF },
270     { "POSIX_V6_ILP32_OFFBIG_CFLAGS", _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS, CONFSTR },
271     { "POSIX_V6_ILP32_OFFBIG_LDFLAGS", _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS, CONFSTR },
272     { "POSIX_V6_ILP32_OFFBIG_LIBS", _CS_POSIX_V6_ILP32_OFFBIG_LIBS, CONFSTR },
273     { "POSIX_V6_ILP32_OFFBIG_LINTFLAGS", _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS, CONFSTR },
274 
275     { "_POSIX_V6_LP64_OFF64", _SC_V6_LP64_OFF64, SYSCONF },
276     { "POSIX_V6_LP64_OFF64_CFLAGS", _CS_POSIX_V6_LP64_OFF64_CFLAGS, CONFSTR },
277     { "POSIX_V6_LP64_OFF64_LDFLAGS", _CS_POSIX_V6_LP64_OFF64_LDFLAGS, CONFSTR },
278     { "POSIX_V6_LP64_OFF64_LIBS", _CS_POSIX_V6_LP64_OFF64_LIBS, CONFSTR },
279     { "POSIX_V6_LP64_OFF64_LINTFLAGS", _CS_POSIX_V6_LP64_OFF64_LINTFLAGS, CONFSTR },
280 
281     { "_POSIX_V6_LPBIG_OFFBIG", _SC_V6_LPBIG_OFFBIG, SYSCONF },
282     { "POSIX_V6_LPBIG_OFFBIG_CFLAGS", _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS, CONFSTR },
283     { "POSIX_V6_LPBIG_OFFBIG_LDFLAGS", _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS, CONFSTR },
284     { "POSIX_V6_LPBIG_OFFBIG_LIBS", _CS_POSIX_V6_LPBIG_OFFBIG_LIBS, CONFSTR },
285     { "POSIX_V6_LPBIG_OFFBIG_LINTFLAGS", _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS, CONFSTR },
286 
287     { "_POSIX_V7_ILP32_OFF32", _SC_V7_ILP32_OFF32, SYSCONF },
288     { "POSIX_V7_ILP32_OFF32_CFLAGS", _CS_POSIX_V7_ILP32_OFF32_CFLAGS, CONFSTR },
289     { "POSIX_V7_ILP32_OFF32_LDFLAGS", _CS_POSIX_V7_ILP32_OFF32_LDFLAGS, CONFSTR },
290     { "POSIX_V7_ILP32_OFF32_LIBS", _CS_POSIX_V7_ILP32_OFF32_LIBS, CONFSTR },
291     { "POSIX_V7_ILP32_OFF32_LINTFLAGS", _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS, CONFSTR },
292 
293     { "_POSIX_V7_WIDTH_RESTRICTED_ENVS", _CS_V7_WIDTH_RESTRICTED_ENVS, CONFSTR },
294     { "POSIX_V7_WIDTH_RESTRICTED_ENVS", _CS_V7_WIDTH_RESTRICTED_ENVS, CONFSTR },
295 
296     { "_POSIX_V7_ILP32_OFFBIG", _SC_V7_ILP32_OFFBIG, SYSCONF },
297     { "POSIX_V7_ILP32_OFFBIG_CFLAGS", _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS, CONFSTR },
298     { "POSIX_V7_ILP32_OFFBIG_LDFLAGS", _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS, CONFSTR },
299     { "POSIX_V7_ILP32_OFFBIG_LIBS", _CS_POSIX_V7_ILP32_OFFBIG_LIBS, CONFSTR },
300     { "POSIX_V7_ILP32_OFFBIG_LINTFLAGS", _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS, CONFSTR },
301 
302     { "_POSIX_V7_LP64_OFF64", _SC_V7_LP64_OFF64, SYSCONF },
303     { "POSIX_V7_LP64_OFF64_CFLAGS", _CS_POSIX_V7_LP64_OFF64_CFLAGS, CONFSTR },
304     { "POSIX_V7_LP64_OFF64_LDFLAGS", _CS_POSIX_V7_LP64_OFF64_LDFLAGS, CONFSTR },
305     { "POSIX_V7_LP64_OFF64_LIBS", _CS_POSIX_V7_LP64_OFF64_LIBS, CONFSTR },
306     { "POSIX_V7_LP64_OFF64_LINTFLAGS", _CS_POSIX_V7_LP64_OFF64_LINTFLAGS, CONFSTR },
307 
308     { "_POSIX_V7_LPBIG_OFFBIG", _SC_V7_LPBIG_OFFBIG, SYSCONF },
309     { "POSIX_V7_LPBIG_OFFBIG_CFLAGS", _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS, CONFSTR },
310     { "POSIX_V7_LPBIG_OFFBIG_LDFLAGS", _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS, CONFSTR },
311     { "POSIX_V7_LPBIG_OFFBIG_LIBS", _CS_POSIX_V7_LPBIG_OFFBIG_LIBS, CONFSTR },
312     { "POSIX_V7_LPBIG_OFFBIG_LINTFLAGS", _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS, CONFSTR },
313 
314     { "_POSIX_ADVISORY_INFO", _SC_ADVISORY_INFO, SYSCONF },
315     { "_POSIX_BARRIERS", _SC_BARRIERS, SYSCONF },
316     { "_POSIX_BASE", _SC_BASE, SYSCONF },
317     { "_POSIX_C_LANG_SUPPORT", _SC_C_LANG_SUPPORT, SYSCONF },
318     { "_POSIX_C_LANG_SUPPORT_R", _SC_C_LANG_SUPPORT_R, SYSCONF },
319     { "_POSIX_CLOCK_SELECTION", _SC_CLOCK_SELECTION, SYSCONF },
320     { "_POSIX_CPUTIME", _SC_CPUTIME, SYSCONF },
321     { "_POSIX_THREAD_CPUTIME", _SC_THREAD_CPUTIME, SYSCONF },
322     { "_POSIX_DEVICE_SPECIFIC", _SC_DEVICE_SPECIFIC, SYSCONF },
323     { "_POSIX_DEVICE_SPECIFIC_R", _SC_DEVICE_SPECIFIC_R, SYSCONF },
324     { "_POSIX_FD_MGMT", _SC_FD_MGMT, SYSCONF },
325     { "_POSIX_FIFO", _SC_FIFO, SYSCONF },
326     { "_POSIX_PIPE", _SC_PIPE, SYSCONF },
327     { "_POSIX_FILE_ATTRIBUTES", _SC_FILE_ATTRIBUTES, SYSCONF },
328     { "_POSIX_FILE_LOCKING", _SC_FILE_LOCKING, SYSCONF },
329     { "_POSIX_FILE_SYSTEM", _SC_FILE_SYSTEM, SYSCONF },
330     { "_POSIX_MONOTONIC_CLOCK", _SC_MONOTONIC_CLOCK, SYSCONF },
331     { "_POSIX_MULTI_PROCESS", _SC_MULTI_PROCESS, SYSCONF },
332     { "_POSIX_SINGLE_PROCESS", _SC_SINGLE_PROCESS, SYSCONF },
333     { "_POSIX_NETWORKING", _SC_NETWORKING, SYSCONF },
334     { "_POSIX_READER_WRITER_LOCKS", _SC_READER_WRITER_LOCKS, SYSCONF },
335     { "_POSIX_SPIN_LOCKS", _SC_SPIN_LOCKS, SYSCONF },
336     { "_POSIX_REGEXP", _SC_REGEXP, SYSCONF },
337     { "_REGEX_VERSION", _SC_REGEX_VERSION, SYSCONF },
338     { "_POSIX_SHELL", _SC_SHELL, SYSCONF },
339     { "_POSIX_SIGNALS", _SC_SIGNALS, SYSCONF },
340     { "_POSIX_SPAWN", _SC_SPAWN, SYSCONF },
341     { "_POSIX_SPORADIC_SERVER", _SC_SPORADIC_SERVER, SYSCONF },
342     { "_POSIX_THREAD_SPORADIC_SERVER", _SC_THREAD_SPORADIC_SERVER, SYSCONF },
343     { "_POSIX_SYSTEM_DATABASE", _SC_SYSTEM_DATABASE, SYSCONF },
344     { "_POSIX_SYSTEM_DATABASE_R", _SC_SYSTEM_DATABASE_R, SYSCONF },
345     { "_POSIX_TIMEOUTS", _SC_TIMEOUTS, SYSCONF },
346     { "_POSIX_TYPED_MEMORY_OBJECTS", _SC_TYPED_MEMORY_OBJECTS, SYSCONF },
347     { "_POSIX_USER_GROUPS", _SC_USER_GROUPS, SYSCONF },
348     { "_POSIX_USER_GROUPS_R", _SC_USER_GROUPS_R, SYSCONF },
349     { "POSIX2_PBS", _SC_2_PBS, SYSCONF },
350     { "POSIX2_PBS_ACCOUNTING", _SC_2_PBS_ACCOUNTING, SYSCONF },
351     { "POSIX2_PBS_LOCATE", _SC_2_PBS_LOCATE, SYSCONF },
352     { "POSIX2_PBS_TRACK", _SC_2_PBS_TRACK, SYSCONF },
353     { "POSIX2_PBS_MESSAGE", _SC_2_PBS_MESSAGE, SYSCONF },
354     { "SYMLOOP_MAX", _SC_SYMLOOP_MAX, SYSCONF },
355     { "STREAM_MAX", _SC_STREAM_MAX, SYSCONF },
356     { "AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX, SYSCONF },
357     { "AIO_MAX", _SC_AIO_MAX, SYSCONF },
358     { "AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX, SYSCONF },
359     { "DELAYTIMER_MAX", _SC_DELAYTIMER_MAX, SYSCONF },
360     { "HOST_NAME_MAX", _SC_HOST_NAME_MAX, SYSCONF },
361     { "LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX, SYSCONF },
362     { "MQ_OPEN_MAX", _SC_MQ_OPEN_MAX, SYSCONF },
363     { "MQ_PRIO_MAX", _SC_MQ_PRIO_MAX, SYSCONF },
364     { "_POSIX_DEVICE_IO", _SC_DEVICE_IO, SYSCONF },
365     { "_POSIX_TRACE", _SC_TRACE, SYSCONF },
366     { "_POSIX_TRACE_EVENT_FILTER", _SC_TRACE_EVENT_FILTER, SYSCONF },
367     { "_POSIX_TRACE_INHERIT", _SC_TRACE_INHERIT, SYSCONF },
368     { "_POSIX_TRACE_LOG", _SC_TRACE_LOG, SYSCONF },
369     { "RTSIG_MAX", _SC_RTSIG_MAX, SYSCONF },
370     { "SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX, SYSCONF },
371     { "SEM_VALUE_MAX", _SC_SEM_VALUE_MAX, SYSCONF },
372     { "SIGQUEUE_MAX", _SC_SIGQUEUE_MAX, SYSCONF },
373     { "FILESIZEBITS", _PC_FILESIZEBITS, PATHCONF },
374     { "POSIX_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN, PATHCONF },
375     { "POSIX_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE, PATHCONF },
376     { "POSIX_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE, PATHCONF },
377     { "POSIX_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE, PATHCONF },
378     { "POSIX_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN, PATHCONF },
379     { "SYMLINK_MAX", _PC_SYMLINK_MAX, PATHCONF },
380     { "GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION, CONFSTR },
381     { "GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION, CONFSTR },
382     { "POSIX2_SYMLINKS", _PC_2_SYMLINKS, PATHCONF },
383 
384     { "LEVEL1_ICACHE_SIZE", _SC_LEVEL1_ICACHE_SIZE, SYSCONF },
385     { "LEVEL1_ICACHE_ASSOC", _SC_LEVEL1_ICACHE_ASSOC, SYSCONF },
386     { "LEVEL1_ICACHE_LINESIZE", _SC_LEVEL1_ICACHE_LINESIZE, SYSCONF },
387     { "LEVEL1_DCACHE_SIZE", _SC_LEVEL1_DCACHE_SIZE, SYSCONF },
388     { "LEVEL1_DCACHE_ASSOC", _SC_LEVEL1_DCACHE_ASSOC, SYSCONF },
389     { "LEVEL1_DCACHE_LINESIZE", _SC_LEVEL1_DCACHE_LINESIZE, SYSCONF },
390     { "LEVEL2_CACHE_SIZE", _SC_LEVEL2_CACHE_SIZE, SYSCONF },
391     { "LEVEL2_CACHE_ASSOC", _SC_LEVEL2_CACHE_ASSOC, SYSCONF },
392     { "LEVEL2_CACHE_LINESIZE", _SC_LEVEL2_CACHE_LINESIZE, SYSCONF },
393     { "LEVEL3_CACHE_SIZE", _SC_LEVEL3_CACHE_SIZE, SYSCONF },
394     { "LEVEL3_CACHE_ASSOC", _SC_LEVEL3_CACHE_ASSOC, SYSCONF },
395     { "LEVEL3_CACHE_LINESIZE", _SC_LEVEL3_CACHE_LINESIZE, SYSCONF },
396     { "LEVEL4_CACHE_SIZE", _SC_LEVEL4_CACHE_SIZE, SYSCONF },
397     { "LEVEL4_CACHE_ASSOC", _SC_LEVEL4_CACHE_ASSOC, SYSCONF },
398     { "LEVEL4_CACHE_LINESIZE", _SC_LEVEL4_CACHE_LINESIZE, SYSCONF },
399 
400     { "IPV6", _SC_IPV6, SYSCONF },
401     { "RAW_SOCKETS", _SC_RAW_SOCKETS, SYSCONF },
402 
403     { "_POSIX_IPV6", _SC_IPV6, SYSCONF },
404     { "_POSIX_RAW_SOCKETS", _SC_RAW_SOCKETS, SYSCONF },
405 
406     { NULL, 0, SYSCONF }
407   };
408 
409 
410 extern const char *__progname;
411 
412 
413 static void
usage(void)414 usage (void)
415 {
416   fprintf (stderr,
417 	   _("Usage: %s [-v specification] variable_name [pathname]\n"),
418 	   __progname);
419   fprintf (stderr,
420 	   _("       %s -a [pathname]\n"), __progname);
421   exit (2);
422 }
423 
424 
425 static void
print_all(const char * path)426 print_all (const char *path)
427 {
428   const struct conf *c;
429   size_t clen;
430   long int value;
431   char *cvalue;
432   for (c = vars; c->name != NULL; ++c) {
433     printf("%-35s", c->name);
434     switch (c->call) {
435       case PATHCONF:
436 	value = pathconf (path, c->call_name);
437 	if (value != -1) {
438 	  printf("%ld", value);
439 	}
440 	printf("\n");
441 	break;
442       case SYSCONF:
443 	value = sysconf (c->call_name);
444 	if (value == -1l) {
445 	  if (c->call_name == _SC_UINT_MAX
446 	    || c->call_name == _SC_ULONG_MAX)
447 	    printf ("%lu", value);
448 	}
449 	else {
450 	  printf ("%ld", value);
451 	}
452 	printf ("\n");
453 	break;
454       case CONFSTR:
455 	clen = confstr (c->call_name, (char *) NULL, 0);
456 	cvalue = (char *) malloc (clen);
457 	if (cvalue == NULL)
458 	  error (3, 0, _("memory exhausted"));
459 	if (confstr (c->call_name, cvalue, clen) != clen)
460 	  error (3, errno, "confstr");
461 	printf ("%.*s\n", (int) clen, cvalue);
462 	free (cvalue);
463 	break;
464     }
465   }
466   exit (0);
467 }
468 
469 int
main(int argc,char * argv[])470 main (int argc, char *argv[])
471 {
472   const struct conf *c;
473 
474   /* Set locale.  Do not set LC_ALL because the other categories must
475      not be affected (according to POSIX.2).  */
476   setlocale (LC_CTYPE, "");
477   setlocale (LC_MESSAGES, "");
478 
479   /* Initialize the message catalog.  */
480   textdomain (PACKAGE);
481 
482   if (argc > 1 && strcmp (argv[1], "--version") == 0)
483     {
484       printf ("getconf %s%s\n", PKGVERSION, VERSION);
485       printf (gettext ("\
486 Copyright (C) %s Free Software Foundation, Inc.\n\
487 This is free software; see the source for copying conditions.  There is NO\n\
488 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
489 "), "2022");
490       printf (gettext ("Written by %s.\n"), "Roland McGrath");
491       return 0;
492     }
493 
494   if (argc > 1 && strcmp (argv[1], "--help") == 0)
495     {
496       printf (gettext ("\
497 Usage: getconf [-v SPEC] VAR\n\
498   or:  getconf [-v SPEC] PATH_VAR PATH\n\
499 \n\
500 Get the configuration value for variable VAR, or for variable PATH_VAR\n\
501 for path PATH.  If SPEC is given, give values for compilation\n\
502 environment SPEC.\n\n"));
503       printf (gettext ("For bug reporting instructions, please see:\n\
504 %s.\n"), REPORT_BUGS_TO);
505       return 0;
506     }
507 
508 #ifdef ALL_ENVIRONMENTS_DEFINED
509   if (argc > 1 && strncmp (argv[1], "-v", 2) == 0)
510     {
511       if (argv[1][2] == '\0')
512 	{
513 	  if (argc < 3)
514 	    usage ();
515 
516 	  argv += 2;
517 	  argc -= 2;
518 	}
519       else
520 	{
521 	  argv += 1;
522 	  argc -= 1;
523 	}
524     }
525 #else
526   const char *getconf_dir = getenv ("GETCONF_DIR") ?: GETCONF_DIR;
527   size_t getconf_dirlen = strlen (getconf_dir);
528 
529   const char *spec = NULL;
530   char buf[sizeof "POSIX_V6_LPBIG_OFFBIG"];
531   char *argv0 = argv[0];
532   if (argc > 1 && strncmp (argv[1], "-v", 2) == 0)
533     {
534       if (argv[1][2] == '\0')
535 	{
536 	  if (argc < 3)
537 	    usage ();
538 
539 	  spec = argv[2];
540 	  argv += 2;
541 	  argc -= 2;
542 	}
543       else
544 	{
545 	  spec = &argv[1][2];
546 	  argv += 1;
547 	  argc -= 1;
548 	}
549     }
550   else
551     {
552       char default_name[getconf_dirlen + sizeof "/default"];
553       memcpy (mempcpy (default_name, getconf_dir, getconf_dirlen),
554 	      "/default", sizeof "/default");
555       int len = readlink (default_name, buf, sizeof buf - 1);
556       if (len > 0)
557 	{
558 	  buf[len] = '\0';
559 	  spec = buf;
560 	}
561     }
562 
563   /* Check for the specifications we know.  */
564   if (spec != NULL)
565     {
566       size_t i;
567       for (i = 0; i < nspecs; ++i)
568 	if (strcmp (spec, specs[i].name) == 0)
569 	  break;
570 
571       if (i == nspecs)
572 	error (2, 0, _("unknown specification \"%s\""), spec);
573 
574       switch (specs[i].num)
575 	{
576 # ifndef _XBS5_ILP32_OFF32
577 	  case _SC_XBS5_ILP32_OFF32:
578 # endif
579 # ifndef _XBS5_ILP32_OFFBIG
580 	  case _SC_XBS5_ILP32_OFFBIG:
581 # endif
582 # ifndef _XBS5_LP64_OFF64
583 	  case _SC_XBS5_LP64_OFF64:
584 # endif
585 # ifndef _XBS5_LPBIG_OFFBIG
586 	  case _SC_XBS5_LPBIG_OFFBIG:
587 # endif
588 # ifndef _POSIX_V6_ILP32_OFF32
589 	  case _SC_V6_ILP32_OFF32:
590 # endif
591 # ifndef _POSIX_V6_ILP32_OFFBIG
592 	  case _SC_V6_ILP32_OFFBIG:
593 # endif
594 # ifndef _POSIX_V6_LP64_OFF64
595 	  case _SC_V6_LP64_OFF64:
596 # endif
597 # ifndef _POSIX_V6_LPBIG_OFFBIG
598 	  case _SC_V6_LPBIG_OFFBIG:
599 # endif
600 # ifndef _POSIX_V7_ILP32_OFF32
601 	  case _SC_V7_ILP32_OFF32:
602 # endif
603 # ifndef _POSIX_V7_ILP32_OFFBIG
604 	  case _SC_V7_ILP32_OFFBIG:
605 # endif
606 # ifndef _POSIX_V7_LP64_OFF64
607 	  case _SC_V7_LP64_OFF64:
608 # endif
609 # ifndef _POSIX_V7_LPBIG_OFFBIG
610 	  case _SC_V7_LPBIG_OFFBIG:
611 # endif
612 	    {
613 	      const char *args[argc + 3];
614 	      size_t spec_len = strlen (spec);
615 	      char getconf_name[getconf_dirlen + 1 + spec_len + 1];
616 	      memcpy (mempcpy (mempcpy (getconf_name, getconf_dir,
617 					getconf_dirlen),
618 			       "/", 1), spec, spec_len + 1);
619 	      args[0] = argv0;
620 	      args[1] = "-v";
621 	      args[2] = spec;
622 	      memcpy (&args[3], &argv[1], argc * sizeof (argv[1]));
623 	      execv (getconf_name, (char * const *) args);
624 	      error (4, errno, _("Couldn't execute %s"), getconf_name);
625 	    }
626 	  default:
627 	    break;
628 	}
629     }
630 #endif
631 
632   if (argc > 1 && strcmp (argv[1], "-a") == 0)
633     {
634       if (argc == 2)
635 	print_all ("/");
636       else if (argc == 3)
637 	print_all (argv[2]);
638       else
639 	usage ();
640     }
641 
642   int ai = 1;
643   if (argc > ai && strcmp (argv[ai], "--") == 0)
644     ++ai;
645 
646   if (argc - ai < 1 || argc - ai > 2)
647     usage ();
648 
649   for (c = vars; c->name != NULL; ++c)
650     if (strcmp (c->name, argv[ai]) == 0
651 	|| (strncmp (c->name, "_POSIX_", 7) == 0
652 	    && strcmp (c->name + 7, argv[ai]) == 0))
653       {
654 	long int value;
655 	size_t clen;
656 	char *cvalue;
657 	switch (c->call)
658 	  {
659 	  case PATHCONF:
660 	    if (argc - ai < 2)
661 	      usage ();
662 	    errno = 0;
663 	    value = pathconf (argv[ai + 1], c->call_name);
664 	    if (value == -1)
665 	      {
666 		if (errno)
667 		  error (3, errno, "pathconf: %s", argv[ai + 1]);
668 		else
669 		  puts (_("undefined"));
670 	      }
671 	    else
672 	      printf ("%ld\n", value);
673 	    exit (0);
674 
675 	  case SYSCONF:
676 	    if (argc - ai > 1)
677 	      usage ();
678 	    value = sysconf (c->call_name);
679 	    if (value == -1l)
680 	      {
681 		if (c->call_name == _SC_UINT_MAX
682 		    || c->call_name == _SC_ULONG_MAX)
683 		  printf ("%lu\n", value);
684 		else
685 		  puts (_("undefined"));
686 	      }
687 	    else
688 	      printf ("%ld\n", value);
689 	    exit (0);
690 
691 	  case CONFSTR:
692 	    if (argc - ai > 1)
693 	      usage ();
694 	    clen = confstr (c->call_name, (char *) NULL, 0);
695 	    cvalue = (char *) malloc (clen);
696 	    if (cvalue == NULL)
697 	      error (3, 0, _("memory exhausted"));
698 
699 	    if (confstr (c->call_name, cvalue, clen) != clen)
700 	      error (3, errno, "confstr");
701 
702 	    printf ("%.*s\n", (int) clen, cvalue);
703 	    exit (0);
704 	  }
705       }
706 
707   error (2, 0, _("Unrecognized variable `%s'"), argv[ai]);
708   /* NOTREACHED */
709   return 2;
710 }
711