more defensive code

This commit is contained in:
Night Kaly 2024-02-08 12:32:31 +00:00
parent 3cc6fb0d5a
commit 460105a7ba
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE

View file

@ -25,11 +25,16 @@ void check_history_file() {
int histfilename_len = strlen(HISTFILE);
int path_len = env_home_len + histfilename_len + 2; // 2 for slash and null byte
histfile_path = malloc(sizeof(char) * path_len);
if (histfile_path == NULL) {
fprintf(stderr, "rush: Error allocating memory\n");
exit(EXIT_FAILURE);
}
histfile_path[0] = '\0'; // initialise string
// concatenate home and history file name to a path
strcat(histfile_path, env_home);
strcat(histfile_path, "/");
strcat(histfile_path, HISTFILE);
histfile_path[path_len] = '\0';
histfile_path[path_len - 1] = '\0';
if (access(histfile_path, F_OK) != 0) { // check for file existence
history_file = fopen(histfile_path, "w"); // read and write, if doesn't exist, create
if (history_file == NULL) {
@ -87,6 +92,10 @@ char *read_command(int direction) {
cmd_count--;
search[last_nlf - search] = '\0'; // terminate string earlier to find second last \n, search points to first char and last_nlf is last \n, difference is the index of \n
char *first_cmd = malloc(sizeof(char) * (last_nlf - search) + 1);
if (first_cmd == NULL) {
fprintf(stderr, "rush: Error allocating memory\n");
exit(EXIT_FAILURE);
}
strcpy(first_cmd, search);
return first_cmd;
}