errno: check only errno if return value was NULL

This commit is contained in:
Hiltjo Posthuma 2014-07-09 14:56:02 +00:00 committed by sin
parent 528f10be6c
commit 96f15f1d04
3 changed files with 18 additions and 14 deletions

13
id.c
View File

@ -42,7 +42,6 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
errno = 0;
switch (argc) { switch (argc) {
case 0: case 0:
userid(getuid()); userid(getuid());
@ -86,10 +85,12 @@ usernam(const char *nam)
errno = 0; errno = 0;
pw = getpwnam(nam); pw = getpwnam(nam);
if (errno != 0) if(!pw) {
if (errno)
eprintf("getpwnam %s:", nam); eprintf("getpwnam %s:", nam);
else if (!pw) else
eprintf("getpwnam %s: no such user\n", nam); eprintf("getpwnam %s: no such user\n", nam);
}
if (Gflag) if (Gflag)
groupid(pw); groupid(pw);
else else
@ -103,10 +104,12 @@ userid(uid_t id)
errno = 0; errno = 0;
pw = getpwuid(id); pw = getpwuid(id);
if (errno != 0) if(!pw) {
if (errno)
eprintf("getpwuid %d:", id); eprintf("getpwuid %d:", id);
else if (!pw) else
eprintf("getpwuid %d: no such user\n", id); eprintf("getpwuid %d: no such user\n", id);
}
if (Gflag) if (Gflag)
groupid(pw); groupid(pw);
else else

4
su.c
View File

@ -55,10 +55,12 @@ main(int argc, char *argv[])
errno = 0; errno = 0;
pw = getpwnam(usr); pw = getpwnam(usr);
if(!pw) {
if (errno) if (errno)
eprintf("getpwnam: %s:", usr); eprintf("getpwnam: %s:", usr);
else if (!pw) else
eprintf("who are you?\n"); eprintf("who are you?\n");
}
uid = getuid(); uid = getuid();
if (uid) { if (uid) {

View File

@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <sched.h> #include <sched.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>