Index: parser.c =================================================================== --- parser.c (revision 209227) +++ parser.c (working copy) @@ -38,6 +38,8 @@ static char sccsid[] = "@(#)parser.c 8.7 (Berkeley #include __FBSDID("$FreeBSD$"); +#include +#include #include #include @@ -120,6 +122,7 @@ STATIC int noexpand(char *); STATIC void synexpect(int); STATIC void synerror(const char *); STATIC void setprompt(int); +STATIC void getusername(char *, size_t); STATIC void * @@ -1724,6 +1727,25 @@ setprompt(int which) } } +STATIC void +getusername(char *name, size_t namelen) +{ + uid_t uid; + struct passwd *pw; + static char *username = NULL; + + if (username == NULL) { + uid = getuid(); + pw = getpwuid(uid); + if (pw != NULL) + username = strdup(pw->pw_name); + else + asprintf(&username, "%u", uid); + } + + snprintf(name, namelen, "%s", username); +} + /* * called by editline -- any expansions to the prompt * should be added here. @@ -1777,6 +1799,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,