Don't bail out if the username/group name does not exist in ls(1)

This commit is contained in:
sin 2014-04-05 19:22:56 +01:00
parent 81f9cf2a33
commit e3c62682ff

22
ls.c
View File

@ -218,6 +218,8 @@ output(Entry *ent)
ssize_t len; ssize_t len;
struct group *gr; struct group *gr;
struct passwd *pw; struct passwd *pw;
char pwname[_SC_LOGIN_NAME_MAX];
char grname[_SC_LOGIN_NAME_MAX];
Entry entlnk; Entry entlnk;
if (iflag) if (iflag)
@ -259,17 +261,17 @@ output(Entry *ent)
errno = 0; errno = 0;
pw = getpwuid(ent->uid); pw = getpwuid(ent->uid);
if(errno) if(errno || !pw)
eprintf("getpwuid %d:", ent->uid); snprintf(pwname, sizeof(pwname), "%d", ent->uid);
else if(!pw) else
eprintf("getpwuid %d: no such user\n", ent->uid); snprintf(pwname, sizeof(pwname), "%s", pw->pw_name);
errno = 0; errno = 0;
gr = getgrgid(ent->gid); gr = getgrgid(ent->gid);
if(errno) if(errno || !gr)
eprintf("getgrgid %d:", ent->gid); snprintf(grname, sizeof(grname), "%d", ent->gid);
else if(!gr) else
eprintf("getgrgid %d: no such group\n", ent->gid); snprintf(grname, sizeof(grname), "%s", gr->gr_name);
if(time(NULL) > ent->mtime + (180*24*60*60)) /* 6 months ago? */ if(time(NULL) > ent->mtime + (180*24*60*60)) /* 6 months ago? */
fmt = "%b %d %Y"; fmt = "%b %d %Y";
@ -277,8 +279,8 @@ output(Entry *ent)
fmt = "%b %d %H:%M"; fmt = "%b %d %H:%M";
strftime(buf, sizeof buf, fmt, localtime(&ent->mtime)); strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
printf("%s %2ld %-4s %-5s %6lu %s %s%s", mode, (long)ent->nlink, pw->pw_name, printf("%s %2ld %-4s %-5s %6lu %s %s%s", mode, (long)ent->nlink, pwname,
gr->gr_name, (unsigned long)ent->size, buf, ent->name, indicator(ent->mode)); grname, (unsigned long)ent->size, buf, ent->name, indicator(ent->mode));
if(S_ISLNK(ent->mode)) { if(S_ISLNK(ent->mode)) {
if((len = readlink(ent->name, buf, sizeof buf)) == -1) if((len = readlink(ent->name, buf, sizeof buf)) == -1)
eprintf("readlink %s:", ent->name); eprintf("readlink %s:", ent->name);