Repository: alacritty/alacritty
File: alacritty_terminal/src/tty/unix.rs (Rust)

Code before:
    // Look for a shell in SHELL, then in passwd.
    env::var("SHELL").unwrap_or_else(|_| pw.shell.to_owned())
}

#[cfg(not(target_os = "macos"))]
fn default_shell_command(pw: &Passwd<'_>) -> Command {
    Command::new(default_shell(pw))
}

#[cfg(target_os = "macos")]
fn default_shell_command(pw: &Passwd<'_>) -> Command {
    // On macOS, use the `login` command to launch the shell so programs like `w`, `who`, and
    // `logname` work.
    let shell = default_shell(pw);
    let shell_name = shell.rsplit('/').next().unwrap();
    let mut login_command = Command::new("/usr/bin/login");
    // -f bypasses authentication for the already-logged-in user
    // -l tells login that this is not a login session and not to change directories to $HOME
    // -p preserves the environment
    // exec the shell with argv[0] prepended by `-` so it becomes a login shell anyway
    let exec = format!("exec -a -{} {}", shell_name, shell);
    login_command.args(["-flp", pw.name, "/bin/sh", "-c", &exec]);
    login_command
}

/// Create a new TTY and return a handle to interact with it.
pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: Option<usize>) -> Result<Pty> {
    let (master, slave) = make_pty(window_size.to_winsize())?;

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    if let Ok(mut termios) = termios::tcgetattr(master) {
        // Set character encoding to UTF-8.
        termios.input_flags.set(InputFlags::IUTF8, true);
        let _ = termios::tcsetattr(master, SetArg::TCSANOW, &termios);
    }

    let mut buf = [0; 1024];
    let pw = get_pw_entry(&mut buf)?;

    let mut builder = if let Some(prog) = config.shell.as_ref() {
        let mut cmd = Command::new(prog.program());
        cmd.args(prog.args());
        cmd
    } else {
        default_shell_command(&pw)
    };

    // Setup child stdin/stdout/stderr as slave fd of PTY.
    // Ownership of fd is transferred to the Stdio structs and will be closed by them at the end of
    // this scope. (It is not an issue that the fd is closed three times since File::drop ignores
    // error on libc::close.).

Review comment (security):
This is somewhat of a mess, the comment just squished between two lines without any whitespace above it, documenting the two following lines in opposite order.

The exec comment should be above the exec line, the flag comments should be above the args line. If this code is so intricate that it requires documentation above every single line, then that's perfectly fine.

Also what does `-l` actually do? Can you maybe post the `login --help` output? Having `-l` be `no-login` seems odd.

And also please format the exec comment to be capitalized with a full stop at the end and maybe change the args comment to use `-f: Bypasses authentication for the logged-in user.` as a comment format.

Code after:
fn default_shell(pw: &Passwd<'_>) -> String {
    env::var("SHELL").unwrap_or_else(|_| pw.shell.to_owned())
}

#[cfg(not(target_os = "macos"))]
fn default_shell_command(pw: &Passwd<'_>) -> Command {
    Command::new(default_shell(pw))
}

#[cfg(target_os = "macos")]
fn default_shell_command(pw: &Passwd<'_>) -> Command {
    let shell = default_shell(pw);
    let shell_name = shell.rsplit('/').next().unwrap();

    // On macOS, use the `login` command so the shell will appear as a tty session.
    let mut login_command = Command::new("/usr/bin/login");

    // Exec the shell with argv[0] prepended by '-' so it becomes a login shell.
    // `login` normally does this itself, but `-l` disables this.
    let exec = format!("exec -a -{} {}", shell_name, shell);

    // -f: Bypasses authentication for the already-logged-in user.
    // -l: Skips changing directory to $HOME and prepending '-' to argv[0].
    // -p: Preserves the environment.
    login_command.args(["-flp", pw.name, "/bin/sh", "-c", &exec]);
    login_command
}

/// Create a new TTY and return a handle to interact with it.
pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: Option<usize>) -> Result<Pty> {
    let (master, slave) = make_pty(window_size.to_winsize())?;

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    if let Ok(mut termios) = termios::tcgetattr(master) {
        // Set character encoding to UTF-8.
        termios.input_flags.set(InputFlags::IUTF8, true);
        let _ = termios::tcsetattr(master, SetArg::TCSANOW, &termios);
    }

    let mut buf = [0; 1024];
    let pw = get_pw_entry(&mut buf)?;

    let mut builder = if let Some(shell) = config.shell.as_ref() {
        let mut cmd = Command::new(shell.program());
        cmd.args(shell.args());
        cmd
    } else {
        default_shell_command(&pw)
    };

    // Setup child stdin/stdout/stderr as slave fd of PTY.