1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
use std::env;
use std::os::unix::process::ExitStatusExt;
use std::process::{ExitStatus, Output};
/// cd
///
/// The builtin `cd` command. Used to change directories. This must be
/// implemented by the shell, since the present working directory
/// (`pwd`) is context sensitive within a process. If no arguments are
/// given, `cd` will take the user back to their home directory (i.e.
/// `~` / `$HOME`).
///
/// # Shell Example
/// ```sh
/// cd ~/.config # Change into /home/<user>/.config
/// cd / # Change into the root directory
/// cd # Change into the home directory
/// ```
pub fn incant(clause: &Option<Vec<String>>, uerr: bool) -> Output {
let status;
let out: Vec<u8> = Vec::new();
let mut err: Vec<u8> = Vec::new();
let path = match clause {
Some(path) => path[0].to_string(),
None => match env::var("HOME") {
Ok(val) => val,
Err(_) => {
status = 1;
if uerr {
err.append(&mut "cd: unknown home, staying in pwd\n".as_bytes().to_vec());
} else {
eprintln!("cd: unknown home, staying in pwd");
}
return Output {
status: ExitStatus::from_raw(status),
stdout: out,
stderr: err,
};
}
},
};
status = match std::env::set_current_dir(&path) {
Ok(_) => 0,
Err(e) => {
if uerr {
err.append(
&mut format!(
"cd: unable to change into {}: {}\n",
path,
e.to_string().to_lowercase()
)
.as_bytes()
.to_vec(),
);
} else {
eprintln!(
"cd: unable to change into {}: {}",
path,
e.to_string().to_lowercase()
);
}
1
}
};
Output {
status: ExitStatus::from_raw(status),
stdout: out,
stderr: err,
}
}
|