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.
This commit is contained in:
sin 2014-12-22 11:22:00 +00:00
parent 32651cb2da
commit bb59d2eb34

3
ls.c
View File

@ -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);