Implement -u support for ls(1)

This commit is contained in:
sin 2015-02-18 16:29:46 +00:00
parent 9bd094193d
commit 2a3f82a5cd
3 changed files with 19 additions and 5 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 -C, -R, -u
= ls no -C, -R
=* md5sum non-posix none
=* mkdir yes none
=* mkfifo yes none

7
ls.1
View File

@ -1,4 +1,4 @@
.Dd February 17, 2015
.Dd February 18, 2015
.Dt LS 1
.Os sbase
.Sh NAME
@ -6,7 +6,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm
.Op Fl 1acdFHhiLlqrtU
.Op Fl 1acdFHhiLlqrtUu
.Op Ar file ...
.Sh DESCRIPTION
.Nm
@ -44,6 +44,9 @@ Reverse the sort order.
Sort files by last file status/modification time instead of by name.
.It Fl U
Keep the list unsorted.
.It Fl u
Use file's last access time instead of last modification time for
sorting or printing.
.El
.Sh SEE ALSO
.Xr stat 2

15
ls.c
View File

@ -37,6 +37,7 @@ static int qflag = 0;
static int rflag = 0;
static int tflag = 0;
static int Uflag = 0;
static int uflag = 0;
static int first = 1;
static int many;
@ -55,7 +56,12 @@ mkent(struct entry *ent, char *path, int dostat, int follow)
ent->uid = st.st_uid;
ent->gid = st.st_gid;
ent->size = st.st_size;
ent->t = cflag ? st.st_ctime : st.st_mtime;
if (cflag)
ent->t = st.st_ctime;
else if (uflag)
ent->t = st.st_atime;
else
ent->t = st.st_mtime;
ent->ino = st.st_ino;
if (S_ISLNK(ent->mode))
ent->tmode = stat(path, &st) == 0 ? st.st_mode : 0;
@ -247,7 +253,7 @@ ls(const struct entry *ent)
static void
usage(void)
{
eprintf("usage: %s [-1acdFHhiLlqrtU] [file ...]\n", argv0);
eprintf("usage: %s [-1acdFHhiLlqrtUu] [file ...]\n", argv0);
}
int
@ -265,6 +271,7 @@ main(int argc, char *argv[])
break;
case 'c':
cflag = 1;
uflag = 0;
break;
case 'd':
dflag = 1;
@ -299,6 +306,10 @@ main(int argc, char *argv[])
case 'U':
Uflag = 1;
break;
case 'u':
uflag = 1;
cflag = 0;
break;
default:
usage();
} ARGEND;