environ #15
16
src/array.c
16
src/array.c
@ -23,6 +23,10 @@
|
|||||||
*/
|
*/
|
||||||
StringArray *create_string_array() {
|
StringArray *create_string_array() {
|
||||||
StringArray *string_array = malloc(sizeof(StringArray));
|
StringArray *string_array = malloc(sizeof(StringArray));
|
||||||
|
if (string_array == NULL) {
|
||||||
|
perror("malloc");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
string_array->array = NULL;
|
string_array->array = NULL;
|
||||||
string_array->size = 0;
|
string_array->size = 0;
|
||||||
return string_array;
|
return string_array;
|
||||||
@ -35,25 +39,30 @@ StringArray *create_string_array() {
|
|||||||
*/
|
*/
|
||||||
void insert_string_array(StringArray *string_array, char *string) {
|
void insert_string_array(StringArray *string_array, char *string) {
|
||||||
if (string_array->size == 0) {
|
if (string_array->size == 0) {
|
||||||
string_array->array = malloc(sizeof(char *));
|
string_array->array = malloc(2 * sizeof(char *));
|
||||||
if (string_array->array == NULL) {
|
if (string_array->array == NULL) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
|
free_string_array(string_array);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *));
|
string_array->array = realloc(string_array->array, (string_array->size + 2) * sizeof(char *));
|
||||||
if (string_array->array == NULL) {
|
if (string_array->array == NULL) {
|
||||||
perror("realloc");
|
perror("realloc");
|
||||||
|
free_string_array(string_array);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
string_array->array[string_array->size] = malloc(sizeof(string));
|
string_array->array[string_array->size] = malloc(sizeof(string));
|
||||||
if (string_array->array == NULL) {
|
if (string_array->array == NULL) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
|
free_string_array(string_array);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
strcpy(string_array->array[string_array->size], string);
|
strcpy(string_array->array[string_array->size], string);
|
||||||
|
// A NULL terminated array
|
||||||
|
string_array->array[string_array->size + 1] = NULL;
|
||||||
string_array->size++;
|
string_array->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,9 +85,10 @@ void delete_string_array(StringArray *string_array, int index) {
|
|||||||
}
|
}
|
||||||
free(string_array->array[string_array->size - 1]);
|
free(string_array->array[string_array->size - 1]);
|
||||||
string_array->array[string_array->size - 1] = NULL;
|
string_array->array[string_array->size - 1] = NULL;
|
||||||
string_array->array = realloc(string_array->array, (string_array->size - 1) * sizeof(char *));
|
string_array->array = realloc(string_array->array, (string_array->size) * sizeof(char *));
|
||||||
if (string_array->array == NULL) {
|
if (string_array->array == NULL) {
|
||||||
perror("realloc");
|
perror("realloc");
|
||||||
|
free_string_array(string_array);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
string_array->size--;
|
string_array->size--;
|
||||||
|
14
src/launch.c
14
src/launch.c
@ -30,17 +30,11 @@ void launch_program(StringArray *args) {
|
|||||||
child = fork();
|
child = fork();
|
||||||
|
|
||||||
if (child == 0) {
|
if (child == 0) {
|
||||||
// Copy the array and add a NULL to the end of it
|
if (execvp(args->array[0], args->array) == -1) {
|
||||||
char *argv[args->size + 1];
|
fprintf(stderr, "%s: command not found\n", args->array[0]);
|
||||||
for (size_t i = 0; i < args->size; i++) {
|
free_string_array(args);
|
||||||
argv[i] = args->array[i];
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
argv[args->size] = NULL;
|
|
||||||
|
|
||||||
execvp(args->array[0], argv);
|
|
||||||
fprintf(stderr, "%s: command not found\n", args->array[0]);
|
|
||||||
free_string_array(args);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
else if (child < 0) {
|
else if (child < 0) {
|
||||||
perror("fork");
|
perror("fork");
|
||||||
|
Loading…
Reference in New Issue
Block a user