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 | |
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@
-rw-r--r-- | bin/ksh/lex.c | 8 | ||||
-rw-r--r-- | bin/ksh/main.c | 21 | ||||
-rw-r--r-- | bin/ksh/sh.h | 3 |
3 files changed, 24 insertions, 8 deletions
diff --git a/bin/ksh/lex.c b/bin/ksh/lex.c index 5beefbe6e55..2441dc14842 100644 --- a/bin/ksh/lex.c +++ b/bin/ksh/lex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lex.c,v 1.34 2005/01/10 17:52:04 millert Exp $ */ +/* $OpenBSD: lex.c,v 1.35 2005/02/21 16:01:58 otto Exp $ */ /* * lexical analysis and source input @@ -1278,11 +1278,7 @@ dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) strftime(strbuf, sizeof strbuf, "%R", tm); break; case 'u': /* '\' 'u' username */ - p = getlogin(); - if (p) - strlcpy(strbuf, p, sizeof strbuf); - else - strbuf[0] = '\0'; + strlcpy(strbuf, username, sizeof strbuf); break; case 'v': /* '\' 'v' version (short) */ p = strchr(ksh_version, ' '); 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) { diff --git a/bin/ksh/sh.h b/bin/ksh/sh.h index 9b017c48d8c..a0e1269e715 100644 --- a/bin/ksh/sh.h +++ b/bin/ksh/sh.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sh.h,v 1.25 2004/12/22 17:14:34 millert Exp $ */ +/* $OpenBSD: sh.h,v 1.26 2005/02/21 16:01:58 otto Exp $ */ /* * Public Domain Bourne/Korn shell @@ -77,6 +77,7 @@ EXTERN uid_t ksheuid; /* effective uid of shell */ EXTERN int exstat; /* exit status */ EXTERN int subst_exstat; /* exit status of last $(..)/`..` */ EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */ +EXTERN char username[]; /* username for \u prompt expansion */ /* * Area-based allocation built on malloc/free |