diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2005-02-21 16:01:59 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2005-02-21 16:01:59 +0000 |
commit | db2e4bb5db676bf608aa795e0fa98f8966524246 (patch) | |
tree | 5e6894038eb283e82fd27142e2f7d2409b898745 /bin/ksh/main.c | |
parent | a2b28142f37c5eb4a8a173d9b92c4c338c87b109 (diff) |
Set \u in prompt expansion to the right value, while avoiding getpw* calls,
which might be very inconvenient when the yp server is not available.
ok deraadt@ millert@
Diffstat (limited to 'bin/ksh/main.c')
-rw-r--r-- | bin/ksh/main.c | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/bin/ksh/main.c b/bin/ksh/main.c index 904ca59f8e1..fb32add4231 100644 --- a/bin/ksh/main.c +++ b/bin/ksh/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.35 2004/12/22 18:57:28 otto Exp $ */ +/* $OpenBSD: main.c,v 1.36 2005/02/21 16:01:58 otto Exp $ */ /* * startup, main loop, environments and error handling @@ -8,6 +8,7 @@ #include "sh.h" #include <sys/stat.h> +#include <pwd.h> extern char **environ; @@ -18,6 +19,7 @@ extern char **environ; static void reclaim(void); static void remove_temps(struct temp *tp); static int is_restricted(char *name); +static void init_username(void); /* * shell initialization @@ -61,6 +63,8 @@ static const char *initcoms [] = { NULL }; +char username[_PW_NAME_LEN + 1]; + #define version_param (initcoms[2]) int @@ -235,6 +239,7 @@ main(int argc, char *argv[]) ksheuid = geteuid(); + init_username(); safe_prompt = ksheuid ? "$ " : "# "; { struct tbl *vp = global("PS1"); @@ -374,6 +379,20 @@ main(int argc, char *argv[]) return 0; } +static void +init_username(void) +{ + char *p; + struct tbl *vp = global("USER"); + + if (vp->flag & ISSET) + p = ksheuid == 0 ? "root" : str_val(vp); + else + p = getlogin(); + + strlcpy(username, p != NULL ? p : "?", sizeof username); +} + int include(const char *name, int argc, char **argv, int intr_ok) { |