Combine file mode check

This commit is contained in:
Arun Prakash Jana 2020-02-22 17:10:29 +05:30
parent f61323cccd
commit f2ba23bb3a
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -3100,38 +3100,39 @@ static char *coolsize(off_t size)
return size_buf; return size_buf;
} }
static char get_ind(mode_t mode, bool perms)
{
switch (mode & S_IFMT) {
case S_IFREG:
if (perms)
return '-';
if (mode & 0100)
return '*';
return '\0';
case S_IFDIR:
return perms ? 'd' : '/';
case S_IFLNK:
return perms ? 'l' : '@';
case S_IFSOCK:
return perms ? 's' : '=';
case S_IFIFO:
return perms ? 'p' : '|';
case S_IFBLK:
return perms ? 'b' : '\0';
case S_IFCHR:
return perms ? 'c' : '\0';
default:
return '?';
}
}
/* Convert a mode field into "ls -l" type perms field. */ /* Convert a mode field into "ls -l" type perms field. */
static char *get_lsperms(mode_t mode) static char *get_lsperms(mode_t mode)
{ {
static const char * const rwx[] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"}; static const char * const rwx[] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
static char bits[11] = {'\0'}; static char bits[11] = {'\0'};
switch (mode & S_IFMT) { bits[0] = get_ind(mode, TRUE);
case S_IFREG:
bits[0] = '-';
break;
case S_IFDIR:
bits[0] = 'd';
break;
case S_IFLNK:
bits[0] = 'l';
break;
case S_IFSOCK:
bits[0] = 's';
break;
case S_IFIFO:
bits[0] = 'p';
break;
case S_IFBLK:
bits[0] = 'b';
break;
case S_IFCHR:
bits[0] = 'c';
break;
default:
bits[0] = '?';
break;
}
xstrlcpy(&bits[1], rwx[(mode >> 6) & 7], 4); xstrlcpy(&bits[1], rwx[(mode >> 6) & 7], 4);
xstrlcpy(&bits[4], rwx[(mode >> 3) & 7], 4); xstrlcpy(&bits[4], rwx[(mode >> 3) & 7], 4);
@ -3150,37 +3151,13 @@ static char *get_lsperms(mode_t mode)
static void printent(const struct entry *ent, uint namecols, bool sel) static void printent(const struct entry *ent, uint namecols, bool sel)
{ {
wchar_t *wstr; wchar_t *wstr;
char ind = '\0';
char hln = '\0'; char hln = '\0';
char ind = get_ind(ent->mode, FALSE);
switch (ent->mode & S_IFMT) { if (S_ISREG(ent->mode) && (ent->flags & HARD_LINK)) {
case S_IFREG:
if (ent->flags & HARD_LINK) {
hln = '>'; hln = '>';
--namecols; --namecols;
} }
if (ent->mode & 0100)
ind = '*';
break;
case S_IFDIR:
ind = '/';
break;
case S_IFLNK:
ind = '@';
break;
case S_IFSOCK:
ind = '=';
break;
case S_IFIFO:
ind = '|';
break;
case S_IFBLK: // fallthrough
case S_IFCHR:
break;
default:
ind = '?';
break;
}
if (!ind) if (!ind)
++namecols; ++namecols;