From dc3f655cd70a6bfedf5f17be9515b4df39f2ddfe Mon Sep 17 00:00:00 2001 From: zrj Date: Tue, 26 Dec 2017 11:22:04 +0200 Subject: [PATCH] ls(1): Add -D option to set date-time format in ls -l. Taken-from: FreeBSD --- bin/ls/ls.1 | 20 ++++++++++++++++++++ bin/ls/ls.c | 6 +++++- bin/ls/ls.h | 1 + bin/ls/print.c | 7 ++++++- bin/ls/util.c | 4 ++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 7c404f3843..121caa16a2 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -40,6 +40,7 @@ .Sh SYNOPSIS .Nm .Op Fl 1ABCFGHILPRSTWabcdfghiklmnopqrstuwxy +.Op Fl D Ar format .Op Ar .Sh DESCRIPTION For each operand that names a @@ -81,6 +82,21 @@ where is the numeric value of the character in octal. .It Fl C Force multi-column output; this is the default when output is to a terminal. +.It Fl D Ar format +When printing in the long +.Pq Fl l +format, use +.Ar format +to format the date and time output. +The argument +.Ar format +is a string used by +.Xr strftime 3 . +Depending on the choice of format string, this may result in a +different number of columns in the output. +This option overrides the +.Fl T +option. .It Fl F Display a slash .Pq Ql / @@ -650,6 +666,10 @@ the .Nm utility conforms to .St -p1003.1-2001 . +The options +.Fl B , D , G , I , T , W , b , h , w , y +are compatible extensions not defined in +.St -p1003.1-2001 . .Sh HISTORY An .Nm diff --git a/bin/ls/ls.c b/bin/ls/ls.c index ebe82a1be9..1f8ebced8a 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -117,6 +117,7 @@ static int f_singlecol; /* use single column output */ int f_sortacross; /* sort across rows, not down columns */ int f_statustime; /* use time of last mode change */ static int f_stream; /* stream the output, separate with commas */ + const char *f_timeformat; /* user-specified time format */ static int f_timesort; /* sort by time vice name */ int f_type; /* add type character for non-regular files */ static int f_whiteout; /* show whiteout entries */ @@ -172,7 +173,7 @@ main(int argc, char *argv[]) fts_options = FTS_PHYSICAL; while ((ch = getopt(argc, argv, - "1ABCFGHILPRSTWabcdfghiklmnopqrstuwxy")) != -1) { + "1ABCD:FGHILPRSTWabcdfghiklmnopqrstuwxy")) != -1) { switch (ch) { /* * The -1, -C, -x and -l options all override each other so @@ -215,6 +216,9 @@ main(int argc, char *argv[]) f_accesstime = 1; f_statustime = 0; break; + case 'D': + f_timeformat = optarg; + break; case 'F': f_type = 1; f_slash = 0; diff --git a/bin/ls/ls.h b/bin/ls/ls.h index 0ef10ea55a..7809edfe34 100644 --- a/bin/ls/ls.h +++ b/bin/ls/ls.h @@ -51,6 +51,7 @@ extern int f_size; /* list size in short listing */ extern int f_slash; /* append a '/' if the file is a directory */ extern int f_sortacross; /* sort across rows, not down columns */ extern int f_statustime; /* use time of last mode change */ +extern const char *f_timeformat; /* user-specified time format */ extern int f_notabs; /* don't use tab-separated multi-col output */ extern int f_type; /* add type character for non-regular files */ #ifdef COLORLS diff --git a/bin/ls/print.c b/bin/ls/print.c index bbf46307df..8b8ee2ff12 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -364,7 +364,12 @@ printtime(time_t ftime) posix_time = (strcmp(lc_time, "C") == 0); } - if (f_sectime) { + if (f_timeformat) { + /* + * User specified format + */ + format = f_timeformat; + } else if (f_sectime) { /* * POSIX: Not covered by standard. If C/POSIX locale used * the old convention of mmm dd hh:mm:ss yyyy is kept. diff --git a/bin/ls/util.c b/bin/ls/util.c index 65d1eba68f..1bc8a4ece0 100644 --- a/bin/ls/util.c +++ b/bin/ls/util.c @@ -217,9 +217,9 @@ usage(void) { fprintf(stderr, #ifdef COLORLS - "usage: ls [-1ABCFGHILPRSTWabcdfghiklmnopqrstuwxy]" + "usage: ls [-1ABCFGHILPRSTWabcdfghiklmnopqrstuwxy] [-D format]" #else - "usage: ls [-1ABCFHILPRSTWabcdfghiklmnopqrstuwxy]" + "usage: ls [-1ABCFHILPRSTWabcdfghiklmnopqrstuwxy] [-D format]" #endif " [file ...]\n"); exit(1); -- 2.11.4.GIT