From 97ec79a17552afbb6cbbe5e63f54fd64031cacf9 Mon Sep 17 00:00:00 2001 From: Garrett Wollman Date: Mon, 15 Jul 2002 21:51:19 +0000 Subject: All of the things that confstr() returns are compile-time constants. It's silly to call sysctl() to get the value of _PATH_STDPATH from when we can just use it directly. This greatly simplifies the implementation. (This is also part of my grand scheme to get rid of sysctl's `user' category, which should never have been created.) Use strlcpy() instead of strncpy() as it has the exact semantics we want. --- lib/libc/gen/confstr.c | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/lib/libc/gen/confstr.c b/lib/libc/gen/confstr.c index 753287dbc8b43..ad2e5a0959c3d 100644 --- a/lib/libc/gen/confstr.c +++ b/lib/libc/gen/confstr.c @@ -38,48 +38,27 @@ static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93"; __FBSDID("$FreeBSD$"); #include -#include #include #include -#include #include #include size_t -confstr(name, buf, len) - int name; - char *buf; - size_t len; +confstr(int name, char *buf, size_t len) { - size_t tlen; - int mib[2], sverrno; - char *p; + const char *p; switch (name) { case _CS_PATH: - mib[0] = CTL_USER; - mib[1] = USER_CS_PATH; - if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1) - return (-1); - if (len != 0 && buf != NULL) { - if ((p = malloc(tlen)) == NULL) - return (-1); - if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) { - sverrno = errno; - free(p); - errno = sverrno; - return (-1); - } - /* - * POSIX 1003.2 requires partial return of - * the string -- that should be *real* useful. - */ - (void)strncpy(buf, p, len - 1); - buf[len - 1] = '\0'; - free(p); - } - return (tlen + 1); + p = _PATH_STDPATH; + goto docopy; + +docopy: + if (len != 0 && buf != NULL) + strlcpy(buf, p, len); + return (strlen(p) + 1); + default: errno = EINVAL; return (0); -- cgit v1.2.3