finish report
This commit is contained in:
33
doc/code/environ.txt
Normal file
33
doc/code/environ.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
typedef struct {
|
||||
StringArray *keys;
|
||||
StringArray *values;
|
||||
size_t size;
|
||||
} ArrayList;
|
||||
|
||||
void set_array_list(ArrayList *array_list, char *key, char *value) {
|
||||
if (array_list->keys == NULL) {
|
||||
array_list->keys = create_string_array();
|
||||
array_list->values = create_string_array();
|
||||
}
|
||||
for (size_t i = 0; i < array_list->size; i++) {
|
||||
if (strcmp(array_list->keys->array[i], key) == 0) {
|
||||
array_list->values->array[i] = realloc(array_list->values->array[i], strlen(value) * sizeof(char *));
|
||||
strcpy(array_list->values->array[i], value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
insert_string_array(array_list->keys, key);
|
||||
insert_string_array(array_list->values, value);
|
||||
array_list->size++;
|
||||
}
|
||||
|
||||
char *get_array_list(ArrayList *array_list, char *key) {
|
||||
if (array_list->keys != NULL) {
|
||||
for (size_t i = 0; i < array_list->size; i++) {
|
||||
if (strcmp(array_list->keys->array[i], key) == 0) {
|
||||
return array_list->values->array[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
44
doc/code/redirection.txt
Normal file
44
doc/code/redirection.txt
Normal file
@@ -0,0 +1,44 @@
|
||||
typedef struct {
|
||||
int fd;
|
||||
int fd_new;
|
||||
int fd_copy;
|
||||
char *filename;
|
||||
int flags;
|
||||
} Redirect;
|
||||
|
||||
Redirect in = {.fd = STDIN_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_RDONLY};
|
||||
Redirect out = {.fd = STDOUT_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
|
||||
Redirect err = {.fd = STDERR_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
|
||||
|
||||
void open_redirect(Redirect *redirect) {
|
||||
redirect->fd_copy = dup(redirect->fd);
|
||||
if (redirect->fd_copy == -1) {
|
||||
perror("dup");
|
||||
}
|
||||
else {
|
||||
redirect->fd_new = open(redirect->filename, redirect->flags, 0664);
|
||||
if (redirect->fd_new == -1) {
|
||||
fprintf(stderr, "open: Could not open file %s: \"%s\"\n", redirect->filename, strerror(errno));
|
||||
}
|
||||
else {
|
||||
if (close(redirect->fd) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
if (dup(redirect->fd_new) == -1) {
|
||||
perror("dup");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void close_redirect(Redirect *redirect) {
|
||||
if (close(redirect->fd_new) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
if (dup2(redirect->fd_copy, redirect->fd) == -1) {
|
||||
perror("dup2");
|
||||
}
|
||||
if (close(redirect->fd_copy) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user