PR/35170: Guy Harris: confstr() should return 0, not -1, on errors

This commit is contained in:
christos 2006-12-03 00:39:19 +00:00
parent b9d66ca8f1
commit 8b3eb79f93
2 changed files with 18 additions and 11 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: confstr.3,v 1.15 2003/08/07 16:42:46 agc Exp $ .\" $NetBSD: confstr.3,v 1.16 2006/12/03 00:39:19 christos Exp $
.\" .\"
.\" Copyright (c) 1993 .\" Copyright (c) 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\" .\"
.\" @(#)confstr.3 8.1 (Berkeley) 6/4/93 .\" @(#)confstr.3 8.1 (Berkeley) 6/4/93
.\" .\"
.Dd June 4, 1993 .Dd December 2, 2006
.Dt CONFSTR 3 .Dt CONFSTR 3
.Os .Os
.Sh NAME .Sh NAME
@ -86,7 +86,7 @@ environment variable that finds all the standard utilities.
.Sh RETURN VALUES .Sh RETURN VALUES
If the call to If the call to
.Nm confstr .Nm confstr
is not successful, ((size_t)-1) is returned and is not successful, is returned and
.Va errno .Va errno
is set appropriately. is set appropriately.
Otherwise, if the variable does not have a configuration defined value, Otherwise, if the variable does not have a configuration defined value,
@ -124,6 +124,9 @@ The
.Nm confstr .Nm confstr
function conforms to function conforms to
.St -p1003.2-92 . .St -p1003.2-92 .
.Sh BUGS
The standards require us to return 0 both on errors, and when the value
is not set.
.Sh HISTORY .Sh HISTORY
The The
.Nm confstr .Nm confstr

View File

@ -1,4 +1,4 @@
/* $NetBSD: confstr.c,v 1.11 2003/08/07 16:42:46 agc Exp $ */ /* $NetBSD: confstr.c,v 1.12 2006/12/03 00:39:19 christos Exp $ */
/*- /*-
* Copyright (c) 1993 * Copyright (c) 1993
@ -34,7 +34,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: confstr.c,v 1.11 2003/08/07 16:42:46 agc Exp $"); __RCSID("$NetBSD: confstr.c,v 1.12 2006/12/03 00:39:19 christos Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -62,33 +62,37 @@ confstr(name, buf, len)
int mib[2], sverrno; int mib[2], sverrno;
char *p; char *p;
/*
* POSIX 1003.2 requires errors to return 0 --
* that is *really* useful.
*/
switch (name) { switch (name) {
case _CS_PATH: case _CS_PATH:
mib[0] = CTL_USER; mib[0] = CTL_USER;
mib[1] = USER_CS_PATH; mib[1] = USER_CS_PATH;
if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1) if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1)
return (size_t)-1; return 0;
if (len != 0 && buf != NULL) { if (len != 0 && buf != NULL) {
if ((p = malloc(tlen)) == NULL) if ((p = malloc(tlen)) == NULL)
return (size_t)-1; return 0;
if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) { if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) {
sverrno = errno; sverrno = errno;
free(p); free(p);
errno = sverrno; errno = sverrno;
return (size_t)-1; return 0;
} }
/* /*
* POSIX 1003.2 requires partial return of * POSIX 1003.2 requires partial return of
* the string -- that should be *real* useful. * the string -- that is even more useful.
*/ */
(void)strncpy(buf, p, len - 1); (void)strncpy(buf, p, len - 1);
buf[len - 1] = '\0'; buf[len - 1] = '\0';
free(p); free(p);
} }
return (tlen + 1); return tlen + 1;
default: default:
errno = EINVAL; errno = EINVAL;
return (0); return 0;
} }
/* NOTREACHED */ /* NOTREACHED */
} }