11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311573Srgrimesstatic char sccsid[] = "@(#)sysctl.c	8.2 (Berkeley) 1/4/94";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3392986Sobrien#include <sys/cdefs.h>
3492986Sobrien__FBSDID("$FreeBSD$");
351573Srgrimes
361573Srgrimes#include <sys/param.h>
371573Srgrimes#include <sys/sysctl.h>
381573Srgrimes
391573Srgrimes#include <errno.h>
401573Srgrimes#include <limits.h>
411573Srgrimes#include <paths.h>
421573Srgrimes#include <stdio.h>
431573Srgrimes#include <unistd.h>
4411659Sphk#include <string.h>
451573Srgrimes
46204170Sedextern int __sysctl(const int *name, u_int namelen, void *oldp,
47204170Sed    size_t *oldlenp, const void *newp, size_t newlen);
48111010Snectar
491573Srgrimesint
50204170Sedsysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
51204170Sed    const void *newp, size_t newlen)
521573Srgrimes{
531573Srgrimes	if (name[0] != CTL_USER)
541573Srgrimes		return (__sysctl(name, namelen, oldp, oldlenp, newp, newlen));
551573Srgrimes
561573Srgrimes	if (newp != NULL) {
571573Srgrimes		errno = EPERM;
581573Srgrimes		return (-1);
591573Srgrimes	}
601573Srgrimes	if (namelen != 2) {
611573Srgrimes		errno = EINVAL;
621573Srgrimes		return (-1);
631573Srgrimes	}
641573Srgrimes
651573Srgrimes	switch (name[1]) {
661573Srgrimes	case USER_CS_PATH:
6742353Sdes		if (oldp && *oldlenp < sizeof(_PATH_STDPATH)) {
6842353Sdes			errno = ENOMEM;
6942353Sdes			return -1;
7042353Sdes		}
711573Srgrimes		*oldlenp = sizeof(_PATH_STDPATH);
721573Srgrimes		if (oldp != NULL)
731573Srgrimes			memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH));
741573Srgrimes		return (0);
751573Srgrimes	}
761573Srgrimes
7742353Sdes	if (oldp && *oldlenp < sizeof(int)) {
7842353Sdes		errno = ENOMEM;
7942353Sdes		return (-1);
8042353Sdes	}
811573Srgrimes	*oldlenp = sizeof(int);
821573Srgrimes	if (oldp == NULL)
831573Srgrimes		return (0);
841573Srgrimes
851573Srgrimes	switch (name[1]) {
861573Srgrimes	case USER_BC_BASE_MAX:
871573Srgrimes		*(int *)oldp = BC_BASE_MAX;
881573Srgrimes		return (0);
891573Srgrimes	case USER_BC_DIM_MAX:
901573Srgrimes		*(int *)oldp = BC_DIM_MAX;
911573Srgrimes		return (0);
921573Srgrimes	case USER_BC_SCALE_MAX:
931573Srgrimes		*(int *)oldp = BC_SCALE_MAX;
941573Srgrimes		return (0);
951573Srgrimes	case USER_BC_STRING_MAX:
961573Srgrimes		*(int *)oldp = BC_STRING_MAX;
971573Srgrimes		return (0);
981573Srgrimes	case USER_COLL_WEIGHTS_MAX:
991573Srgrimes		*(int *)oldp = COLL_WEIGHTS_MAX;
1001573Srgrimes		return (0);
1011573Srgrimes	case USER_EXPR_NEST_MAX:
1021573Srgrimes		*(int *)oldp = EXPR_NEST_MAX;
1031573Srgrimes		return (0);
1041573Srgrimes	case USER_LINE_MAX:
1051573Srgrimes		*(int *)oldp = LINE_MAX;
1061573Srgrimes		return (0);
1071573Srgrimes	case USER_RE_DUP_MAX:
1081573Srgrimes		*(int *)oldp = RE_DUP_MAX;
1091573Srgrimes		return (0);
1101573Srgrimes	case USER_POSIX2_VERSION:
1111573Srgrimes		*(int *)oldp = _POSIX2_VERSION;
1121573Srgrimes		return (0);
1131573Srgrimes	case USER_POSIX2_C_BIND:
1141573Srgrimes#ifdef POSIX2_C_BIND
1151573Srgrimes		*(int *)oldp = 1;
1161573Srgrimes#else
1171573Srgrimes		*(int *)oldp = 0;
1181573Srgrimes#endif
1191573Srgrimes		return (0);
1201573Srgrimes	case USER_POSIX2_C_DEV:
1211573Srgrimes#ifdef	POSIX2_C_DEV
1221573Srgrimes		*(int *)oldp = 1;
1231573Srgrimes#else
1241573Srgrimes		*(int *)oldp = 0;
1251573Srgrimes#endif
1261573Srgrimes		return (0);
1271573Srgrimes	case USER_POSIX2_CHAR_TERM:
1281573Srgrimes#ifdef	POSIX2_CHAR_TERM
1291573Srgrimes		*(int *)oldp = 1;
1301573Srgrimes#else
1311573Srgrimes		*(int *)oldp = 0;
1321573Srgrimes#endif
1331573Srgrimes		return (0);
1341573Srgrimes	case USER_POSIX2_FORT_DEV:
1351573Srgrimes#ifdef	POSIX2_FORT_DEV
1361573Srgrimes		*(int *)oldp = 1;
1371573Srgrimes#else
1381573Srgrimes		*(int *)oldp = 0;
1391573Srgrimes#endif
1401573Srgrimes		return (0);
1411573Srgrimes	case USER_POSIX2_FORT_RUN:
1421573Srgrimes#ifdef	POSIX2_FORT_RUN
1431573Srgrimes		*(int *)oldp = 1;
1441573Srgrimes#else
1451573Srgrimes		*(int *)oldp = 0;
1461573Srgrimes#endif
1471573Srgrimes		return (0);
1481573Srgrimes	case USER_POSIX2_LOCALEDEF:
1491573Srgrimes#ifdef	POSIX2_LOCALEDEF
1501573Srgrimes		*(int *)oldp = 1;
1511573Srgrimes#else
1521573Srgrimes		*(int *)oldp = 0;
1531573Srgrimes#endif
1541573Srgrimes		return (0);
1551573Srgrimes	case USER_POSIX2_SW_DEV:
1561573Srgrimes#ifdef	POSIX2_SW_DEV
1571573Srgrimes		*(int *)oldp = 1;
1581573Srgrimes#else
1591573Srgrimes		*(int *)oldp = 0;
1601573Srgrimes#endif
1611573Srgrimes		return (0);
1621573Srgrimes	case USER_POSIX2_UPE:
1631573Srgrimes#ifdef	POSIX2_UPE
1641573Srgrimes		*(int *)oldp = 1;
1651573Srgrimes#else
1661573Srgrimes		*(int *)oldp = 0;
1671573Srgrimes#endif
1681573Srgrimes		return (0);
1691573Srgrimes	case USER_STREAM_MAX:
1701573Srgrimes		*(int *)oldp = FOPEN_MAX;
1711573Srgrimes		return (0);
1721573Srgrimes	case USER_TZNAME_MAX:
1731573Srgrimes		*(int *)oldp = NAME_MAX;
1741573Srgrimes		return (0);
1751573Srgrimes	default:
1761573Srgrimes		errno = EINVAL;
1771573Srgrimes		return (-1);
1781573Srgrimes	}
1791573Srgrimes	/* NOTREACHED */
1801573Srgrimes}
181