[hackers] [sbase] ls: No need to set errno to zero || sin
commit bb59d2eb34b4f9d15688c22d5fa6822f006fc096
Author: sin <sin_AT_2f30.org>
Date: Mon Dec 22 11:22:00 2014 +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.
diff --git a/ls.c b/ls.c
index c864295..8862b87 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -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>
_AT_@ -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);
Received on Mon Dec 22 2014 - 12:28:26 CET
This archive was generated by hypermail 2.3.0
: Mon Dec 22 2014 - 12:36:12 CET