Home | History | Annotate | Line # | Download | only in getconf
getconf.c revision 1.20
      1 /*	$NetBSD: getconf.c,v 1.20 2003/02/02 20:33:10 kleink 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.20 2003/02/02 20:33:10 kleink 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   { NULL }
    169 };
    170 
    171 
    172 int
    173 main(argc, argv)
    174 	int argc;
    175 	char **argv;
    176 {
    177 	int ch;
    178 	const struct conf_variable *cp;
    179 
    180 	long val;
    181 	size_t slen;
    182 	char * sval;
    183 
    184 	setlocale(LC_ALL, "");
    185 
    186 	while ((ch = getopt(argc, argv, "")) != -1) {
    187 		switch (ch) {
    188 		case '?':
    189 		default:
    190 			usage();
    191 		}
    192 	}
    193 	argc -= optind;
    194 	argv += optind;
    195 
    196 	if (argc < 1 || argc > 2) {
    197 		usage();
    198 		/* NOTREACHED */
    199 	}
    200 
    201 	for (cp = conf_table; cp->name != NULL; cp++) {
    202 		if (strcmp(*argv, cp->name) == 0)
    203 			break;
    204 	}
    205 	if (cp->name == NULL) {
    206 		errx(1, "%s: unknown variable", *argv);
    207 		/* NOTREACHED */
    208 	}
    209 
    210 	if (cp->type == PATHCONF) {
    211 		if (argc != 2) usage();
    212 	} else {
    213 		if (argc != 1) usage();
    214 	}
    215 
    216 	switch (cp->type) {
    217 	case CONSTANT:
    218 		printf("%ld\n", cp->value);
    219 		break;
    220 
    221 	case CONFSTR:
    222 		slen = confstr (cp->value, (char *) 0, (size_t) 0);
    223 
    224 		if ((sval = malloc(slen)) == NULL)
    225 			err(1, "malloc");
    226 
    227 		confstr(cp->value, sval, slen);
    228 		printf("%s\n", sval);
    229 		break;
    230 
    231 	case SYSCONF:
    232 		errno = 0;
    233 		if ((val = sysconf(cp->value)) == -1) {
    234 			if (errno != 0) {
    235 				err(1, "malloc");
    236 				/* NOTREACHED */
    237 			}
    238 
    239 			printf ("undefined\n");
    240 		} else {
    241 			printf("%ld\n", val);
    242 		}
    243 		break;
    244 
    245 	case PATHCONF:
    246 		errno = 0;
    247 		if ((val = pathconf(argv[1], cp->value)) == -1) {
    248 			if (errno != 0) {
    249 				err(1, "%s", argv[1]);
    250 				/* NOTREACHED */
    251 			}
    252 
    253 			printf ("undefined\n");
    254 		} else {
    255 			printf ("%ld\n", val);
    256 		}
    257 		break;
    258 	}
    259 
    260 	exit (ferror(stdout));
    261 }
    262 
    263 
    264 static void
    265 usage()
    266 {
    267   fprintf (stderr, "usage: getconf system_var\n");
    268   fprintf (stderr, "       getconf path_var pathname\n");
    269   exit(1);
    270 }
    271