xref: /csrg-svn/lib/libc/gen/confstr.c (revision 59720)
159694Sbostic /*-
259694Sbostic  * Copyright (c) 1993 The Regents of the University of California.
359694Sbostic  * All rights reserved.
459694Sbostic  *
559694Sbostic  * %sccs.include.redist.c%
659694Sbostic  */
759694Sbostic 
859694Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*59720Sbostic static char sccsid[] = "@(#)confstr.c	5.3 (Berkeley) 05/04/93";
1059694Sbostic #endif /* LIBC_SCCS and not lint */
1159694Sbostic 
12*59720Sbostic #include <sys/param.h>
13*59720Sbostic #include <sys/sysctl.h>
14*59720Sbostic 
1559694Sbostic #include <errno.h>
1659694Sbostic #include <paths.h>
17*59720Sbostic #include <stdlib.h>
1859694Sbostic #include <unistd.h>
1959694Sbostic 
2059694Sbostic size_t
2159694Sbostic confstr(name, buf, len)
2259694Sbostic 	int name;
2359694Sbostic 	char *buf;
2459694Sbostic 	size_t len;
2559694Sbostic {
26*59720Sbostic 	size_t tlen;
27*59720Sbostic 	int mib[2], sverrno;
28*59720Sbostic 	char *p;
29*59720Sbostic 
3059694Sbostic 	switch (name) {
3159694Sbostic 	case _CS_PATH:
32*59720Sbostic 		mib[0] = CTL_USER;
33*59720Sbostic 		mib[1] = USER_CS_PATH;
34*59720Sbostic 		if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1)
35*59720Sbostic 			return (-1);
3659694Sbostic 		if (len != 0 && buf != NULL) {
37*59720Sbostic 			if ((p = malloc(tlen)) == NULL)
38*59720Sbostic 				return (-1);
39*59720Sbostic 			if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) {
40*59720Sbostic 				sverrno = errno;
41*59720Sbostic 				free(p);
42*59720Sbostic 				errno = sverrno;
43*59720Sbostic 				return (-1);
44*59720Sbostic 			}
45*59720Sbostic 			/*
46*59720Sbostic 			 * POSIX 1003.2 requires partial return of
47*59720Sbostic 			 * the string -- that should be *real* useful.
48*59720Sbostic 			 */
49*59720Sbostic 			(void)strncpy(buf, p, len - 1);
5059694Sbostic 			buf[len - 1] = '\0';
51*59720Sbostic 			free(p);
5259694Sbostic 		}
53*59720Sbostic 		return (tlen);
5459694Sbostic 	default:
5559694Sbostic 		errno = EINVAL;
5659694Sbostic 		return (0);
5759694Sbostic 	}
5859694Sbostic 	/* NOTREACHED */
5959694Sbostic }
60