environ #15

Merged
cromer merged 27 commits from environ into master 2021-07-17 22:15:24 -04:00
2 changed files with 34 additions and 0 deletions
Showing only changes of commit cc153bd481 - Show all commits

View File

@ -22,6 +22,11 @@ typedef struct {
size_t size;
} StringArray;
typedef struct {
void **array;
size_t size;
} CleanArray;
void create_string_array(StringArray *string_array);
void insert_string_array(StringArray *string_array, char *string);

View File

@ -22,10 +22,39 @@
#include "console_line.h"
#include "launch.h"
static CleanArray clean;
/**
* Add memory address to array to be cleaned up later.
* @param data The data to be cleaned up on exit.
*/
void add_to_cleanup(void *data) {
clean.array = realloc(clean.array, (clean.size + 1) * sizeof(void *));
clean.array[clean.size++] = data;
}
/**
* Cleanup memory when exiting.
*/
void exit_cleanup() {
for (size_t i = 0; i < clean.size; i++) {
if (clean.array[i] != NULL) {
free(clean.array[i]);
clean.array[i] = NULL;
}
}
if (clean.array != NULL) {
free(clean.array);
clean.array = NULL;
}
}
/**
* This is the loop that checks for user input and acts on it.
*/
void loop() {
clean.size = 0;
atexit(exit_cleanup);
while (1) {
print_input_line();