Blame bin/getconf.c

Kylie McClain b61f59
/*-
Kylie McClain b61f59
 * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
Kylie McClain b61f59
 * All rights reserved.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * This code is derived from software contributed to The NetBSD Foundation
Kylie McClain b61f59
 * by J.T. Conklin.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * Mostly rewritten to be used in Alpine Linux (with musl c-library)
Kylie McClain b61f59
 * by Timo Teräs.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * Redistribution and use in source and binary forms, with or without
Kylie McClain b61f59
 * modification, are permitted provided that the following conditions
Kylie McClain b61f59
 * are met:
Kylie McClain b61f59
 * 1. Redistributions of source code must retain the above copyright
Kylie McClain b61f59
 *    notice, this list of conditions and the following disclaimer.
Kylie McClain b61f59
 * 2. Redistributions in binary form must reproduce the above copyright
Kylie McClain b61f59
 *    notice, this list of conditions and the following disclaimer in the
Kylie McClain b61f59
 *    documentation and/or other materials provided with the distribution.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
Kylie McClain b61f59
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Kylie McClain b61f59
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Kylie McClain b61f59
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
Kylie McClain b61f59
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Kylie McClain b61f59
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Kylie McClain b61f59
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Kylie McClain b61f59
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Kylie McClain b61f59
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Kylie McClain b61f59
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Kylie McClain b61f59
 * POSSIBILITY OF SUCH DAMAGE.
Kylie McClain b61f59
 */
