commit 02fcba22d24a832f0381ea291d82c8b9267d3ea3
parent b3886e8731fe61a22d52064bf85663a6005f93d5
Author: night0721 <[email protected]>
Date: Tue, 6 Feb 2024 18:29:16 +0000
more defensive code
Diffstat:
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/commands.c b/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;
diff --git a/history.c b/history.c
@@ -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