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:
3
ls.c
3
ls.c
@@ -1,6 +1,5 @@
|
|||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.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_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
|
||||||
if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
|
if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
pw = getpwuid(ent->uid);
|
pw = getpwuid(ent->uid);
|
||||||
if (pw)
|
if (pw)
|
||||||
snprintf(pwname, sizeof(pwname), "%s", pw->pw_name);
|
snprintf(pwname, sizeof(pwname), "%s", pw->pw_name);
|
||||||
else
|
else
|
||||||
snprintf(pwname, sizeof(pwname), "%d", ent->uid);
|
snprintf(pwname, sizeof(pwname), "%d", ent->uid);
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
gr = getgrgid(ent->gid);
|
gr = getgrgid(ent->gid);
|
||||||
if (gr)
|
if (gr)
|
||||||
snprintf(grname, sizeof(grname), "%s", gr->gr_name);
|
snprintf(grname, sizeof(grname), "%s", gr->gr_name);
|
||||||
|
|||||||
Reference in New Issue
Block a user