Kylie McClain b61f59
Kylie McClain b61f59
#include <err.h>
Kylie McClain b61f59
#include <errno.h>
Kylie McClain b61f59
#include <values.h>
Kylie McClain b61f59
#include <limits.h>
Kylie McClain b61f59
#include <locale.h>
Kylie McClain b61f59
#include <stdio.h>
Kylie McClain b61f59
#include <stdlib.h>
Kylie McClain b61f59
#include <unistd.h>
Kylie McClain b61f59
#include <string.h>
Kylie McClain b61f59
Kylie McClain b61f59
struct conf_variable {
Kylie McClain b61f59
	const char *name;
Kylie McClain b61f59
	enum { SYSCONF, CONFSTR, PATHCONF, CONSTANT, UCONSTANT, NUM_TYPES } type;
Kylie McClain b61f59
	long value;
Kylie McClain b61f59
};
Kylie McClain b61f59
Kylie McClain b61f59
static const struct conf_variable conf_table[] = {
Kylie McClain b61f59
{ "PATH",			CONFSTR,	_CS_PATH		},
Kylie McClain b61f59
Kylie McClain b61f59
/* Utility Limit Minimum Values */
Kylie McClain b61f59
{ "POSIX2_BC_BASE_MAX",		CONSTANT,	_POSIX2_BC_BASE_MAX	},
Kylie McClain b61f59
{ "POSIX2_BC_DIM_MAX",		CONSTANT,	_POSIX2_BC_DIM_MAX	},
Kylie McClain b61f59
{ "POSIX2_BC_SCALE_MAX",	CONSTANT,	_POSIX2_BC_SCALE_MAX	},
Kylie McClain b61f59
{ "POSIX2_BC_STRING_MAX",	CONSTANT,	_POSIX2_BC_STRING_MAX	},
Kylie McClain b61f59
{ "POSIX2_COLL_WEIGHTS_MAX",	CONSTANT,	_POSIX2_COLL_WEIGHTS_MAX },
Kylie McClain b61f59
{ "POSIX2_EXPR_NEST_MAX",	CONSTANT,	_POSIX2_EXPR_NEST_MAX	},
Kylie McClain b61f59
{ "POSIX2_LINE_MAX",		CONSTANT,	_POSIX2_LINE_MAX	},
Kylie McClain b61f59
{ "POSIX2_RE_DUP_MAX",		CONSTANT,	_POSIX2_RE_DUP_MAX	},
Kylie McClain b61f59
{ "POSIX2_VERSION",		CONSTANT,	_POSIX2_VERSION		},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1 Minimum Values */
Kylie McClain b61f59
{ "_POSIX_AIO_LISTIO_MAX",	CONSTANT,	_POSIX_AIO_LISTIO_MAX	},
Kylie McClain b61f59
{ "_POSIX_AIO_MAX",		CONSTANT,       _POSIX_AIO_MAX		},
Kylie McClain b61f59
{ "_POSIX_ARG_MAX",		CONSTANT,	_POSIX_ARG_MAX		},
Kylie McClain b61f59
{ "_POSIX_CHILD_MAX",		CONSTANT,	_POSIX_CHILD_MAX	},
Kylie McClain b61f59
{ "_POSIX_LINK_MAX",		CONSTANT,	_POSIX_LINK_MAX		},
Kylie McClain b61f59
{ "_POSIX_MAX_CANON",		CONSTANT,	_POSIX_MAX_CANON	},
Kylie McClain b61f59
{ "_POSIX_MAX_INPUT",		CONSTANT,	_POSIX_MAX_INPUT	},
Kylie McClain b61f59
{ "_POSIX_MQ_OPEN_MAX",		CONSTANT,	_POSIX_MQ_OPEN_MAX	},
Kylie McClain b61f59
{ "_POSIX_MQ_PRIO_MAX",		CONSTANT,	_POSIX_MQ_PRIO_MAX	},
Kylie McClain b61f59
{ "_POSIX_NAME_MAX",		CONSTANT,	_POSIX_NAME_MAX		},
Kylie McClain b61f59
{ "_POSIX_NGROUPS_MAX",		CONSTANT,	_POSIX_NGROUPS_MAX	},
Kylie McClain b61f59
{ "_POSIX_OPEN_MAX",		CONSTANT,	_POSIX_OPEN_MAX		},
Kylie McClain b61f59
{ "_POSIX_PATH_MAX",		CONSTANT,	_POSIX_PATH_MAX		},
Kylie McClain b61f59
{ "_POSIX_PIPE_BUF",		CONSTANT,	_POSIX_PIPE_BUF		},
Kylie McClain b61f59
{ "_POSIX_SSIZE_MAX",		CONSTANT,	_POSIX_SSIZE_MAX	},
Kylie McClain b61f59
{ "_POSIX_STREAM_MAX",		CONSTANT,	_POSIX_STREAM_MAX	},
Kylie McClain b61f59
{ "_POSIX_TZNAME_MAX",		CONSTANT,	_POSIX_TZNAME_MAX	},
Kylie McClain b61f59
Kylie McClain b61f59
/* Symbolic Utility Limits */
Kylie McClain b61f59
{ "BC_BASE_MAX",		SYSCONF,	_SC_BC_BASE_MAX		},
Kylie McClain b61f59
{ "BC_DIM_MAX",			SYSCONF,	_SC_BC_DIM_MAX		},
Kylie McClain b61f59
{ "BC_SCALE_MAX",		SYSCONF,	_SC_BC_SCALE_MAX	},
Kylie McClain b61f59
{ "BC_STRING_MAX",		SYSCONF,	_SC_BC_STRING_MAX	},
Kylie McClain b61f59
{ "COLL_WEIGHTS_MAX",		SYSCONF,	_SC_COLL_WEIGHTS_MAX	},
Kylie McClain b61f59
{ "EXPR_NEST_MAX",		SYSCONF,	_SC_EXPR_NEST_MAX	},
Kylie McClain b61f59
{ "LINE_MAX",			SYSCONF,	_SC_LINE_MAX		},
Kylie McClain b61f59
{ "RE_DUP_MAX",			SYSCONF,	_SC_RE_DUP_MAX		},
Kylie McClain b61f59
Kylie McClain b61f59
/* Optional Facility Configuration Values */
Kylie McClain b61f59
{ "_POSIX2_C_BIND",		SYSCONF,	_SC_2_C_BIND		},
Kylie McClain b61f59
{ "POSIX2_C_DEV",		SYSCONF,	_SC_2_C_DEV		},
Kylie McClain b61f59
{ "POSIX2_CHAR_TERM",		SYSCONF,	_SC_2_CHAR_TERM		},
Kylie McClain b61f59
{ "POSIX2_FORT_DEV",		SYSCONF,	_SC_2_FORT_DEV		},
Kylie McClain b61f59
{ "POSIX2_FORT_RUN",		SYSCONF,	_SC_2_FORT_RUN		},
Kylie McClain b61f59
{ "POSIX2_LOCALEDEF",		SYSCONF,	_SC_2_LOCALEDEF		},
Kylie McClain b61f59
{ "POSIX2_SW_DEV",		SYSCONF,	_SC_2_SW_DEV		},
Kylie McClain b61f59
{ "POSIX2_UPE",			SYSCONF,	_SC_2_UPE		},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1 Configurable System Variables */
Kylie McClain b61f59
{ "AIO_LISTIO_MAX",		SYSCONF,	_SC_AIO_LISTIO_MAX	},
Kylie McClain b61f59
{ "AIO_MAX",			SYSCONF,	_SC_AIO_MAX		},
Kylie McClain b61f59
{ "ARG_MAX",			SYSCONF,	_SC_ARG_MAX 		},
Kylie McClain b61f59
{ "CHILD_MAX",			SYSCONF,	_SC_CHILD_MAX		},
Kylie McClain b61f59
{ "CLK_TCK",			SYSCONF,	_SC_CLK_TCK		},
Kylie McClain b61f59
{ "MQ_OPEN_MAX",		SYSCONF,	_SC_MQ_OPEN_MAX		},
Kylie McClain b61f59
{ "MQ_PRIO_MAX",		SYSCONF,	_SC_MQ_PRIO_MAX		},
Kylie McClain b61f59
{ "NGROUPS_MAX",		SYSCONF,	_SC_NGROUPS_MAX		},
Kylie McClain b61f59
{ "OPEN_MAX",			SYSCONF,	_SC_OPEN_MAX		},
Kylie McClain b61f59
{ "STREAM_MAX",			SYSCONF,	_SC_STREAM_MAX		},
Kylie McClain b61f59
{ "TZNAME_MAX",			SYSCONF,	_SC_TZNAME_MAX		},
Kylie McClain b61f59
{ "_POSIX_JOB_CONTROL",		SYSCONF,	_SC_JOB_CONTROL 	},
Kylie McClain b61f59
{ "_POSIX_SAVED_IDS",		SYSCONF,	_SC_SAVED_IDS		},
Kylie McClain b61f59
{ "_POSIX_VERSION",		SYSCONF,	_SC_VERSION		},
Kylie McClain b61f59
Kylie McClain b61f59
{ "LINK_MAX",			PATHCONF,	_PC_LINK_MAX		},
Kylie McClain b61f59
{ "MAX_CANON",			PATHCONF,	_PC_MAX_CANON		},
Kylie McClain b61f59
{ "MAX_INPUT",			PATHCONF,	_PC_MAX_INPUT		},
Kylie McClain b61f59
{ "NAME_MAX",			PATHCONF,	_PC_NAME_MAX		},
Kylie McClain b61f59
{ "PATH_MAX",			PATHCONF,	_PC_PATH_MAX		},
Kylie McClain b61f59
{ "PIPE_BUF",			PATHCONF,	_PC_PIPE_BUF		},
Kylie McClain b61f59
{ "_POSIX_CHOWN_RESTRICTED",	PATHCONF,	_PC_CHOWN_RESTRICTED	},
Kylie McClain b61f59
{ "_POSIX_NO_TRUNC",		PATHCONF,	_PC_NO_TRUNC		},
Kylie McClain b61f59
{ "_POSIX_VDISABLE",		PATHCONF,	_PC_VDISABLE		},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1b Configurable System Variables */
Kylie McClain b61f59
{ "PAGESIZE",			SYSCONF,	_SC_PAGESIZE		},
Kylie McClain b61f59
{ "_POSIX_ASYNCHRONOUS_IO",	SYSCONF,	_SC_ASYNCHRONOUS_IO	},
Kylie McClain b61f59
{ "_POSIX_FSYNC",		SYSCONF,	_SC_FSYNC		},
Kylie McClain b61f59
{ "_POSIX_MAPPED_FILES",	SYSCONF,	_SC_MAPPED_FILES	},
Kylie McClain b61f59
{ "_POSIX_MEMLOCK",		SYSCONF,	_SC_MEMLOCK		},
Kylie McClain b61f59
{ "_POSIX_MEMLOCK_RANGE",	SYSCONF,	_SC_MEMLOCK_RANGE	},
Kylie McClain b61f59
{ "_POSIX_MEMORY_PROTECTION",	SYSCONF,	_SC_MEMORY_PROTECTION	},
Kylie McClain b61f59
{ "_POSIX_MESSAGE_PASSING",	SYSCONF,	_SC_MESSAGE_PASSING	},
Kylie McClain b61f59
{ "_POSIX_MONOTONIC_CLOCK",	SYSCONF,	_SC_MONOTONIC_CLOCK	},
Kylie McClain b61f59
{ "_POSIX_PRIORITY_SCHEDULING", SYSCONF,	_SC_PRIORITY_SCHEDULING },
Kylie McClain b61f59
{ "_POSIX_SEMAPHORES",		SYSCONF,	_SC_SEMAPHORES		},
Kylie McClain b61f59
{ "_POSIX_SHARED_MEMORY_OBJECTS", SYSCONF,	_SC_SHARED_MEMORY_OBJECTS },
Kylie McClain b61f59
{ "_POSIX_SYNCHRONIZED_IO",	SYSCONF,	_SC_SYNCHRONIZED_IO	},
Kylie McClain b61f59
{ "_POSIX_TIMERS",		SYSCONF,	_SC_TIMERS		},
Kylie McClain b61f59
Kylie McClain b61f59
{ "_POSIX_SYNC_IO",		PATHCONF,	_PC_SYNC_IO		},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1c Configurable System Variables */
Kylie McClain b61f59
{ "LOGIN_NAME_MAX",		SYSCONF,	_SC_LOGIN_NAME_MAX	},
Kylie McClain b61f59
{ "_POSIX_THREADS",		SYSCONF,	_SC_THREADS		},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1j Configurable System Variables */
Kylie McClain b61f59
{ "_POSIX_BARRIERS",		SYSCONF,	_SC_BARRIERS		},
Kylie McClain b61f59
{ "_POSIX_READER_WRITER_LOCKS", SYSCONF,	_SC_READER_WRITER_LOCKS	},
Kylie McClain b61f59
{ "_POSIX_SPIN_LOCKS",		SYSCONF,	_SC_SPIN_LOCKS		},
Kylie McClain b61f59
Kylie McClain b61f59
/* XPG4.2 Configurable System Variables */
Kylie McClain b61f59
{ "IOV_MAX",			SYSCONF,	_SC_IOV_MAX		},
Kylie McClain b61f59
{ "PAGE_SIZE",			SYSCONF,	_SC_PAGE_SIZE		},
Kylie McClain b61f59
{ "_XOPEN_SHM",			SYSCONF,	_SC_XOPEN_SHM		},
Kylie McClain b61f59
Kylie McClain b61f59
/* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */
Kylie McClain b61f59
{ "FILESIZEBITS",		PATHCONF,	_PC_FILESIZEBITS	},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1-2001 XSI Option Group Configurable System Variables */
Kylie McClain b61f59
{ "ATEXIT_MAX",			SYSCONF,	_SC_ATEXIT_MAX		},
Kylie McClain b61f59
Kylie McClain b61f59
/* POSIX.1-2001 TSF Configurable System Variables */
Kylie McClain b61f59
{ "GETGR_R_SIZE_MAX",		SYSCONF,	_SC_GETGR_R_SIZE_MAX	},
Kylie McClain b61f59
{ "GETPW_R_SIZE_MAX",		SYSCONF,	_SC_GETPW_R_SIZE_MAX	},
Kylie McClain b61f59
Kylie McClain b61f59
/* Commonly provided extensions */
Kylie McClain b61f59
{ "_PHYS_PAGES",		SYSCONF,	_SC_PHYS_PAGES		},
Kylie McClain b61f59
{ "_AVPHYS_PAGES",		SYSCONF,	_SC_AVPHYS_PAGES	},
Kylie McClain b61f59
{ "_NPROCESSORS_CONF",		SYSCONF,	_SC_NPROCESSORS_CONF	},
Kylie McClain b61f59
{ "_NPROCESSORS_ONLN",		SYSCONF,	_SC_NPROCESSORS_ONLN	},
Kylie McClain b61f59
Kylie McClain b61f59
/* Data type related extensions */
Kylie McClain b61f59
{ "CHAR_BIT",			CONSTANT,	CHAR_BIT		},
Kylie McClain b61f59
{ "CHAR_MAX",			CONSTANT,	CHAR_MAX		},
Kylie McClain b61f59
{ "CHAR_MIN",			CONSTANT,	CHAR_MIN		},
Kylie McClain b61f59
{ "INT_MAX",			CONSTANT,	INT_MAX			},
Kylie McClain b61f59
{ "INT_MIN",			CONSTANT,	INT_MIN			},
Kylie McClain b61f59
{ "LONG_BIT",			CONSTANT,	LONG_BIT		},
Kylie McClain b61f59
{ "LONG_MAX",			CONSTANT,	LONG_MAX		},
Kylie McClain b61f59
{ "LONG_MIN",			CONSTANT,	LONG_MIN		},
Kylie McClain b61f59
{ "SCHAR_MAX",			CONSTANT,	SCHAR_MAX		},
Kylie McClain b61f59
{ "SCHAR_MIN",			CONSTANT,	SCHAR_MIN		},
Kylie McClain b61f59
{ "SHRT_MAX",			CONSTANT,	SHRT_MAX		},
Kylie McClain b61f59
{ "SHRT_MIN",			CONSTANT,	SHRT_MIN		},
Kylie McClain b61f59
{ "SSIZE_MAX",			CONSTANT,	SSIZE_MAX		},
Kylie McClain b61f59
{ "UCHAR_MAX",			UCONSTANT,	(long) UCHAR_MAX	},
Kylie McClain b61f59
{ "UINT_MAX",			UCONSTANT,	(long) UINT_MAX		},
Kylie McClain b61f59
{ "ULONG_MAX",			UCONSTANT,	(long) ULONG_MAX	},
Kylie McClain b61f59
{ "USHRT_MAX",			UCONSTANT,	(long) USHRT_MAX	},
Kylie McClain b61f59
{ "WORD_BIT",			CONSTANT,	WORD_BIT		},
Kylie McClain b61f59
Kylie McClain b61f59
{ NULL, CONSTANT, 0L }
Kylie McClain b61f59
};
Kylie McClain b61f59
Kylie McClain b61f59
static int all = 0;
Kylie McClain b61f59
Kylie McClain b61f59
static void usage(const char *p)
Kylie McClain b61f59
{
Kylie McClain b61f59
	(void)fprintf(stderr, "Usage: %s system_var\n\t%s -a\n"
Kylie McClain b61f59
	    "\t%s path_var pathname\n\t%s -a pathname\n", p, p, p, p);
Kylie McClain b61f59
	exit(EXIT_FAILURE);
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static void print_long(const char *name, long val)
Kylie McClain b61f59
{
Kylie McClain b61f59
	if (all) printf("%s = %ld\n", name, val);
Kylie McClain b61f59
	else printf("%ld\n", val);
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static void print_ulong(const char *name, unsigned long val)
Kylie McClain b61f59
{
Kylie McClain b61f59
	if (all) printf("%s = %lu\n", name, val);
Kylie McClain b61f59
	else printf("%lu\n", val);
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static void print_string(const char *name, const char *val)
Kylie McClain b61f59
{
Kylie McClain b61f59
	if (all) printf("%s = %s\n", name, val);
Kylie McClain b61f59
	else printf("%s\n", val);
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static int print_constant(const struct conf_variable *cp, const char *pathname)
Kylie McClain b61f59
{
Kylie McClain b61f59
	print_long(cp->name, cp->value);
Kylie McClain b61f59
	return 0;
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static int print_uconstant(const struct conf_variable *cp, const char *pathname)
Kylie McClain b61f59
{
Kylie McClain b61f59
	print_ulong(cp->name, (unsigned long) cp->value);
Kylie McClain b61f59
	return 0;
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static int print_sysconf(const struct conf_variable *cp, const char *pathname)
Kylie McClain b61f59
{
Kylie McClain b61f59
	long val;
Kylie McClain b61f59
Kylie McClain b61f59
	errno = 0;
Kylie McClain b61f59
	if ((val = sysconf((int)cp->value)) == -1) {
Kylie McClain b61f59
		if (errno != 0) err(EXIT_FAILURE, "sysconf(%ld)", cp->value);
Kylie McClain b61f59
		return -1;
Kylie McClain b61f59
	}
Kylie McClain b61f59
	print_long(cp->name, val);
Kylie McClain b61f59
	return 0;
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static int print_confstr(const struct conf_variable *cp, const char *pathname)
Kylie McClain b61f59
{
Kylie McClain b61f59
	size_t len;
Kylie McClain b61f59
	char *val;
Kylie McClain b61f59
Kylie McClain b61f59
	errno = 0;
Kylie McClain b61f59
	if ((len = confstr((int)cp->value, NULL, 0)) == 0) goto error;
Kylie McClain b61f59
	if ((val = malloc(len)) == NULL) err(EXIT_FAILURE, "Can't allocate %zu bytes", len);
Kylie McClain b61f59
	errno = 0;
Kylie McClain b61f59
	if (confstr((int)cp->value, val, len) == 0) goto error;
Kylie McClain b61f59
	print_string(cp->name, val);
Kylie McClain b61f59
	free(val);
Kylie McClain b61f59
	return 0;
Kylie McClain b61f59
error:
Kylie McClain b61f59
	if (errno != EINVAL) err(EXIT_FAILURE, "confstr(%ld)", cp->value);
Kylie McClain b61f59
	return -1;
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
static int print_pathconf(const struct conf_variable *cp, const char *pathname)
Kylie McClain b61f59
{
Kylie McClain b61f59
	long val;
Kylie McClain b61f59
Kylie McClain b61f59
	errno = 0;
Kylie McClain b61f59
	if ((val = pathconf(pathname, (int)cp->value)) == -1) {
Kylie McClain b61f59
		if (all && errno == EINVAL) return 0;
Kylie McClain b61f59
		if (errno != 0) err(EXIT_FAILURE, "pathconf(%s, %ld)", pathname, cp->value);
Kylie McClain b61f59
		return -1;
Kylie McClain b61f59
	}
Kylie McClain b61f59
	print_long(cp->name, val);
Kylie McClain b61f59
	return 0;
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
typedef int (*handler_t)(const struct conf_variable *cp, const char *pathname);
Kylie McClain b61f59
static const handler_t type_handlers[NUM_TYPES] = {
Kylie McClain b61f59
	[SYSCONF]	= print_sysconf,
Kylie McClain b61f59
	[CONFSTR]	= print_confstr,
Kylie McClain b61f59
	[PATHCONF]	= print_pathconf,
Kylie McClain b61f59
	[CONSTANT]	= print_constant,
Kylie McClain b61f59
	[UCONSTANT]	= print_uconstant,
Kylie McClain b61f59
};
Kylie McClain b61f59
Kylie McClain b61f59
int main(int argc, char **argv)
Kylie McClain b61f59
{
Kylie McClain b61f59
	const char *progname = argv[0];
Kylie McClain b61f59
	const struct conf_variable *cp;
Kylie McClain b61f59
	const char *varname, *pathname;
Kylie McClain b61f59
	int ch, found = 0;
Kylie McClain b61f59
Kylie McClain b61f59
	(void)setlocale(LC_ALL, "");
Kylie McClain b61f59
	while ((ch = getopt(argc, argv, "a")) != -1) {
Kylie McClain b61f59
		switch (ch) {
Kylie McClain b61f59
		case 'a':
Kylie McClain b61f59
			all = 1;
Kylie McClain b61f59
			break;
Kylie McClain b61f59
		case '?':
Kylie McClain b61f59
		default:
Kylie McClain b61f59
			usage(progname);
Kylie McClain b61f59
		}
Kylie McClain b61f59
	}
Kylie McClain b61f59
	argc -= optind;
Kylie McClain b61f59
	argv += optind;
Kylie McClain b61f59
Kylie McClain b61f59
	if (!all) {
Kylie McClain b61f59
		if (argc == 0)
Kylie McClain b61f59
			usage(progname);
Kylie McClain b61f59
		varname = argv[0];
Kylie McClain b61f59
		argc--;
Kylie McClain b61f59
		argv++;
Kylie McClain b61f59
	} else
Kylie McClain b61f59
		varname = NULL;
Kylie McClain b61f59
Kylie McClain b61f59
	if (argc > 1)
Kylie McClain b61f59
		usage(progname);
Kylie McClain b61f59
	pathname = argv[0];	/* may be NULL */
Kylie McClain b61f59
Kylie McClain b61f59
	for (cp = conf_table; cp->name != NULL; cp++) {
Kylie McClain b61f59
		if (!all && strcmp(varname, cp->name) != 0) continue;
Kylie McClain b61f59
		if ((cp->type == PATHCONF) == (pathname != NULL)) {
Kylie McClain b61f59
			if (type_handlers[cp->type](cp, pathname) < 0)
Kylie McClain b61f59
				print_string(cp->name, "undefined");
Kylie McClain b61f59
			found = 1;
Kylie McClain b61f59
		} else if (!all)
Kylie McClain b61f59
			errx(EXIT_FAILURE, "%s: invalid variable type", cp->name);
Kylie McClain b61f59
	}
Kylie McClain b61f59
	if (!all && !found) errx(EXIT_FAILURE, "%s: unknown variable", varname);
Kylie McClain b61f59
	(void)fflush(stdout);
Kylie McClain b61f59
	return ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS;
Kylie McClain b61f59
}