[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ksh prompt username expansion patch

Recently commited bash style prompt expansion to ksh implements the '\u'
expansion for username differently from bash.

\u in promprt string expands to current session's login user.
On the contorary bash implementation expands it to effective uid's

Why does it matter?

User 'charlie' uses 
	PS1='\u_(_at_)_\h:\w \$ ' as prompt string.

He logs in and his prompt is 
	charlie_(_at_)_foo:~ $

Charlie switches to user 'bobbie' via 
	su - bobbie

Prompt is still
	charlie_(_at_)_foo:~ $

...because we use getlogin(2) to expand the \u in PS

The patch below makes \u to expand to current effective uid's username.
I'm not comfortable to include pwd.h in lex.c after looking at sh.h but
anyway this is a quick fix.

Index: lex.c
RCS file: /cvs/src/bin/ksh/lex.c,v
retrieving revision 1.21
diff -u -r1.21 lex.c
--- lex.c	6 Nov 2004 20:36:44 -0000	1.21
+++ lex.c	7 Nov 2004 13:21:17 -0000
@@ -7,6 +7,7 @@
 #include "sh.h"
 #include <libgen.h>
 #include <ctype.h>
+#include <pwd.h>
 /* Structure to keep track of the lexing state and the various pieces of info
@@ -1176,6 +1177,7 @@
 	int len, c, n, totlen = 0, indelimit = 0, counting = 1, delimitthis;
 	const char *cp = sp, *ccp;
 	extern INT32 njobs;
+	struct passwd *pwdent;
 	struct tm *tm;
 	time_t t;
@@ -1287,9 +1289,9 @@
 				strftime(strbuf, sizeof strbuf, "%R", tm);
 			case 'u':	/* '\' 'u' username */
-				p = getlogin();
-				if (p)
-					strlcpy(strbuf, p, sizeof strbuf);
+				pwdent = getpwuid(geteuid());
+				if (pwdent && pwdent->pw_name)
+					strlcpy(strbuf, pwdent->pw_name, sizeof strbuf);
 					strbuf[0] = '\0';

Visit your host, monkey.org