This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/3195] New: getpwnam() wrongly sets errno for "not found" case
- From: "michael dot kerrisk at gmx dot net" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 12 Sep 2006 04:01:34 -0000
- Subject: [Bug libc/3195] New: getpwnam() wrongly sets errno for "not found" case
- Reply-to: sourceware-bugzilla at sourceware dot org
My reading of POSIX is that if getpwnam() does not find a match for a username,
then it should return NULL and leave errno unchanged. This allows the caller to
distinguish the "not found" and "error" cases as follows:
struct passwd *pwd;
errno = 0;
pwd = getpwnam(name);
if (pwd == NULL) {
if (errno == 0)
/* Not found */;
else
/* Error */;
}
However for this case, glibc's getpwnam() returns NULL and sets errno to ENOENT.
(Complete test program provided below.) Glibc's behaviour should probably
change to match SUSv3, and also majority Unix practice:
The following implementations all return NULL and set errno to 0 (as requried by
POSIX) for this case:
Solaris 7 (and probably later, but I haven't checked)
HP-UX 11
Tru64 5.1B
FreeBSD 4.8 (and probably later, but I haven't checked)
AIX 5.1 returns NULL and sets errno to ESRCH.
Irix 6.5 returns NULL and sets errno to ENOENT (i.e., same as Linux).
Similar remarks apply for getpwuid(), getgrnam(), getgrgid(), and, for
consistency, the unstandardised getspnam().
PS I suppose the following Austin ERN is also relevant:
http://www.opengroup.org/austin/interps/uploads/40/9980/AI-081.txt
/* t_getpwnam.c */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
int
main(int argc, char *argv[])
{
struct passwd *pwd;
if (argc != 2 || strcmp(argv[1], "--help") == 0) {
fprintf(stderr, "Usage: %s username\n", argv[0]);
exit(EXIT_FAILURE);
}
errno = 0;
pwd = getpwnam(argv[1]);
if (pwd == NULL) {
if (errno == 0)
fprintf(stderr, "Username not found\n");
else
perror("getpwnam");
exit(EXIT_FAILURE);
}
printf("Name: %s\n", pwd->pw_name);
printf("Password: %s\n", pwd->pw_passwd);
printf("UID: %ld\n", (long) pwd->pw_uid);
printf("GID: %ld\n", (long) pwd->pw_gid);
printf("Dir: %s\n", pwd->pw_dir);
printf("Gecos: %s\n", pwd->pw_gecos);
printf("Shell: %s\n", pwd->pw_shell);
exit(EXIT_SUCCESS);
} /* main */
--
Summary: getpwnam() wrongly sets errno for "not found" case
Product: glibc
Version: 2.4
Status: NEW
Severity: normal
Priority: P2
Component: libc
AssignedTo: drepper at redhat dot com
ReportedBy: michael dot kerrisk at gmx dot net
CC: glibc-bugs at sources dot redhat dot com
http://sourceware.org/bugzilla/show_bug.cgi?id=3195
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.