diff --git a/bin/ls/cmp.c b/bin/ls/cmp.c index 37b4a750e31f..badd10867465 100644 --- a/bin/ls/cmp.c +++ b/bin/ls/cmp.c @@ -114,6 +114,32 @@ revacccmp(const FTSENT *a, const FTSENT *b) return (acccmp(b, a)); } +int +birthcmp(const FTSENT *a, const FTSENT *b) +{ + + if (b->fts_statp->st_birthtimespec.tv_sec > + a->fts_statp->st_birthtimespec.tv_sec) + return (1); + if (b->fts_statp->st_birthtimespec.tv_sec < + a->fts_statp->st_birthtimespec.tv_sec) + return (-1); + if (b->fts_statp->st_birthtimespec.tv_nsec > + a->fts_statp->st_birthtimespec.tv_nsec) + return (1); + if (b->fts_statp->st_birthtimespec.tv_nsec < + a->fts_statp->st_birthtimespec.tv_nsec) + return (-1); + return (strcoll(a->fts_name, b->fts_name)); +} + +int +revbirthcmp(const FTSENT *a, const FTSENT *b) +{ + + return (birthcmp(b, a)); +} + int statcmp(const FTSENT *a, const FTSENT *b) { diff --git a/bin/ls/extern.h b/bin/ls/extern.h index 02a61f5852b2..f290fbbe7e05 100644 --- a/bin/ls/extern.h +++ b/bin/ls/extern.h @@ -32,6 +32,8 @@ int acccmp(const FTSENT *, const FTSENT *); int revacccmp(const FTSENT *, const FTSENT *); +int birthcmp(const FTSENT *, const FTSENT *); +int revbirthcmp(const FTSENT *, const FTSENT *); int modcmp(const FTSENT *, const FTSENT *); int revmodcmp(const FTSENT *, const FTSENT *); int namecmp(const FTSENT *, const FTSENT *); diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 4a1667928630..ce4c65eeb5b0 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -149,6 +149,8 @@ When used with the .Dq ell ) option, display complete time information for the file, including month, day, hour, minute, second, and year. +.It Fl U +Use time when file was created for sorting or printing. .It Fl W Display whiteouts when scanning directories. .It Fl Z diff --git a/bin/ls/ls.c b/bin/ls/ls.c index f354fc234db7..4a752ce2f989 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -104,6 +104,7 @@ int termwidth = 80; /* default terminal width */ /* flags */ int f_accesstime; /* use time of last access */ + int f_birthtime; /* use time of birth */ int f_flags; /* show flags associated with a file */ int f_humanval; /* show human-readable file sizes */ int f_inode; /* print inode */ @@ -178,7 +179,7 @@ main(int argc, char *argv[]) fts_options = FTS_PHYSICAL; while ((ch = getopt(argc, argv, - "1ABCFGHILPRSTWZabcdfghiklmnopqrstuwx")) != -1) { + "1ABCFGHILPRSTUWZabcdfghiklmnopqrstuwx")) != -1) { switch (ch) { /* * The -1, -C, -x and -l options all override each other so @@ -207,14 +208,21 @@ main(int argc, char *argv[]) f_longform = 0; f_singlecol = 0; break; - /* The -c and -u options override each other. */ + /* The -c, -u, and -U options override each other. */ case 'c': f_statustime = 1; f_accesstime = 0; + f_birthtime = 0; break; case 'u': f_accesstime = 1; f_statustime = 0; + f_birthtime = 0; + break; + case 'U': + f_birthtime = 1; + f_accesstime = 0; + f_statustime = 0; break; case 'F': f_type = 1; @@ -410,6 +418,8 @@ main(int argc, char *argv[]) sortfcn = revnamecmp; else if (f_accesstime) sortfcn = revacccmp; + else if (f_birthtime) + sortfcn = revbirthcmp; else if (f_statustime) sortfcn = revstatcmp; else if (f_sizesort) @@ -421,6 +431,8 @@ main(int argc, char *argv[]) sortfcn = namecmp; else if (f_accesstime) sortfcn = acccmp; + else if (f_birthtime) + sortfcn = birthcmp; else if (f_statustime) sortfcn = statcmp; else if (f_sizesort) diff --git a/bin/ls/ls.h b/bin/ls/ls.h index d4e5db6ad219..4d12b22b2360 100644 --- a/bin/ls/ls.h +++ b/bin/ls/ls.h @@ -38,6 +38,7 @@ extern long blocksize; /* block size units */ extern int f_accesstime; /* use time of last access */ +extern int f_birthtime; /* use time of file creation */ extern int f_flags; /* show flags associated with a file */ extern int f_humanval; /* show human-readable file sizes */ extern int f_label; /* show MAC label */ diff --git a/bin/ls/print.c b/bin/ls/print.c index 185b418afe30..8ca40e2ba574 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -190,6 +190,8 @@ printlong(const DISPLAY *dp) printsize(dp->s_size, sp->st_size); if (f_accesstime) printtime(sp->st_atime); + else if (f_birthtime) + printtime(sp->st_birthtime); else if (f_statustime) printtime(sp->st_ctime); else diff --git a/bin/ls/util.c b/bin/ls/util.c index 01ae6cb39f90..62fa75adf807 100644 --- a/bin/ls/util.c +++ b/bin/ls/util.c @@ -222,9 +222,9 @@ usage(void) { (void)fprintf(stderr, #ifdef COLORLS - "usage: ls [-ABCFGHILPRSTWZabcdfghiklmnopqrstuwx1]" + "usage: ls [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuwx1]" #else - "usage: ls [-ABCFHILPRSTWZabcdfghiklmnopqrstuwx1]" + "usage: ls [-ABCFHILPRSTUWZabcdfghiklmnopqrstuwx1]" #endif " [file ...]\n"); exit(1);