Add the -F flag to ls
This commit is contained in:
parent
c6f59aadbd
commit
080db637f8
42
ls.c
42
ls.c
|
@ -31,6 +31,7 @@ static void output(Entry *);
|
||||||
|
|
||||||
static bool aflag = false;
|
static bool aflag = false;
|
||||||
static bool dflag = false;
|
static bool dflag = false;
|
||||||
|
static bool Fflag = false;
|
||||||
static bool iflag = false;
|
static bool iflag = false;
|
||||||
static bool lflag = false;
|
static bool lflag = false;
|
||||||
static bool rflag = false;
|
static bool rflag = false;
|
||||||
|
@ -42,7 +43,7 @@ static bool many;
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-adilrtU] [FILE...]\n", argv0);
|
eprintf("usage: %s [-adFilrtU] [FILE...]\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -58,6 +59,9 @@ main(int argc, char *argv[])
|
||||||
case 'd':
|
case 'd':
|
||||||
dflag = true;
|
dflag = true;
|
||||||
break;
|
break;
|
||||||
|
case 'F':
|
||||||
|
Fflag = true;
|
||||||
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
iflag = true;
|
iflag = true;
|
||||||
break;
|
break;
|
||||||
|
@ -140,7 +144,7 @@ lsdir(const char *path)
|
||||||
if(d->d_name[0] == '.' && !aflag)
|
if(d->d_name[0] == '.' && !aflag)
|
||||||
continue;
|
continue;
|
||||||
if(Uflag){
|
if(Uflag){
|
||||||
mkent(&ent, d->d_name, lflag || iflag);
|
mkent(&ent, d->d_name, Fflag || lflag || iflag);
|
||||||
output(&ent);
|
output(&ent);
|
||||||
} else {
|
} else {
|
||||||
if(!(ents = realloc(ents, ++n * sizeof *ents)))
|
if(!(ents = realloc(ents, ++n * sizeof *ents)))
|
||||||
|
@ -148,7 +152,7 @@ lsdir(const char *path)
|
||||||
if(!(p = malloc((sz = strlen(d->d_name)+1))))
|
if(!(p = malloc((sz = strlen(d->d_name)+1))))
|
||||||
eprintf("malloc:");
|
eprintf("malloc:");
|
||||||
memcpy(p, d->d_name, sz);
|
memcpy(p, d->d_name, sz);
|
||||||
mkent(&ents[n-1], p, tflag || lflag || iflag);
|
mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
@ -184,6 +188,28 @@ mkent(Entry *ent, char *path, bool dostat)
|
||||||
ent->ino = st.st_ino;
|
ent->ino = st.st_ino;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
indicator(mode_t mode)
|
||||||
|
{
|
||||||
|
if(!Fflag)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
if(S_ISLNK(mode))
|
||||||
|
return "@";
|
||||||
|
else if(S_ISDIR(mode))
|
||||||
|
return "/";
|
||||||
|
else if(S_ISFIFO(mode))
|
||||||
|
return "|";
|
||||||
|
else if(S_ISSOCK(mode))
|
||||||
|
return "=";
|
||||||
|
else if(mode & S_IXUSR ||
|
||||||
|
mode & S_IXGRP ||
|
||||||
|
mode & S_IXOTH)
|
||||||
|
return "*";
|
||||||
|
else
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
output(Entry *ent)
|
output(Entry *ent)
|
||||||
{
|
{
|
||||||
|
@ -192,11 +218,12 @@ output(Entry *ent)
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
struct group *gr;
|
struct group *gr;
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
|
Entry entlnk;
|
||||||
|
|
||||||
if (iflag)
|
if (iflag)
|
||||||
printf("%lu ", (unsigned long)ent->ino);
|
printf("%lu ", (unsigned long)ent->ino);
|
||||||
if(!lflag) {
|
if(!lflag) {
|
||||||
puts(ent->name);
|
printf("%s%s\n", ent->name, indicator(ent->mode));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(S_ISREG(ent->mode))
|
if(S_ISREG(ent->mode))
|
||||||
|
@ -250,13 +277,14 @@ 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", mode, (long)ent->nlink, pw->pw_name,
|
printf("%s %2ld %-4s %-5s %6lu %s %s%s", mode, (long)ent->nlink, pw->pw_name,
|
||||||
gr->gr_name, (unsigned long)ent->size, buf, ent->name);
|
gr->gr_name, (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);
|
||||||
buf[len] = '\0';
|
buf[len] = '\0';
|
||||||
printf(" -> %s", buf);
|
mkent(&entlnk, buf, Fflag);
|
||||||
|
printf(" -> %s%s", buf, indicator(entlnk.mode));
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user