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
<paths.h> 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.
This commit is contained in:
Garrett Wollman 2002-07-15 21:51:19 +00:00
parent ef5b639902
commit 97ec79a175
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=100146

View file

@ -38,48 +38,27 @@ static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93";
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <sys/param.h> #include <sys/param.h>
#include <sys/sysctl.h>
#include <errno.h> #include <errno.h>
#include <paths.h> #include <paths.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
size_t size_t
confstr(name, buf, len) confstr(int name, char *buf, size_t len)
int name;
char *buf;
size_t len;
{ {
size_t tlen; const char *p;
int mib[2], sverrno;
char *p;
switch (name) { switch (name) {
case _CS_PATH: case _CS_PATH:
mib[0] = CTL_USER; p = _PATH_STDPATH;
mib[1] = USER_CS_PATH; goto docopy;
if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1)
return (-1); docopy:
if (len != 0 && buf != NULL) { if (len != 0 && buf != NULL)
if ((p = malloc(tlen)) == NULL) strlcpy(buf, p, len);
return (-1); return (strlen(p) + 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);
default: default:
errno = EINVAL; errno = EINVAL;
return (0); return (0);