From dc70eb797610565650ed2b8d91b1ec74d0ca1197 Mon Sep 17 00:00:00 2001 From: sin Date: Tue, 20 Jan 2015 16:50:37 +0000 Subject: [PATCH] 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. --- README | 2 +- ls.1 | 87 ++++++++++++++++++++++++++++------------------------------ ls.c | 16 +++++++---- 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/README b/README index e51cb5a..ff90332 100644 --- a/README +++ b/README @@ -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, -H, -L, -R, -c, -q, -u += ls no -C, -R, -q, -u md5sum non-posix none = mkdir yes none = mkfifo yes none diff --git a/ls.1 b/ls.1 index 792b07b..0bb6269 100644 --- a/ls.1 +++ b/ls.1 @@ -1,49 +1,46 @@ -.TH LS 1 sbase\-VERSION -.SH NAME -ls \- list directory contents -.SH SYNOPSIS -.B ls -.RB [ \-adFHhiLlrtU ] -.RI [ file ...] -.SH DESCRIPTION -.B ls +.Dd January 20, 2015 +.Dt LS 1 sbase\-VERSION +.Sh NAME +.Nm ls +.Nd list directory contents +.Sh SYNOPSIS +.Nm ls +.Op Fl 1acdFHhiLlrtU +.Op Ar file ... +.Sh DESCRIPTION +.Nm lists each given file, and the contents of each given directory. If no files are given the current directory is listed. -.SH OPTIONS -.TP -.B \-a -shows hidden files (those beginning with '.'). -.TP -.B \-d -lists directories themselves, not their contents. -.TP -.B \-F -append a file type indicator to files. -.TP -.B \-H -list information about the targets of symbolic links specified on the command +.Sh OPTIONS +.Bl -tag -width Ds +.It Fl a +Show hidden files (those beginning with '.'). +.It Fl c +Use time file's status was last changed instead of last +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. +.It Fl H +List information about the targets of symbolic links specified on the command line instead of the links themselves. -.TP -.B \-h -show filesizes in human\-readable format. -.TP -.B \-i -print the index number of each file. -.TP -.B \-L -list information about the targets of symbolic links instead of the links +.It Fl h +Show filesizes in human\-readable format. +.It Fl i +Print the index number of each file. +.It Fl L +List information about the targets of symbolic links instead of the links themselves. -.B \-l -lists detailed information about each file, including their type, permissions, -links, owner, group, size, and modification time. -.TP -.B \-r -reverses the sort order. -.TP -.B \-t -sorts files by modification time instead of by name. -.TP -.B \-U -keeps the list unsorted. -.SH SEE ALSO -.IR stat (2) +.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 r +Reverse the sort order. +.It Fl t +Sort files by last file status/modification time instead of by name. +.It Fl U +Keep the list unsorted. +.El +.Sh SEE ALSO +.Xr stat 2 diff --git a/ls.c b/ls.c index 8862b87..4ac0da2 100644 --- a/ls.c +++ b/ls.c @@ -18,7 +18,7 @@ typedef struct { uid_t uid; gid_t gid; off_t size; - time_t mtime; + time_t t; ino_t ino; } Entry; @@ -29,6 +29,7 @@ static void mkent(Entry *, char *, int, int); static void output(Entry *); static int aflag = 0; +static int cflag = 0; static int dflag = 0; static int Fflag = 0; static int Hflag = 0; @@ -45,7 +46,7 @@ static int many; static void usage(void) { - eprintf("usage: %s [-1adFhilrtU] [FILE...]\n", argv0); + eprintf("usage: %s [-1acdFHhiLlrtU] [file ...]\n", argv0); } int @@ -61,6 +62,9 @@ main(int argc, char *argv[]) case 'a': aflag = 1; break; + case 'c': + cflag = 1; + break; case 'd': dflag = 1; break; @@ -116,7 +120,7 @@ entcmp(const void *va, const void *vb) const Entry *a = va, *b = vb; if (tflag) - return b->mtime - a->mtime; + return b->t - a->t; else 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->gid = st.st_gid; ent->size = st.st_size; - ent->mtime = st.st_mtime; + ent->t = cflag ? st.st_ctime : st.st_mtime; ent->ino = st.st_ino; if (S_ISLNK(ent->mode)) ent->tmode = stat(path, &st) == 0 ? st.st_mode : 0; @@ -284,12 +288,12 @@ output(Entry *ent) else 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"; else 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); if (hflag) printf("%10s ", humansize((unsigned long)ent->size));