ls: Implement -c and update manpage

Update usage + README as well.  Apparently some of the options (
-H and -L) had already been implemented but not reflected in the
program usage line.
This commit is contained in:
sin 2015-01-20 16:50:37 +00:00
parent 98d759a274
commit dc70eb7976
3 changed files with 53 additions and 52 deletions

2
README
View File

@ -40,7 +40,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
= ln yes none = ln yes none
=* logger yes none =* logger yes none
= logname yes none = logname yes none
= ls no -C, -H, -L, -R, -c, -q, -u = ls no -C, -R, -q, -u
md5sum non-posix none md5sum non-posix none
= mkdir yes none = mkdir yes none
= mkfifo yes none = mkfifo yes none

87
ls.1
View File

@ -1,49 +1,46 @@
.TH LS 1 sbase\-VERSION .Dd January 20, 2015
.SH NAME .Dt LS 1 sbase\-VERSION
ls \- list directory contents .Sh NAME
.SH SYNOPSIS .Nm ls
.B ls .Nd list directory contents
.RB [ \-adFHhiLlrtU ] .Sh SYNOPSIS
.RI [ file ...] .Nm ls
.SH DESCRIPTION .Op Fl 1acdFHhiLlrtU
.B ls .Op Ar file ...
.Sh DESCRIPTION
.Nm
lists each given file, and the contents of each given directory. If no files lists each given file, and the contents of each given directory. If no files
are given the current directory is listed. are given the current directory is listed.
.SH OPTIONS .Sh OPTIONS
.TP .Bl -tag -width Ds
.B \-a .It Fl a
shows hidden files (those beginning with '.'). Show hidden files (those beginning with '.').
.TP .It Fl c
.B \-d Use time file's status was last changed instead of last
lists directories themselves, not their contents. modification time for sorting or printing.
.TP .It Fl d
.B \-F List directories themselves, not their contents.
append a file type indicator to files. .It Fl F
.TP Append a file type indicator to files.
.B \-H .It Fl H
list information about the targets of symbolic links specified on the command List information about the targets of symbolic links specified on the command
line instead of the links themselves. line instead of the links themselves.
.TP .It Fl h
.B \-h Show filesizes in human\-readable format.
show filesizes in human\-readable format. .It Fl i
.TP Print the index number of each file.
.B \-i .It Fl L
print the index number of each file. List information about the targets of symbolic links instead of the links
.TP
.B \-L
list information about the targets of symbolic links instead of the links
themselves. themselves.
.B \-l .It Fl l
lists detailed information about each file, including their type, permissions, List detailed information about each file, including their type, permissions,
links, owner, group, size, and modification time. links, owner, group, size, and last file status/modification time.
.TP .It Fl r
.B \-r Reverse the sort order.
reverses the sort order. .It Fl t
.TP Sort files by last file status/modification time instead of by name.
.B \-t .It Fl U
sorts files by modification time instead of by name. Keep the list unsorted.
.TP .El
.B \-U .Sh SEE ALSO
keeps the list unsorted. .Xr stat 2
.SH SEE ALSO
.IR stat (2)

16
ls.c
View File

@ -18,7 +18,7 @@ typedef struct {
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
off_t size; off_t size;
time_t mtime; time_t t;
ino_t ino; ino_t ino;
} Entry; } Entry;
@ -29,6 +29,7 @@ static void mkent(Entry *, char *, int, int);
static void output(Entry *); static void output(Entry *);
static int aflag = 0; static int aflag = 0;
static int cflag = 0;
static int dflag = 0; static int dflag = 0;
static int Fflag = 0; static int Fflag = 0;
static int Hflag = 0; static int Hflag = 0;
@ -45,7 +46,7 @@ static int many;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-1adFhilrtU] [FILE...]\n", argv0); eprintf("usage: %s [-1acdFHhiLlrtU] [file ...]\n", argv0);
} }
int int
@ -61,6 +62,9 @@ main(int argc, char *argv[])
case 'a': case 'a':
aflag = 1; aflag = 1;
break; break;
case 'c':
cflag = 1;
break;
case 'd': case 'd':
dflag = 1; dflag = 1;
break; break;
@ -116,7 +120,7 @@ entcmp(const void *va, const void *vb)
const Entry *a = va, *b = vb; const Entry *a = va, *b = vb;
if (tflag) if (tflag)
return b->mtime - a->mtime; return b->t - a->t;
else else
return strcmp(a->name, b->name); return strcmp(a->name, b->name);
} }
@ -196,7 +200,7 @@ mkent(Entry *ent, char *path, int dostat, int follow)
ent->uid = st.st_uid; ent->uid = st.st_uid;
ent->gid = st.st_gid; ent->gid = st.st_gid;
ent->size = st.st_size; ent->size = st.st_size;
ent->mtime = st.st_mtime; ent->t = cflag ? st.st_ctime : st.st_mtime;
ent->ino = st.st_ino; ent->ino = st.st_ino;
if (S_ISLNK(ent->mode)) if (S_ISLNK(ent->mode))
ent->tmode = stat(path, &st) == 0 ? st.st_mode : 0; ent->tmode = stat(path, &st) == 0 ? st.st_mode : 0;
@ -284,12 +288,12 @@ output(Entry *ent)
else else
snprintf(grname, sizeof(grname), "%d", ent->gid); snprintf(grname, sizeof(grname), "%d", ent->gid);
if (time(NULL) > ent->mtime + (180*24*60*60)) /* 6 months ago? */ if (time(NULL) > ent->t + (180*24*60*60)) /* 6 months ago? */
fmt = "%b %d %Y"; fmt = "%b %d %Y";
else else
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->t));
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname); printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
if (hflag) if (hflag)
printf("%10s ", humansize((unsigned long)ent->size)); printf("%10s ", humansize((unsigned long)ent->size));