Projects STRLCPY alacritty Commits 246ec894
🤬
  • ■ ■ ■ ■ ■
    alacritty_terminal/src/tty/unix.rs
    skipped 117 lines
    118 118   }
    119 119  }
    120 120   
    121  -/// Look for a shell in the `$SHELL` environment variable, then in `passwd`.
    122  -fn default_shell(pw: &Passwd<'_>) -> String {
    123  - env::var("SHELL").unwrap_or_else(|_| pw.shell.to_owned())
     121 +/// User information that is required for a new shell session.
     122 +struct ShellUser {
     123 + user: String,
     124 + home: String,
     125 + shell: String,
     126 +}
     127 + 
     128 +impl ShellUser {
     129 + /// look for shell, username, longname, and home dir in the respective environment variables
     130 + /// before falling back on looking in to `passwd`.
     131 + fn from_env() -> Result<Self> {
     132 + let mut buf = [0; 1024];
     133 + let pw = get_pw_entry(&mut buf);
     134 + 
     135 + let user = match env::var("USER") {
     136 + Ok(user) => user,
     137 + Err(_) => match pw {
     138 + Ok(ref pw) => pw.name.to_owned(),
     139 + Err(err) => return Err(err),
     140 + },
     141 + };
     142 + 
     143 + let home = match env::var("HOME") {
     144 + Ok(home) => home,
     145 + Err(_) => match pw {
     146 + Ok(ref pw) => pw.dir.to_owned(),
     147 + Err(err) => return Err(err),
     148 + },
     149 + };
     150 + 
     151 + let shell = match env::var("SHELL") {
     152 + Ok(shell) => shell,
     153 + Err(_) => match pw {
     154 + Ok(ref pw) => pw.shell.to_owned(),
     155 + Err(err) => return Err(err),
     156 + },
     157 + };
     158 + 
     159 + Ok(Self { user, home, shell })
     160 + }
    124 161  }
    125 162   
    126 163  #[cfg(not(target_os = "macos"))]
    127  -fn default_shell_command(pw: &Passwd<'_>) -> Command {
    128  - Command::new(default_shell(pw))
     164 +fn default_shell_command(shell: &str, _user: &str) -> Command {
     165 + Command::new(shell)
    129 166  }
    130 167   
    131 168  #[cfg(target_os = "macos")]
    132  -fn default_shell_command(pw: &Passwd<'_>) -> Command {
    133  - let shell = default_shell(pw);
     169 +fn default_shell_command(shell: &str, user: &str) -> Command {
    134 170   let shell_name = shell.rsplit('/').next().unwrap();
    135 171   
    136 172   // On macOS, use the `login` command so the shell will appear as a tty session.
    skipped 8 lines
    145 181   // -p: Preserves the environment.
    146 182   //
    147 183   // XXX: we use zsh here over sh due to `exec -a`.
    148  - login_command.args(["-flp", pw.name, "/bin/zsh", "-c", &exec]);
     184 + login_command.args(["-flp", user, "/bin/zsh", "-c", &exec]);
    149 185   login_command
    150 186  }
    151 187   
    skipped 8 lines
    160 196   let _ = termios::tcsetattr(master, SetArg::TCSANOW, &termios);
    161 197   }
    162 198   
    163  - let mut buf = [0; 1024];
    164  - let pw = get_pw_entry(&mut buf)?;
     199 + let user = ShellUser::from_env()?;
    165 200   
    166 201   let mut builder = if let Some(shell) = config.shell.as_ref() {
    167 202   let mut cmd = Command::new(shell.program());
    168 203   cmd.args(shell.args());
    169 204   cmd
    170 205   } else {
    171  - default_shell_command(&pw)
     206 + default_shell_command(&user.shell, &user.user)
    172 207   };
    173 208   
    174 209   // Setup child stdin/stdout/stderr as slave fd of PTY.
    skipped 7 lines
    182 217   // Setup shell environment.
    183 218   let window_id = window_id.to_string();
    184 219   builder.env("ALACRITTY_WINDOW_ID", &window_id);
    185  - builder.env("LOGNAME", pw.name);
    186  - builder.env("USER", pw.name);
    187  - builder.env("HOME", pw.dir);
     220 + builder.env("USER", user.user);
     221 + builder.env("HOME", user.home);
    188 222   
    189 223   // Set Window ID for clients relying on X11 hacks.
    190 224   builder.env("WINDOWID", window_id);
    skipped 212 lines
Please wait...
Page is in error, reload to recover