ls: add support for -p

This commit is contained in:
Quentin Rameau 2015-02-19 19:48:15 +01:00 committed by sin
parent d02327d0eb
commit 51b707e91e
3 changed files with 25 additions and 18 deletions

2
README
View File

@ -40,7 +40,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* ln yes none
=* logger yes none
=* logname yes none
= ls no -A, (-C), -R, -S, -f, -m, -n, -p, -s, -x
= ls no -A, (-C), -R, -S, -f, -m, -n, -s, -x
=* md5sum non-posix none
=* mkdir yes none
=* mkfifo yes none

4
ls.1
View File

@ -22,7 +22,7 @@ modification time for sorting or printing.
.It Fl d
List directories themselves, not their contents.
.It Fl F
Append a file type indicator to files.
Append a file type indicator to all special files.
.It Fl H
List information about the targets of symbolic links specified on the command
line instead of the links themselves.
@ -36,6 +36,8 @@ themselves.
.It Fl l
List detailed information about each file, including their type, permissions,
links, owner, group, size, and last file status/modification time.
.It Fl p
Append a file type indicator to directories.
.It Fl q
Replace non-printable characters in filenames with '?'.
.It Fl r

37
ls.c
View File

@ -33,6 +33,7 @@ static int hflag = 0;
static int iflag = 0;
static int Lflag = 0;
static int lflag = 0;
static int pflag = 0;
static int qflag = 0;
static int rflag = 0;
static int tflag = 0;
@ -70,21 +71,22 @@ mkent(struct entry *ent, char *path, int dostat, int follow)
static char *
indicator(mode_t mode)
{
if (!Fflag)
return "";
if (pflag || Fflag)
if (S_ISDIR(mode))
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 "";
if (Fflag) {
if (S_ISLNK(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 "*";
}
return "";
}
static void
@ -205,7 +207,7 @@ lsdir(const char *path)
if (d->d_name[0] == '.' && !aflag)
continue;
if (Uflag){
mkent(&ent, d->d_name, Fflag || lflag || iflag, Lflag);
mkent(&ent, d->d_name, Fflag || lflag || pflag || iflag, Lflag);
output(&ent);
} else {
ents = erealloc(ents, ++n * sizeof(*ents));
@ -224,7 +226,7 @@ lsdir(const char *path)
}
*p = '\0';
}
mkent(&ents[n - 1], name, tflag || Fflag || lflag || iflag, Lflag);
mkent(&ents[n - 1], name, tflag || Fflag || iflag || lflag || pflag, Lflag);
}
}
closedir(dp);
@ -295,6 +297,9 @@ main(int argc, char *argv[])
case 'l':
lflag = 1;
break;
case 'p':
pflag = 1;
break;
case 'q':
qflag = 1;
break;