libxml2: copy the xml2-config to the crosstoolsdir and patch the paths in the native...
[AROS-Contrib.git] / development / getconf / getconf.c
blob1253a5d31f1052de65c6f766872446c4496172b9
1 /* $NetBSD: getconf.c,v 1.34 2008/08/27 08:56:49 christos 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.34 2008/08/27 08:56:49 christos 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) __dead2;
58 static const struct conf_variable conf_table[] =
60 #if defined(_CS_PATH)
61 { "PATH", CONFSTR, _CS_PATH },
62 #endif
64 /* Utility Limit Minimum Values */
65 #if defined(_POSIX2_BC_BASE_MAX)
66 { "POSIX2_BC_BASE_MAX", CONSTANT, _POSIX2_BC_BASE_MAX },
67 #endif
68 #if defined(_POSIX2_BC_DIM_MAX)
69 { "POSIX2_BC_DIM_MAX", CONSTANT, _POSIX2_BC_DIM_MAX },
70 #endif
71 #if defined(_POSIX2_BC_SCALE_MAX)
72 { "POSIX2_BC_SCALE_MAX", CONSTANT, _POSIX2_BC_SCALE_MAX },
73 #endif
74 #if defined(_POSIX2_BC_STRING_MAX)
75 { "POSIX2_BC_STRING_MAX", CONSTANT, _POSIX2_BC_STRING_MAX },
76 #endif
77 #if defined(_POSIX2_COLL_WEIGHTS_MAX)
78 { "POSIX2_COLL_WEIGHTS_MAX", CONSTANT, _POSIX2_COLL_WEIGHTS_MAX },
79 #endif
80 #if defined(_POSIX2_EXPR_NEST_MAX)
81 { "POSIX2_EXPR_NEST_MAX", CONSTANT, _POSIX2_EXPR_NEST_MAX },
82 #endif
83 #if defined(_POSIX2_LINE_MAX)
84 { "POSIX2_LINE_MAX", CONSTANT, _POSIX2_LINE_MAX },
85 #endif
86 #if defined(_POSIX2_RE_DUP_MAX)
87 { "POSIX2_RE_DUP_MAX", CONSTANT, _POSIX2_RE_DUP_MAX },
88 #endif
89 #if defined(_POSIX2_VERSION)
90 { "POSIX2_VERSION", CONSTANT, _POSIX2_VERSION },
91 #endif
93 /* POSIX.1 Minimum Values */
94 #if defined(_POSIX_AIO_LISTIO_MAX)
95 { "_POSIX_AIO_LISTIO_MAX", CONSTANT, _POSIX_AIO_LISTIO_MAX },
96 #endif
97 #if defined(_POSIX_AIO_MAX)
98 { "_POSIX_AIO_MAX", CONSTANT, _POSIX_AIO_MAX },
99 #endif
100 #if defined(_POSIX_ARG_MAX)
101 { "_POSIX_ARG_MAX", CONSTANT, _POSIX_ARG_MAX },
102 #endif
103 #if defined(_POSIX_CHILD_MAX)
104 { "_POSIX_CHILD_MAX", CONSTANT, _POSIX_CHILD_MAX },
105 #endif
106 #if defined(_POSIX_LINK_MAX)
107 { "_POSIX_LINK_MAX", CONSTANT, _POSIX_LINK_MAX },
108 #endif
109 #if defined(_POSIX_MAX_CANON)
110 { "_POSIX_MAX_CANON", CONSTANT, _POSIX_MAX_CANON },
111 #endif
112 #if defined(_POSIX_MAX_INPUT)
113 { "_POSIX_MAX_INPUT", CONSTANT, _POSIX_MAX_INPUT },
114 #endif
115 #if defined(_POSIX_MQ_OPEN_MAX)
116 { "_POSIX_MQ_OPEN_MAX", CONSTANT, _POSIX_MQ_OPEN_MAX },
117 #endif
118 #if defined(_POSIX_MQ_PRIO_MAX)
119 { "_POSIX_MQ_PRIO_MAX", CONSTANT, _POSIX_MQ_PRIO_MAX },
120 #endif
121 #if defined(_POSIX_NAME_MAX)
122 { "_POSIX_NAME_MAX", CONSTANT, _POSIX_NAME_MAX },
123 #endif
124 #if defined(_POSIX_NGROUPS_MAX)
125 { "_POSIX_NGROUPS_MAX", CONSTANT, _POSIX_NGROUPS_MAX },
126 #endif
127 #if defined(_POSIX_OPEN_MAX)
128 { "_POSIX_OPEN_MAX", CONSTANT, _POSIX_OPEN_MAX },
129 #endif
130 #if defined(_POSIX_PATH_MAX)
131 { "_POSIX_PATH_MAX", CONSTANT, _POSIX_PATH_MAX },
132 #endif
133 #if defined(_POSIX_PIPE_BUF)
134 { "_POSIX_PIPE_BUF", CONSTANT, _POSIX_PIPE_BUF },
135 #endif
136 #if defined(_POSIX_SSIZE_MAX)
137 { "_POSIX_SSIZE_MAX", CONSTANT, _POSIX_SSIZE_MAX },
138 #endif
139 #if defined(_POSIX_STREAM_MAX)
140 { "_POSIX_STREAM_MAX", CONSTANT, _POSIX_STREAM_MAX },
141 #endif
142 #if defined(_POSIX_TZNAME_MAX)
143 { "_POSIX_TZNAME_MAX", CONSTANT, _POSIX_TZNAME_MAX },
144 #endif
146 /* Symbolic Utility Limits */
147 #if defined(_SC_BC_BASE_MAX)
148 { "BC_BASE_MAX", SYSCONF, _SC_BC_BASE_MAX },
149 #endif
150 #if defined(_SC_BC_DIM_MAX)
151 { "BC_DIM_MAX", SYSCONF, _SC_BC_DIM_MAX },
152 #endif
153 #if defined(_SC_BC_SCALE_MAX)
154 { "BC_SCALE_MAX", SYSCONF, _SC_BC_SCALE_MAX },
155 #endif
156 #if defined(_SC_BC_STRING_MAX)
157 { "BC_STRING_MAX", SYSCONF, _SC_BC_STRING_MAX },
158 #endif
159 #if defined(_SC_COLL_WEIGHTS_MAX)
160 { "COLL_WEIGHTS_MAX", SYSCONF, _SC_COLL_WEIGHTS_MAX },
161 #endif
162 #if defined(_SC_EXPR_NEST_MAX)
163 { "EXPR_NEST_MAX", SYSCONF, _SC_EXPR_NEST_MAX },
164 #endif
165 #if defined(_SC_LINE_MAX)
166 { "LINE_MAX", SYSCONF, _SC_LINE_MAX },
167 #endif
168 #if defined(_SC_RE_DUP_MAX)
169 { "RE_DUP_MAX", SYSCONF, _SC_RE_DUP_MAX },
170 #endif
172 /* Optional Facility Configuration Values */
173 #if defined(_SC_2_C_BIND)
174 { "_POSIX2_C_BIND", SYSCONF, _SC_2_C_BIND },
175 #endif
176 #if defined(_SC_2_C_DEV)
177 { "POSIX2_C_DEV", SYSCONF, _SC_2_C_DEV },
178 #endif
179 #if defined(_SC_2_CHAR_TERM)
180 { "POSIX2_CHAR_TERM", SYSCONF, _SC_2_CHAR_TERM },
181 #endif
182 #if defined(_SC_2_FORT_DEV)
183 { "POSIX2_FORT_DEV", SYSCONF, _SC_2_FORT_DEV },
184 #endif
185 #if defined(_SC_2_FORT_RUN)
186 { "POSIX2_FORT_RUN", SYSCONF, _SC_2_FORT_RUN },
187 #endif
188 #if defined(_SC_2_LOCALEDEF)
189 { "POSIX2_LOCALEDEF", SYSCONF, _SC_2_LOCALEDEF },
190 #endif
191 #if defined(_SC_2_SW_DEV)
192 { "POSIX2_SW_DEV", SYSCONF, _SC_2_SW_DEV },
193 #endif
194 #if defined(_SC_2_UPE)
195 { "POSIX2_UPE", SYSCONF, _SC_2_UPE },
196 #endif
198 /* POSIX.1 Configurable System Variables */
199 #if defined(_SC_AIO_LISTIO_MAX)
200 { "AIO_LISTIO_MAX", SYSCONF, _SC_AIO_LISTIO_MAX },
201 #endif
202 #if defined(_SC_AIO_MAX)
203 { "AIO_MAX", SYSCONF, _SC_AIO_MAX },
204 #endif
205 #if defined(_SC_ARG_MAX)
206 { "ARG_MAX", SYSCONF, _SC_ARG_MAX },
207 #endif
208 #if defined(_SC_CHILD_MAX)
209 { "CHILD_MAX", SYSCONF, _SC_CHILD_MAX },
210 #endif
211 #if defined(_SC_CLK_TCK)
212 { "CLK_TCK", SYSCONF, _SC_CLK_TCK },
213 #endif
214 #if defined(_SC_MQ_OPEN_MAX)
215 { "MQ_OPEN_MAX", SYSCONF, _SC_MQ_OPEN_MAX },
216 #endif
217 #if defined(_SC_MQ_PRIO_MAX)
218 { "MQ_PRIO_MAX", SYSCONF, _SC_MQ_PRIO_MAX },
219 #endif
220 #if defined(_SC_NGROUPS_MAX)
221 { "NGROUPS_MAX", SYSCONF, _SC_NGROUPS_MAX },
222 #endif
223 #if defined(_SC_OPEN_MAX)
224 { "OPEN_MAX", SYSCONF, _SC_OPEN_MAX },
225 #endif
226 #if defined(_SC_STREAM_MAX)
227 { "STREAM_MAX", SYSCONF, _SC_STREAM_MAX },
228 #endif
229 #if defined(_SC_TZNAME_MAX)
230 { "TZNAME_MAX", SYSCONF, _SC_TZNAME_MAX },
231 #endif
232 #if defined(_SC_JOB_CONTROL)
233 { "_POSIX_JOB_CONTROL", SYSCONF, _SC_JOB_CONTROL },
234 #endif
235 #if defined(_SC_SAVED_IDS)
236 { "_POSIX_SAVED_IDS", SYSCONF, _SC_SAVED_IDS },
237 #endif
238 #if defined(_SC_VERSION)
239 { "_POSIX_VERSION", SYSCONF, _SC_VERSION },
240 #endif
242 #if defined(_PC_LINK_MAX)
243 { "LINK_MAX", PATHCONF, _PC_LINK_MAX },
244 #endif
245 #if defined(_PC_MAX_CANON)
246 { "MAX_CANON", PATHCONF, _PC_MAX_CANON },
247 #endif
248 #if defined(_PC_MAX_INPUT)
249 { "MAX_INPUT", PATHCONF, _PC_MAX_INPUT },
250 #endif
251 #if defined(_PC_NAME_MAX)
252 { "NAME_MAX", PATHCONF, _PC_NAME_MAX },
253 #endif
254 #if defined(_PC_PATH_MAX)
255 { "PATH_MAX", PATHCONF, _PC_PATH_MAX },
256 #endif
257 #if defined(_PC_PIPE_BUF)
258 { "PIPE_BUF", PATHCONF, _PC_PIPE_BUF },
259 #endif
260 #if defined(_PC_CHOWN_RESTRICTED)
261 { "_POSIX_CHOWN_RESTRICTED", PATHCONF, _PC_CHOWN_RESTRICTED },
262 #endif
263 #if defined(_PC_NO_TRUNC)
264 { "_POSIX_NO_TRUNC", PATHCONF, _PC_NO_TRUNC },
265 #endif
266 #if defined(_PC_VDISABLE)
267 { "_POSIX_VDISABLE", PATHCONF, _PC_VDISABLE },
268 #endif
270 /* POSIX.1b Configurable System Variables */
271 #if defined(_SC_PAGESIZE)
272 { "PAGESIZE", SYSCONF, _SC_PAGESIZE },
273 #endif
274 #if defined(_SC_ASYNCHRONOUS_IO)
275 { "_POSIX_ASYNCHRONOUS_IO", SYSCONF, _SC_ASYNCHRONOUS_IO },
276 #endif
277 #if defined(_SC_FSYNC)
278 { "_POSIX_FSYNC", SYSCONF, _SC_FSYNC },
279 #endif
280 #if defined(_SC_MAPPED_FILES)
281 { "_POSIX_MAPPED_FILES", SYSCONF, _SC_MAPPED_FILES },
282 #endif
283 #if defined(_SC_MEMLOCK)
284 { "_POSIX_MEMLOCK", SYSCONF, _SC_MEMLOCK },
285 #endif
286 #if defined(_SC_MEMLOCK_RANGE)
287 { "_POSIX_MEMLOCK_RANGE", SYSCONF, _SC_MEMLOCK_RANGE },
288 #endif
289 #if defined(_SC_MEMORY_PROTECTION)
290 { "_POSIX_MEMORY_PROTECTION", SYSCONF, _SC_MEMORY_PROTECTION },
291 #endif
292 #if defined(_SC_MESSAGE_PASSING)
293 { "_POSIX_MESSAGE_PASSING", SYSCONF, _SC_MESSAGE_PASSING },
294 #endif
295 #if defined(_SC_MONOTONIC_CLOCK)
296 { "_POSIX_MONOTONIC_CLOCK", SYSCONF, _SC_MONOTONIC_CLOCK },
297 #endif
298 #if defined(_SC_PRIORITY_SCHEDULING)
299 { "_POSIX_PRIORITY_SCHEDULING", SYSCONF, _SC_PRIORITY_SCHEDULING },
300 #endif
301 #if defined(_SC_SEMAPHORES)
302 { "_POSIX_SEMAPHORES", SYSCONF, _SC_SEMAPHORES },
303 #endif
304 #if defined(_SC_SYNCHRONIZED_IO)
305 { "_POSIX_SYNCHRONIZED_IO", SYSCONF, _SC_SYNCHRONIZED_IO },
306 #endif
307 #if defined(_SC_TIMERS)
308 { "_POSIX_TIMERS", SYSCONF, _SC_TIMERS },
309 #endif
311 #if defined(_PC_SYNC_IO)
312 { "_POSIX_SYNC_IO", PATHCONF, _PC_SYNC_IO },
313 #endif
315 /* POSIX.1c Configurable System Variables */
316 #if defined(_SC_LOGIN_NAME_MAX)
317 { "LOGIN_NAME_MAX", SYSCONF, _SC_LOGIN_NAME_MAX },
318 #endif
319 #if defined(_SC_THREADS)
320 { "_POSIX_THREADS", SYSCONF, _SC_THREADS },
321 #endif
323 /* POSIX.1j Configurable System Variables */
324 #if defined(_SC_BARRIERS)
325 { "_POSIX_BARRIERS", SYSCONF, _SC_BARRIERS },
326 #endif
327 #if defined(_SC_READER_WRITER_LOCKS)
328 { "_POSIX_READER_WRITER_LOCKS", SYSCONF, _SC_READER_WRITER_LOCKS },
329 #endif
330 #if defined(_SC_SPIN_LOCKS)
331 { "_POSIX_SPIN_LOCKS", SYSCONF, _SC_SPIN_LOCKS },
332 #endif
334 /* XPG4.2 Configurable System Variables */
335 #if defined(_SC_IOV_MAX)
336 { "IOV_MAX", SYSCONF, _SC_IOV_MAX },
337 #endif
338 #if defined(_SC_PAGE_SIZE)
339 { "PAGE_SIZE", SYSCONF, _SC_PAGE_SIZE },
340 #endif
341 #if defined(_SC_XOPEN_SHM)
342 { "_XOPEN_SHM", SYSCONF, _SC_XOPEN_SHM },
343 #endif
345 /* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */
346 #if defined(_PC_FILESIZEBITS)
347 { "FILESIZEBITS", PATHCONF, _PC_FILESIZEBITS },
348 #endif
350 /* POSIX.1-2001 XSI Option Group Configurable System Variables */
351 #if defined(_SC_ATEXIT_MAX)
352 { "ATEXIT_MAX", SYSCONF, _SC_ATEXIT_MAX },
353 #endif
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 },
358 #endif
359 #if defined(_SC_GETPW_R_SIZE_MAX)
360 { "GETPW_R_SIZE_MAX", SYSCONF, _SC_GETPW_R_SIZE_MAX },
361 #endif
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)
377 errno = EINVAL;
378 return -1;
380 #endif
383 main(int argc, char **argv)
385 int ch;
386 const struct conf_variable *cp;
387 const char *varname, *pathname;
388 int found;
390 #if !defined(__AROS__)
391 setprogname(argv[0]);
392 #endif
393 (void)setlocale(LC_ALL, "");
395 while ((ch = getopt(argc, argv, "a")) != -1) {
396 switch (ch) {
397 case 'a':
398 a_flag = 1;
399 break;
400 case '?':
401 default:
402 usage();
405 argc -= optind;
406 argv += optind;
408 if (!a_flag) {
409 if (argc == 0)
410 usage();
411 varname = argv[0];
412 argc--;
413 argv++;
414 } else
415 varname = NULL;
417 if (argc > 1)
418 usage();
419 pathname = argv[0]; /* may be NULL */
421 found = 0;
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);
427 found = 1;
428 } else if (!a_flag)
429 errx(EXIT_FAILURE,
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;
441 static void
442 print_longvar(const char *name, long value)
444 if (a_flag)
445 (void)printf("%s = %ld\n", name, value);
446 else
447 (void)printf("%ld\n", value);
450 static void
451 print_strvar(const char *name, const char *sval)
453 if (a_flag)
454 (void)printf("%s = %s\n", name, sval);
455 else
456 (void)printf("%s\n", sval);
459 static void
460 printvar(const struct conf_variable *cp, const char *pathname)
462 size_t slen;
463 char *sval;
464 long val;
466 switch (cp->type) {
467 case CONSTANT:
468 print_longvar(cp->name, cp->value);
469 break;
471 case CONFSTR:
472 errno = 0;
473 slen = confstr((int)cp->value, NULL, 0);
474 if (slen == 0) {
475 if (errno != 0)
477 out: err(EXIT_FAILURE, "confstr(%ld)", cp->value);
478 else
479 print_strvar(cp->name, "undefined");
482 if ((sval = malloc(slen)) == NULL)
483 err(EXIT_FAILURE, "Can't allocate %zu bytes", slen);
485 errno = 0;
486 if (confstr((int)cp->value, sval, slen) == 0) {
487 if (errno != 0)
488 goto out;
489 else
490 print_strvar(cp->name, "undefined");
491 } else
492 print_strvar(cp->name, sval);
494 free(sval);
495 break;
497 case SYSCONF:
498 errno = 0;
499 if ((val = sysconf((int)cp->value)) == -1) {
500 if (errno != 0)
501 err(EXIT_FAILURE, "sysconf(%ld)", cp->value);
502 print_strvar(cp->name, "undefined");
503 } else
504 print_longvar(cp->name, val);
505 break;
507 case PATHCONF:
508 errno = 0;
509 if ((val = pathconf(pathname, (int)cp->value)) == -1) {
510 if (errno != 0) {
511 if (a_flag && errno == EINVAL) {
512 /* Just skip invalid variables */
513 return;
515 err(EXIT_FAILURE, "pathconf(%s, %ld)",
516 pathname, cp->value);
517 /* NOTREACHED */
520 print_strvar(cp->name, "undefined");
521 } else
522 print_longvar(cp->name, val);
523 break;
528 static void
529 usage(void)
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);
534 exit(EXIT_FAILURE);