xref: /netbsd-src/usr.bin/getconf/getconf.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: getconf.c,v 1.22 2004/11/10 04:02:52 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by J.T. Conklin.
9  *
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.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: getconf.c,v 1.22 2004/11/10 04:02:52 lukem Exp $");
42 #endif /* not lint */
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <locale.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 
53 int	main __P((int, char **));
54 static void usage __P((void));
55 
56 struct conf_variable
57 {
58   const char *name;
59   enum { SYSCONF, CONFSTR, PATHCONF, CONSTANT } type;
60   long value;
61 };
62 
63 const struct conf_variable conf_table[] =
64 {
65   { "PATH",			CONFSTR,	_CS_PATH		},
66 
67   /* Utility Limit Minimum Values */
68   { "POSIX2_BC_BASE_MAX",	CONSTANT,	_POSIX2_BC_BASE_MAX	},
69   { "POSIX2_BC_DIM_MAX",	CONSTANT,	_POSIX2_BC_DIM_MAX	},
70   { "POSIX2_BC_SCALE_MAX",	CONSTANT,	_POSIX2_BC_SCALE_MAX	},
71   { "POSIX2_BC_STRING_MAX",	CONSTANT,	_POSIX2_BC_STRING_MAX	},
72   { "POSIX2_COLL_WEIGHTS_MAX",	CONSTANT,	_POSIX2_COLL_WEIGHTS_MAX },
73   { "POSIX2_EXPR_NEST_MAX",	CONSTANT,	_POSIX2_EXPR_NEST_MAX	},
74   { "POSIX2_LINE_MAX",		CONSTANT,	_POSIX2_LINE_MAX	},
75   { "POSIX2_RE_DUP_MAX",	CONSTANT,	_POSIX2_RE_DUP_MAX	},
76   { "POSIX2_VERSION",		CONSTANT,	_POSIX2_VERSION		},
77 
78   /* POSIX.1 Minimum Values */
79   { "_POSIX_ARG_MAX",		CONSTANT,	_POSIX_ARG_MAX		},
80   { "_POSIX_CHILD_MAX",		CONSTANT,	_POSIX_CHILD_MAX	},
81   { "_POSIX_LINK_MAX",		CONSTANT,	_POSIX_LINK_MAX		},
82   { "_POSIX_MAX_CANON",		CONSTANT,	_POSIX_MAX_CANON	},
83   { "_POSIX_MAX_INPUT",		CONSTANT,	_POSIX_MAX_INPUT	},
84   { "_POSIX_NAME_MAX",		CONSTANT,	_POSIX_NAME_MAX		},
85   { "_POSIX_NGROUPS_MAX",	CONSTANT,	_POSIX_NGROUPS_MAX	},
86   { "_POSIX_OPEN_MAX",		CONSTANT,	_POSIX_OPEN_MAX		},
87   { "_POSIX_PATH_MAX",		CONSTANT,	_POSIX_PATH_MAX		},
88   { "_POSIX_PIPE_BUF",		CONSTANT,	_POSIX_PIPE_BUF		},
89   { "_POSIX_SSIZE_MAX",		CONSTANT,	_POSIX_SSIZE_MAX	},
90   { "_POSIX_STREAM_MAX",	CONSTANT,	_POSIX_STREAM_MAX	},
91   { "_POSIX_TZNAME_MAX",	CONSTANT,	_POSIX_TZNAME_MAX	},
92 
93   /* Symbolic Utility Limits */
94   { "BC_BASE_MAX",		SYSCONF,	_SC_BC_BASE_MAX		},
95   { "BC_DIM_MAX",		SYSCONF,	_SC_BC_DIM_MAX		},
96   { "BC_SCALE_MAX",		SYSCONF,	_SC_BC_SCALE_MAX	},
97   { "BC_STRING_MAX",		SYSCONF,	_SC_BC_STRING_MAX	},
98   { "COLL_WEIGHTS_MAX",		SYSCONF,	_SC_COLL_WEIGHTS_MAX	},
99   { "EXPR_NEST_MAX",		SYSCONF,	_SC_EXPR_NEST_MAX	},
100   { "LINE_MAX",			SYSCONF,	_SC_LINE_MAX		},
101   { "RE_DUP_MAX",		SYSCONF,	_SC_RE_DUP_MAX		},
102 
103   /* Optional Facility Configuration Values */
104   { "POSIX2_C_BIND",		SYSCONF,	_SC_2_C_BIND		},
105   { "POSIX2_C_DEV",		SYSCONF,	_SC_2_C_DEV		},
106   { "POSIX2_CHAR_TERM",		SYSCONF,	_SC_2_CHAR_TERM		},
107   { "POSIX2_FORT_DEV",		SYSCONF,	_SC_2_FORT_DEV		},
108   { "POSIX2_FORT_RUN",		SYSCONF,	_SC_2_FORT_RUN		},
109   { "POSIX2_LOCALEDEF",		SYSCONF,	_SC_2_LOCALEDEF		},
110   { "POSIX2_SW_DEV",		SYSCONF,	_SC_2_SW_DEV		},
111   { "POSIX2_UPE",		SYSCONF,	_SC_2_UPE		},
112 
113   /* POSIX.1 Configurable System Variables */
114   { "ARG_MAX",			SYSCONF,	_SC_ARG_MAX 		},
115   { "CHILD_MAX",		SYSCONF,	_SC_CHILD_MAX		},
116   { "CLK_TCK",			SYSCONF,	_SC_CLK_TCK		},
117   { "NGROUPS_MAX",		SYSCONF,	_SC_NGROUPS_MAX		},
118   { "OPEN_MAX",			SYSCONF,	_SC_OPEN_MAX		},
119   { "STREAM_MAX",		SYSCONF,	_SC_STREAM_MAX		},
120   { "TZNAME_MAX",		SYSCONF,	_SC_TZNAME_MAX		},
121   { "_POSIX_JOB_CONTROL",	SYSCONF,	_SC_JOB_CONTROL 	},
122   { "_POSIX_SAVED_IDS",		SYSCONF,	_SC_SAVED_IDS		},
123   { "_POSIX_VERSION",		SYSCONF,	_SC_VERSION		},
124 
125   { "LINK_MAX",			PATHCONF,	_PC_LINK_MAX		},
126   { "MAX_CANON",		PATHCONF,	_PC_MAX_CANON		},
127   { "MAX_INPUT",		PATHCONF,	_PC_MAX_INPUT		},
128   { "NAME_MAX",			PATHCONF,	_PC_NAME_MAX		},
129   { "PATH_MAX",			PATHCONF,	_PC_PATH_MAX		},
130   { "PIPE_BUF",			PATHCONF,	_PC_PIPE_BUF		},
131   { "_POSIX_CHOWN_RESTRICTED",	PATHCONF,	_PC_CHOWN_RESTRICTED	},
132   { "_POSIX_NO_TRUNC",		PATHCONF,	_PC_NO_TRUNC		},
133   { "_POSIX_VDISABLE",		PATHCONF,	_PC_VDISABLE		},
134 
135   /* POSIX.1b Configurable System Variables */
136   { "PAGESIZE",			SYSCONF,	_SC_PAGESIZE		},
137   { "_POSIX_FSYNC",		SYSCONF,	_SC_FSYNC		},
138   { "_POSIX_MAPPED_FILES",	SYSCONF,	_SC_MAPPED_FILES	},
139   { "_POSIX_MEMLOCK",		SYSCONF,	_SC_MEMLOCK		},
140   { "_POSIX_MEMLOCK_RANGE",	SYSCONF,	_SC_MEMLOCK_RANGE	},
141   { "_POSIX_MEMORY_PROTECTION",	SYSCONF,	_SC_MEMORY_PROTECTION	},
142   { "_POSIX_MONOTONIC_CLOCK",	SYSCONF,	_SC_MONOTONIC_CLOCK	},
143   { "_POSIX_SEMAPHORES",	SYSCONF,	_SC_SEMAPHORES		},
144   { "_POSIX_SYNCHRONIZED_IO",	SYSCONF,	_SC_SYNCHRONIZED_IO	},
145   { "_POSIX_TIMERS",		SYSCONF,	_SC_TIMERS		},
146 
147   { "_POSIX_SYNC_IO",		PATHCONF,	_PC_SYNC_IO		},
148 
149   /* POSIX.1c Configurable System Variables */
150   { "LOGIN_NAME_MAX",		SYSCONF,	_SC_LOGIN_NAME_MAX	},
151   { "_POSIX_THREADS",		SYSCONF,	_SC_THREADS		},
152 
153   /* POSIX.1j Configurable System Variables */
154   { "_POSIX_BARRIERS",		SYSCONF,	_SC_BARRIERS		},
155   { "_POSIX_READER_WRITER_LOCKS", SYSCONF,	_SC_READER_WRITER_LOCKS	},
156   { "_POSIX_SPIN_LOCKS",	SYSCONF,	_SC_SPIN_LOCKS		},
157 
158   /* XPG4.2 Configurable System Variables */
159   { "IOV_MAX",			SYSCONF,	_SC_IOV_MAX		},
160   { "PAGE_SIZE",		SYSCONF,	_SC_PAGE_SIZE		},
161   { "_XOPEN_SHM",		SYSCONF,	_SC_XOPEN_SHM		},
162 
163   /* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */
164   { "FILESIZEBITS",		PATHCONF,	_PC_FILESIZEBITS	},
165 
166   /* POSIX.1-2001 XSI Option Group Configurable System Variables */
167   { "ATEXIT_MAX",		SYSCONF,	_SC_ATEXIT_MAX		},
168 
169   /* POSIX.1-2001 TSF Configurable System Variables */
170   { "GETGR_R_SIZE_MAX",		SYSCONF,	_SC_GETGR_R_SIZE_MAX	},
171   { "GETPW_R_SIZE_MAX",		SYSCONF,	_SC_GETPW_R_SIZE_MAX	},
172 
173   { NULL }
174 };
175 
176 
177 int
178 main(argc, argv)
179 	int argc;
180 	char **argv;
181 {
182 	int ch;
183 	const struct conf_variable *cp;
184 
185 	long val;
186 	size_t slen;
187 	char * sval;
188 
189 	setlocale(LC_ALL, "");
190 
191 	while ((ch = getopt(argc, argv, "")) != -1) {
192 		switch (ch) {
193 		case '?':
194 		default:
195 			usage();
196 		}
197 	}
198 	argc -= optind;
199 	argv += optind;
200 
201 	if (argc < 1 || argc > 2) {
202 		usage();
203 		/* NOTREACHED */
204 	}
205 
206 	for (cp = conf_table; cp->name != NULL; cp++) {
207 		if (strcmp(*argv, cp->name) == 0)
208 			break;
209 	}
210 	if (cp->name == NULL) {
211 		errx(1, "%s: unknown variable", *argv);
212 		/* NOTREACHED */
213 	}
214 
215 	if (cp->type == PATHCONF) {
216 		if (argc != 2) usage();
217 	} else {
218 		if (argc != 1) usage();
219 	}
220 
221 	switch (cp->type) {
222 	case CONSTANT:
223 		printf("%ld\n", cp->value);
224 		break;
225 
226 	case CONFSTR:
227 		slen = confstr (cp->value, (char *) 0, (size_t) 0);
228 
229 		if ((sval = malloc(slen)) == NULL)
230 			err(1, "malloc");
231 
232 		confstr(cp->value, sval, slen);
233 		printf("%s\n", sval);
234 		break;
235 
236 	case SYSCONF:
237 		errno = 0;
238 		if ((val = sysconf(cp->value)) == -1) {
239 			if (errno != 0) {
240 				err(1, "sysconf");
241 				/* NOTREACHED */
242 			}
243 
244 			printf ("undefined\n");
245 		} else {
246 			printf("%ld\n", val);
247 		}
248 		break;
249 
250 	case PATHCONF:
251 		errno = 0;
252 		if ((val = pathconf(argv[1], cp->value)) == -1) {
253 			if (errno != 0) {
254 				err(1, "%s", argv[1]);
255 				/* NOTREACHED */
256 			}
257 
258 			printf ("undefined\n");
259 		} else {
260 			printf ("%ld\n", val);
261 		}
262 		break;
263 	}
264 
265 	exit (ferror(stdout));
266 }
267 
268 
269 static void
270 usage()
271 {
272   fprintf (stderr, "usage: getconf system_var\n");
273   fprintf (stderr, "       getconf path_var pathname\n");
274   exit(1);
275 }
276