From bb59d2eb34b4f9d15688c22d5fa6822f006fc096 Mon Sep 17 00:00:00 2001 From: sin Date: Mon, 22 Dec 2014 11:22:00 +0000 Subject: [PATCH] ls: No need to set errno to zero Consider the following code: pw = getpwuid(uid); if (!pw) { if (errno) ... else ... } If the entry was not found then as per POSIX errno is not set because that is not considered to be a failing condition. errno is only set if an internal error occurred. If errno happened to be non-zero before the getpwuid() call because of a previous error then we'll report a bogus error. In this case, we have to set errno to zero before the call to getpwuid(). However in ls(1) we only really care if the password entry was found and we do not report any errors so setting errno to 0 is not necessary. --- ls.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/ls.c b/ls.c index c864295..8862b87 100644 --- a/ls.c +++ b/ls.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ #include -#include #include #include #include @@ -273,14 +272,12 @@ output(Entry *ent) if (ent->mode & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S'; if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T'; - errno = 0; pw = getpwuid(ent->uid); if (pw) snprintf(pwname, sizeof(pwname), "%s", pw->pw_name); else snprintf(pwname, sizeof(pwname), "%d", ent->uid); - errno = 0; gr = getgrgid(ent->gid); if (gr) snprintf(grname, sizeof(grname), "%s", gr->gr_name);