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

21
id.c
View File

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

10
su.c
View File

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

View File

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