Date: Sat, 25 Jul 2020 11:57:39 +0000 (UTC) From: Piotr Pawel Stefaniak <pstef@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r363525 - in stable/12: bin/csh bin/sh share/skel Message-ID: <202007251157.06PBvdBo095555@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: pstef Date: Sat Jul 25 11:57:39 2020 New Revision: 363525 URL: https://svnweb.freebsd.org/changeset/base/363525 Log: MFC r342576-342577,342645,342812,342881,343231,343399 (by trasz): r342577 Make sh(1) collapse $HOME into "~" in PS1 r342576 Simplify the way we set the default sh(1) PS1 r342645 Add current working directory to the default sh prompt r342812 Give sh(1) a proper default prompt instead of just "$". r342881 Make sh(1) recognize the default $HOME r343231 Don't mess with BLOCKSIZE in shell startup files r343399 Make sh(1) support \u in PS1 Modified: stable/12/bin/csh/csh.login stable/12/bin/csh/dot.cshrc stable/12/bin/sh/parser.c stable/12/bin/sh/profile stable/12/bin/sh/sh.1 stable/12/share/skel/dot.cshrc stable/12/share/skel/dot.profile stable/12/share/skel/dot.shrc Directory Properties: stable/12/ (props changed) Modified: stable/12/bin/csh/csh.login ============================================================================== --- stable/12/bin/csh/csh.login Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/bin/csh/csh.login Sat Jul 25 11:57:39 2020 (r363525) @@ -1,9 +1,6 @@ # $FreeBSD$ # # System-wide .login file for csh(1). -# Uncomment this to give you the default 4.2 behavior, where disk -# information is shown in K-Blocks -# setenv BLOCKSIZE K # # For the setting of languages and character sets please see # login.conf(5) and in particular the charset and lang options. Modified: stable/12/bin/csh/dot.cshrc ============================================================================== --- stable/12/bin/csh/dot.cshrc Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/bin/csh/dot.cshrc Sat Jul 25 11:57:39 2020 (r363525) @@ -23,7 +23,6 @@ set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/s setenv EDITOR vi setenv PAGER less -setenv BLOCKSIZE K if ($?prompt) then # An interactive shell -- set some stuff up Modified: stable/12/bin/sh/parser.c ============================================================================== --- stable/12/bin/sh/parser.c Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/bin/sh/parser.c Sat Jul 25 11:57:39 2020 (r363525) @@ -40,6 +40,8 @@ static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/ #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. @@ -1978,7 +2028,9 @@ getprompt(void *unused __unused) { static char ps[PROMPTLEN]; const char *fmt; + const char *home; const char *pwd; + size_t homelen; int i, trim; static char internal_error[] = "??"; @@ -2025,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, @@ -2039,8 +2102,24 @@ getprompt(void *unused __unused) *pwd == '/' && pwd[1] != '\0') strlcpy(&ps[i], strrchr(pwd, '/') + 1, PROMPTLEN - i); - else - strlcpy(&ps[i], pwd, PROMPTLEN - i); + else { + home = lookupvar("HOME"); + if (home != NULL) + homelen = strlen(home); + if (home != NULL && + strcmp(home, "/") != 0 && + strncmp(pwd, home, homelen) == 0 && + (pwd[homelen] == '/' || + pwd[homelen] == '\0')) { + strlcpy(&ps[i], "~", + PROMPTLEN - i); + strlcpy(&ps[i + 1], + pwd + homelen, + PROMPTLEN - i - 1); + } else { + strlcpy(&ps[i], pwd, PROMPTLEN - i); + } + } /* Skip to end of path. */ while (ps[i + 1] != '\0') i++; Modified: stable/12/bin/sh/profile ============================================================================== --- stable/12/bin/sh/profile Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/bin/sh/profile Sat Jul 25 11:57:39 2020 (r363525) @@ -2,10 +2,6 @@ # # System-wide .profile file for sh(1). # -# Uncomment this to give you the default 4.2 behavior, where disk -# information is shown in K-Blocks -# BLOCKSIZE=K; export BLOCKSIZE -# # For the setting of languages and character sets please see # login.conf(5) and in particular the charset and lang options. # For full locales list check /usr/share/locale/* Modified: stable/12/bin/sh/sh.1 ============================================================================== --- stable/12/bin/sh/sh.1 Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/bin/sh/sh.1 Sat Jul 25 11:57:39 2020 (r363525) @@ -1417,6 +1417,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 Modified: stable/12/share/skel/dot.cshrc ============================================================================== --- stable/12/share/skel/dot.cshrc Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/share/skel/dot.cshrc Sat Jul 25 11:57:39 2020 (r363525) @@ -15,7 +15,6 @@ alias ll ls -lAF # These are normally set through /etc/login.conf. You may override them here # if wanted. # set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin) -# setenv BLOCKSIZE K # A righteous umask # umask 22 Modified: stable/12/share/skel/dot.profile ============================================================================== --- stable/12/share/skel/dot.profile Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/share/skel/dot.profile Sat Jul 25 11:57:39 2020 (r363525) @@ -8,7 +8,6 @@ # These are normally set through /etc/login.conf. You may override them here # if wanted. # PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$HOME/bin; export PATH -# BLOCKSIZE=K; export BLOCKSIZE # Setting TERM is normally done through /etc/ttys. Do only override # if you're sure that you'll never log in via telnet or xterm or a @@ -20,6 +19,9 @@ PAGER=less; export PAGER # set ENV to a file invoked each time sh is started for interactive use. ENV=$HOME/.shrc; export ENV + +# Let sh(1) know it's at home, despite /home being a symlink. +if [ "$PWD" != "$HOME" ] && [ "$PWD" -ef "$HOME" ] ; then cd ; fi # Query terminal size; useful for serial lines. if [ -x /usr/bin/resizewin ] ; then /usr/bin/resizewin -z ; fi Modified: stable/12/share/skel/dot.shrc ============================================================================== --- stable/12/share/skel/dot.shrc Sat Jul 25 11:34:50 2020 (r363524) +++ stable/12/share/skel/dot.shrc Sat Jul 25 11:57:39 2020 (r363525) @@ -32,12 +32,8 @@ alias g='egrep -i' # alias rm='rm -i' -# # set prompt: ``username@hostname$ '' -# PS1="`whoami`@`hostname | sed 's/\..*//'`" -# case `id -u` in -# 0) PS1="${PS1}# ";; -# *) PS1="${PS1}$ ";; -# esac +# set prompt: ``username@hostname:directory $ '' +PS1="\u@\h:\w \\$ " # search path for cd(1) # CDPATH=:$HOME
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202007251157.06PBvdBo095555>