xref: /csrg-svn/lib/libc/gen/confstr.c (revision 61111)
159694Sbostic /*-
2*61111Sbostic  * Copyright (c) 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
459694Sbostic  *
559694Sbostic  * %sccs.include.redist.c%
659694Sbostic  */
759694Sbostic 
859694Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)confstr.c	8.1 (Berkeley) 06/04/93";
1059694Sbostic #endif /* LIBC_SCCS and not lint */
1159694Sbostic 
1259720Sbostic #include <sys/param.h>
1359720Sbostic #include <sys/sysctl.h>
1459720Sbostic 
1559694Sbostic #include <errno.h>
1659694Sbostic #include <paths.h>
1759720Sbostic #include <stdlib.h>
1859694Sbostic #include <unistd.h>
1959694Sbostic 
2059694Sbostic size_t
confstr(name,buf,len)2159694Sbostic confstr(name, buf, len)
2259694Sbostic 	int name;
2359694Sbostic 	char *buf;
2459694Sbostic 	size_t len;
2559694Sbostic {
2659720Sbostic 	size_t tlen;
2759720Sbostic 	int mib[2], sverrno;
2859720Sbostic 	char *p;
2959720Sbostic 
3059694Sbostic 	switch (name) {
3159694Sbostic 	case _CS_PATH:
3259720Sbostic 		mib[0] = CTL_USER;
3359720Sbostic 		mib[1] = USER_CS_PATH;
3459720Sbostic 		if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1)
3559720Sbostic 			return (-1);
3659694Sbostic 		if (len != 0 && buf != NULL) {
3759720Sbostic 			if ((p = malloc(tlen)) == NULL)
3859720Sbostic 				return (-1);
3959720Sbostic 			if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) {
4059720Sbostic 				sverrno = errno;
4159720Sbostic 				free(p);
4259720Sbostic 				errno = sverrno;
4359720Sbostic 				return (-1);
4459720Sbostic 			}
4559720Sbostic 			/*
4659720Sbostic 			 * POSIX 1003.2 requires partial return of
4759720Sbostic 			 * the string -- that should be *real* useful.
4859720Sbostic 			 */
4959720Sbostic 			(void)strncpy(buf, p, len - 1);
5059694Sbostic 			buf[len - 1] = '\0';
5159720Sbostic 			free(p);
5259694Sbostic 		}
5360280Sbostic 		return (tlen + 1);
5459694Sbostic 	default:
5559694Sbostic 		errno = EINVAL;
5659694Sbostic 		return (0);
5759694Sbostic 	}
5859694Sbostic 	/* NOTREACHED */
5959694Sbostic }
60