Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F140100171
D35840.id114837.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D35840.id114837.diff
View Options
Index: bin/sh/parser.c
===================================================================
--- bin/sh/parser.c
+++ bin/sh/parser.c
@@ -45,6 +45,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
+#include <time.h>
#include "shell.h"
#include "parser.h"
@@ -2100,6 +2101,51 @@
}
break;
+ /*
+ * Print the current time as per provided strftime format.
+ */
+ case 'D': {
+ char tfmt[128] = "%X"; /* \D{} means %X. */
+ struct tm *now;
+ size_t written;
+
+ 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)});
+ written = strftime(&ps[i], PROMPTLEN - i - 1, tfmt, now);
+ if (written > 0)
+ i += written - 1;
+ break;
+ }
+
/*
* Hostname.
*
Index: bin/sh/sh.1
===================================================================
--- bin/sh/sh.1
+++ bin/sh/sh.1
@@ -1422,6 +1422,16 @@
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
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 21, 5:41 AM (11 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
27108193
Default Alt Text
D35840.id114837.diff (1 KB)
Attached To
Mode
D35840: sh: implement PS1 \D to print current time
Attached
Detach File
Event Timeline
Log In to Comment