more defensive code
This commit is contained in:
parent
b3886e8731
commit
02fcba22d2
2 changed files with 13 additions and 5 deletions
14
commands.c
14
commands.c
|
@ -47,6 +47,10 @@ int num_builtins() {
|
|||
int cd(char **args) {
|
||||
if (args[1] == NULL) {
|
||||
char *home = getenv("HOME");
|
||||
if (home == NULL) {
|
||||
fprintf(stderr, "rush: HOME environment variable is missing\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (chdir(home) != 0) {
|
||||
perror("rush");
|
||||
}
|
||||
|
@ -171,14 +175,14 @@ int execute(char **args) {
|
|||
fprintf(stderr, "rush: command not found: %s\n", args[0]);
|
||||
}
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
exit(EXIT_FAILURE); // exit the child
|
||||
} else if (pid < 0) {
|
||||
perror("Cannot fork");
|
||||
perror("fork failed");
|
||||
} else {
|
||||
// Parent process
|
||||
do {
|
||||
wpid = waitpid(pid, &status, WUNTRACED);
|
||||
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
while (!WIFEXITED(status) && !WIFSIGNALED(status)) {
|
||||
wpid = waitpid(pid, &status, WUNTRACED); // wait child to be exited to return to prompt
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
|
|
@ -17,6 +17,10 @@ void check_history_file() {
|
|||
// fallback to $HOME if $XDG_CONFIG_HOME is null
|
||||
env_home = getenv("HOME");
|
||||
}
|
||||
if (env_home == NULL) {
|
||||
fprintf(stderr, "rush: HOME AND XDG_CONFIG_HOME environment variable is missing\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int env_home_len = strlen(env_home);
|
||||
int histfilename_len = strlen(HISTFILE);
|
||||
int path_len = env_home_len + histfilename_len + 2; // 2 for slash and null byte
|
||||
|
|
Loading…
Reference in a new issue