Skip to content

Commit c440f81

Browse files
committed
sh: correctly handle error
1 parent d35376d commit c440f81

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

sh/src/shell/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::wordexp::{expand_word, expand_word_to_string, word_to_pattern};
3535
use nix::errno::Errno;
3636
use nix::libc;
3737
use nix::sys::wait::{WaitPidFlag, WaitStatus};
38-
use nix::unistd::{getpgrp, getpid, getppid, setpgid, ForkResult, Pid};
38+
use nix::unistd::{getcwd, getpgrp, getpid, getppid, setpgid, ForkResult, Pid};
3939
use std::collections::HashMap;
4040
use std::ffi::{CString, OsString};
4141
use std::fmt::{Display, Formatter};
@@ -942,13 +942,22 @@ impl Shell {
942942
environment.set_global_if_unset("PS4", "+ ");
943943
environment.set_global_if_unset("OPTIND", "1");
944944
let history = initialize_history_from_system(&environment);
945+
let current_directory = match getcwd() {
946+
Ok(path) => path.into_os_string(),
947+
Err(err) => {
948+
eprintln!(
949+
"sh: failed to determine the current working directory ({})",
950+
err
951+
);
952+
std::process::exit(1);
953+
}
954+
};
945955
Shell {
946956
environment,
947957
program_name,
948958
positional_parameters: args,
949959
shell_pid: getpid().as_raw(),
950-
// TODO: handle error
951-
current_directory: std::env::current_dir().unwrap().into_os_string(),
960+
current_directory,
952961
history,
953962
set_options,
954963
is_interactive,

0 commit comments

Comments
 (0)