ls: add -f and -S options

This commit is contained in:
Quentin Rameau 2015-03-18 19:26:42 +01:00 committed by sin
parent 787d99d896
commit 9fdef90feb
3 changed files with 36 additions and 8 deletions

2
README
View File

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

16
ls.1
View File

@ -1,4 +1,4 @@
.Dd February 21, 2015
.Dd March 18, 2015
.Dt LS 1
.Os sbase
.Sh NAME
@ -6,7 +6,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm
.Op Fl 1AacdFHhiLlnqrtUu
.Op Fl 1AacdFfHhiLlnqrStUu
.Op Ar file ...
.Sh DESCRIPTION
.Nm
@ -25,6 +25,16 @@ modification time for sorting or printing.
List directories themselves, not their contents.
.It Fl F
Append a file type indicator to all special files.
.It Fl f
Like
.Fl U
but turns on
.Fl a
and disables
.Fl r ,
.Fl S
and
.Fl t .
.It Fl H
List information about the targets of symbolic links specified on the command
line instead of the links themselves.
@ -52,6 +62,8 @@ List directory content recursively. The
flag is set implicitly.
.It Fl r
Reverse the sort order.
.It Fl S
Sort files by size (in decreasing order).
.It Fl t
Sort files by last file status/modification time instead of by name.
.It Fl U

26
ls.c
View File

@ -29,6 +29,7 @@ static int aflag = 0;
static int cflag = 0;
static int dflag = 0;
static int Fflag = 0;
static int fflag = 0;
static int Hflag = 0;
static int hflag = 0;
static int iflag = 0;
@ -38,6 +39,7 @@ static int nflag = 0;
static int pflag = 0;
static int qflag = 0;
static int Rflag = 0;
static int Sflag = 0;
static int rflag = 0;
static int tflag = 0;
static int Uflag = 0;
@ -176,12 +178,15 @@ output(const struct entry *ent)
static int
entcmp(const void *va, const void *vb)
{
int cmp = 0;
const struct entry *a = va, *b = vb;
if (tflag)
return b->t - a->t;
else
return strcmp(a->name, b->name);
if (Sflag)
cmp = b->size - a->size;
else if (tflag)
cmp = b->t - a->t;
return cmp ? cmp : strcmp(a->name, b->name);
}
static void
@ -289,6 +294,11 @@ main(int argc, char *argv[])
case 'd':
dflag = 1;
break;
case 'f':
aflag = 1;
fflag = 1;
Uflag = 1;
break;
case 'F':
Fflag = 1;
break;
@ -321,9 +331,15 @@ main(int argc, char *argv[])
Rflag = 1;
break;
case 'r':
rflag = 1;
if (!fflag)
rflag = 1;
break;
case 'S':
Sflag = 1;
tflag = 0;
break;
case 't':
Sflag = 0;
tflag = 1;
break;
case 'U':