xref: /original-bsd/lib/libc/gen/confstr.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)confstr.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <sys/sysctl.h>
14 
15 #include <errno.h>
16 #include <paths.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 
20 size_t
21 confstr(name, buf, len)
22 	int name;
23 	char *buf;
24 	size_t len;
25 {
26 	size_t tlen;
27 	int mib[2], sverrno;
28 	char *p;
29 
30 	switch (name) {
31 	case _CS_PATH:
32 		mib[0] = CTL_USER;
33 		mib[1] = USER_CS_PATH;
34 		if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1)
35 			return (-1);
36 		if (len != 0 && buf != NULL) {
37 			if ((p = malloc(tlen)) == NULL)
38 				return (-1);
39 			if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) {
40 				sverrno = errno;
41 				free(p);
42 				errno = sverrno;
43 				return (-1);
44 			}
45 			/*
46 			 * POSIX 1003.2 requires partial return of
47 			 * the string -- that should be *real* useful.
48 			 */
49 			(void)strncpy(buf, p, len - 1);
50 			buf[len - 1] = '\0';
51 			free(p);
52 		}
53 		return (tlen + 1);
54 	default:
55 		errno = EINVAL;
56 		return (0);
57 	}
58 	/* NOTREACHED */
59 }
60