aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorPiotr Pawel Stefaniak <pstef@FreeBSD.org>2023-01-01 19:38:35 +0000
committerPiotr Pawel Stefaniak <pstef@FreeBSD.org>2023-09-23 18:26:45 +0000
commita675eaec5aef0089b6838aca8fd432fea0bd883b (patch)
tree85872d4b7fe1215123886923be08e2bb459fe65b /bin
parentf80babf906b7be51b2a031ef26525893c7bf4e31 (diff)
downloadsrc-a675eaec5aef0089b6838aca8fd432fea0bd883b.tar.gz
src-a675eaec5aef0089b6838aca8fd432fea0bd883b.zip
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/parser.c44
-rw-r--r--bin/sh/sh.110
2 files changed, 54 insertions, 0 deletions
diff --git a/bin/sh/parser.c b/bin/sh/parser.c
index 121c367c601c..8e959b46596b 100644
--- a/bin/sh/parser.c
+++ b/bin/sh/parser.c
@@ -43,6 +43,7 @@ static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
+#include <time.h>
#include "shell.h"
#include "parser.h"
@@ -2101,6 +2102,49 @@ getprompt(void *unused __unused)
break;
/*
+ * Print the current time as per provided strftime format.
+ */
+ case 'D': {
+ char tfmt[128] = "%X"; /* \D{} means %X. */
+ struct tm *now;
+
+ if (fmt[1] != '{') {
+ /*
+ * "\D" but not "\D{", so treat the '\'
+ * literally and rewind fmt to treat 'D'
+ * literally next iteration.
+ */
+ ps[i] = '\\';
+ fmt--;
+ break;
+ }
+ fmt += 2; /* Consume "D{". */
+ if (fmt[0] != '}') {
+ char *end;
+
+ end = memccpy(tfmt, fmt, '}', sizeof(tfmt));
+ if (end == NULL) {
+ /*
+ * Format too long or no '}', so
+ * ignore "\D{" altogether.
+ * The loop will do i++, but nothing
+ * was written to ps, so do i-- here.
+ * Rewind fmt for similar reason.
+ */
+ i--;
+ fmt--;
+ break;
+ }
+ *--end = '\0'; /* Ignore the copy of '}'. */
+ fmt += end - tfmt;
+ }
+ now = localtime(&(time_t){time(NULL)});
+ i += strftime(&ps[i], PROMPTLEN - i - 1, tfmt, now);
+ i--; /* The loop will do i++. */
+ break;
+ }
+
+ /*
* Hostname.
*
* \h specifies just the local hostname,
diff --git a/bin/sh/sh.1 b/bin/sh/sh.1
index fb3afc7d3d4d..adbc32827046 100644
--- a/bin/sh/sh.1
+++ b/bin/sh/sh.1
@@ -1427,6 +1427,16 @@ unless you are the superuser, in which case it defaults to
may include any of the following formatting sequences,
which are replaced by the given information:
.Bl -tag -width indent
+.It Li \eD{format}
+The current time in
+.Xr strftime 3
+.Ar format .
+The braces are required.
+Empty
+.Ar format
+is equivalent to
+\&%X,
+national representation of the time.
.It Li \eH
This system's fully-qualified hostname (FQDN).
.It Li \eh