sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit bb59d2eb34b4f9d15688c22d5fa6822f006fc096
parent 32651cb2da5b04e2fef1eef075374ef79e622ade
Author: sin <sin@2f30.org>
Date:   Mon, 22 Dec 2014 11:22:00 +0000

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.

Diffstat:
Mls.c | 3---
1 file changed, 0 insertions(+), 3 deletions(-)

diff --git a/ls.c b/ls.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ #include <dirent.h> -#include <errno.h> #include <grp.h> #include <pwd.h> #include <stdio.h> @@ -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);