summaryrefslogtreecommitdiff
path: root/bin/sh
diff options
context:
space:
mode:
authorEdward Tomasz Napierala <trasz@FreeBSD.org>2019-01-24 11:59:46 +0000
committerEdward Tomasz Napierala <trasz@FreeBSD.org>2019-01-24 11:59:46 +0000
commitd81ca439e703f3ee4c83f0523aff43df9015e89d (patch)
tree77f1923e62406fc3cdf24e81a6f091e1ca173b86 /bin/sh
parent2cd6ad766eb239d84cb700cdd2752d3f5d8b3731 (diff)
downloadsrc-test2-d81ca439e703f3ee4c83f0523aff43df9015e89d.tar.gz
src-test2-d81ca439e703f3ee4c83f0523aff43df9015e89d.zip
Notes
Diffstat (limited to 'bin/sh')
-rw-r--r--bin/sh/parser.c61
-rw-r--r--bin/sh/sh.14
2 files changed, 64 insertions, 1 deletions
diff --git a/bin/sh/parser.c b/bin/sh/parser.c
index 85498050b15b..297d19d4d9b6 100644
--- a/bin/sh/parser.c
+++ b/bin/sh/parser.c
@@ -40,6 +40,8 @@ static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <sys/param.h>
+#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
@@ -130,6 +132,7 @@ static void synexpect(int) __dead2;
static void synerror(const char *) __dead2;
static void setprompt(int);
static int pgetc_linecont(void);
+static void getusername(char *, size_t);
static void *
@@ -1969,6 +1972,53 @@ pgetc_linecont(void)
return (c);
}
+
+static struct passwd *
+getpwlogin(void)
+{
+ const char *login;
+
+ login = getlogin();
+ if (login == NULL)
+ return (NULL);
+
+ return (getpwnam(login));
+}
+
+
+static void
+getusername(char *name, size_t namelen)
+{
+ static char cached_name[MAXLOGNAME];
+ struct passwd *pw;
+ uid_t euid;
+
+ if (cached_name[0] == '\0') {
+ euid = geteuid();
+
+ /*
+ * Handle the case when there is more than one
+ * login with the same UID, or when the login
+ * returned by getlogin(2) does no longer match
+ * the current UID.
+ */
+ pw = getpwlogin();
+ if (pw == NULL || pw->pw_uid != euid)
+ pw = getpwuid(euid);
+
+ if (pw != NULL) {
+ strlcpy(cached_name, pw->pw_name,
+ sizeof(cached_name));
+ } else {
+ snprintf(cached_name, sizeof(cached_name),
+ "%u", euid);
+ }
+ }
+
+ strlcpy(name, cached_name, namelen);
+}
+
+
/*
* called by editline -- any expansions to the prompt
* should be added here.
@@ -2027,6 +2077,17 @@ getprompt(void *unused __unused)
break;
/*
+ * User name.
+ */
+ case 'u':
+ ps[i] = '\0';
+ getusername(&ps[i], PROMPTLEN - i);
+ /* Skip to end of username. */
+ while (ps[i + 1] != '\0')
+ i++;
+ break;
+
+ /*
* Working directory.
*
* \W specifies just the final component,
diff --git a/bin/sh/sh.1 b/bin/sh/sh.1
index e671a1f0d142..ca9f4c536eb2 100644
--- a/bin/sh/sh.1
+++ b/bin/sh/sh.1
@@ -32,7 +32,7 @@
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $FreeBSD$
.\"
-.Dd December 8, 2018
+.Dd January 24, 2019
.Dt SH 1
.Os
.Sh NAME
@@ -1402,6 +1402,8 @@ which are replaced by the given information:
This system's fully-qualified hostname (FQDN).
.It Li \eh
This system's hostname.
+.It Li \eu
+User name.
.It Li \eW
The final component of the current working directory.
.It Li \ew