From 3daf9ab37d723fbd757c96d18690cc76ad368397 Mon Sep 17 00:00:00 2001 From: stitchy Date: Mon, 30 Oct 2023 14:15:49 -0700 Subject: [PATCH 1/3] Final Fixes for bad file name --- directories/csv_parser.c | 5 +++-- directories/movies_by_year.c | 8 +++++++- directories/readme.md | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/directories/csv_parser.c b/directories/csv_parser.c index a08b9fe..ed84b9b 100644 --- a/directories/csv_parser.c +++ b/directories/csv_parser.c @@ -6,7 +6,8 @@ struct node* parse_csv(char* name) { file = fopen(name, "r"); if(!file) { perror("Error"); - exit(66); + printf("\n"); + return NULL; } // Buffer File For chars @@ -35,7 +36,7 @@ struct node* parse_csv(char* name) { free(buffer); fclose(file); - printf("Processed file %s and processed data for %d movies.\n\n", name, count_nodes(head)); + printf("Processed file %s and processed data for %d movies.\n", name, count_nodes(head)); return head; diff --git a/directories/movies_by_year.c b/directories/movies_by_year.c index 1ff91a7..d38f2e6 100644 --- a/directories/movies_by_year.c +++ b/directories/movies_by_year.c @@ -92,11 +92,14 @@ void choose_movie(char* dir_str) { //printf("\nDirectory: %s\n", dir_str); struct node* head = parse_csv(dir_str); + if(!head) + return; + // Generate the String for movies char* dir = malloc(sizeof(char)*100); sprintf(dir, "temp.movies.%d", rand() % 100000); - printf("Created directory with the name %s\n", dir); + printf("Created directory with the name %s\n\n", dir); print_csv(head, dir); free(dir_str); @@ -132,6 +135,9 @@ void index_nodes(int* val, void* head) { void print_csv(struct node* head, char* dir) { + if(!head) + return; + int result = mkdir(dir, 0750); chdir(dir); diff --git a/directories/readme.md b/directories/readme.md index 3a3c009..185eb40 100644 --- a/directories/readme.md +++ b/directories/readme.md @@ -34,5 +34,5 @@ make run To specify a CSV file, you can type: ``` -./movies_by_year $(CSV_File_to_read.csv) +./movies_by_year ``` -- 2.45.2 From ac6dbe8cd73de0243841d1df990c5d09956f65af Mon Sep 17 00:00:00 2001 From: stitchy Date: Thu, 9 Nov 2023 11:07:32 +0000 Subject: [PATCH 2/3] commiting_from_my_own_personal_shell --- smallsh/main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ smallsh/main.h | 15 ++++++++ smallsh/makefile | 17 +++++++++ smallsh/node.c | 62 ++++++++++++++++++++++++++++++++ smallsh/node.h | 24 +++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 smallsh/main.c create mode 100644 smallsh/main.h create mode 100644 smallsh/makefile create mode 100644 smallsh/node.c create mode 100644 smallsh/node.h diff --git a/smallsh/main.c b/smallsh/main.c new file mode 100644 index 0000000..fbd9ddc --- /dev/null +++ b/smallsh/main.c @@ -0,0 +1,92 @@ +#include "main.h" + +int main() { + + int status = 0; + size_t input_size = 2048; + size_t getline_len; + char* input = malloc(sizeof(char) * input_size); + + while(1) { + printf(": "); + fflush(stdout); + getline_len = getline(&input, &input_size, stdin); + input[getline_len - 1] = '\0'; + + // Ignore whitespace or comments + if(input == NULL || + input[0] == '#') { + continue; + } + + + char*** array = malloc(sizeof(char**)); + int num_strings = input_format(input, input_size, array); + + char* first_array = (*array)[0]; + + // Built in shell commands + if(!strncmp(first_array, "exit", 4)) + return 0; + else if(!strncmp(first_array, "cd", 2)) + inbuilt_cd((*array)[1]); + else if (!strncmp(first_array, "status", 6)) + printf("%d\n", status); + else + status = run_command(array, num_strings); + fflush(stdout); + + } +} + +int input_format(char* input, size_t input_size, char*** array) { + + const int max_args = 512; + int num_strings = 0; + char* pch; + *array = malloc(sizeof(char*) * max_args); + + pch = strtok (input," "); + while (pch != NULL) + { + (*array)[num_strings] = malloc(sizeof(char*) * (strlen(pch) + 1)); + strcpy((*array)[num_strings], pch); + num_strings++; + + pch = strtok (NULL, " "); + } + + return num_strings; +} + +void inbuilt_cd(char* args) { + + if(args) + chdir(args); + else + chdir(getenv("HOME")); + +} + +int run_command(char*** array, int num_array) { + + char* comb_string = malloc(sizeof(char) * 100); + sprintf(comb_string, "/usr/bin/"); + strcat(comb_string, (*array)[0]); + int f = fork(); + pid_t pid; + if(!f) + pid = execv(comb_string, (*array)); + + int status; + waitpid(pid, &status, 0); + + free(comb_string); + + return status; + +} + + + + diff --git a/smallsh/main.h b/smallsh/main.h new file mode 100644 index 0000000..1795bdf --- /dev/null +++ b/smallsh/main.h @@ -0,0 +1,15 @@ + +#ifndef MAIN +#define MAIN + +#include "stdlib.h" +#include "stdio.h" +#include "string.h" +#include "unistd.h" +#include "sys/wait.h" + +int input_format(char*, size_t, char***); +void inbuilt_cd(char*); +int run_command(char***, int); + +#endif diff --git a/smallsh/makefile b/smallsh/makefile new file mode 100644 index 0000000..27130ea --- /dev/null +++ b/smallsh/makefile @@ -0,0 +1,17 @@ +CC=gcc --std=gnu99 -g +output=smallsh + +all: main.c main.h node.o input.o + $(CC) main.c node.o input.o -o $(output) + +node.o: node.c node.h + $(CC) -c node.c -o node.o + +input.o: input.h input.c + $(CC) -c input.c -o input.o + +test: all + ./p3testscript + +clean: + rm -fr *.o vgcore.* $(output) diff --git a/smallsh/node.c b/smallsh/node.c new file mode 100644 index 0000000..698180c --- /dev/null +++ b/smallsh/node.c @@ -0,0 +1,62 @@ + +#include "node.h" + +void append_node(struct node* head, struct node* node_app) { + + if(!head) { + head = node_app; + return; + } + + struct node* temp = head; + + while(temp->node != NULL) { + temp = temp->node; + } + temp->node = node_app; + +} + +struct node* appendv_node(struct node* head, void* data) { + + struct node* node = malloc(sizeof(struct node)); + node->data = data; + node->node = NULL; + + append_node(head, node); + + return node; +} + +int count_nodes(struct node* head) { + + int num = 1; + + if(!head) + return -1; + + struct node* temp = head; + while(temp->node) { + + temp = temp->node; + num++; + } + + return num; +} + +void iterate_nodes(struct node* head, void *func (void*, void*), void* val) { + + if(!head) + return; + + func(head->data, val); + + struct node* temp = head; + + while(temp->node) { + temp = temp->node; + func(temp->data, val); + } +} + diff --git a/smallsh/node.h b/smallsh/node.h new file mode 100644 index 0000000..71f4d6d --- /dev/null +++ b/smallsh/node.h @@ -0,0 +1,24 @@ +#include "stdlib.h" + +#ifndef NODES +#define NODES + +struct node { + struct node* node; + void* data; +}; + +// Appends Node to list +void append_node(struct node*, struct node*); + +// Creates node with value and appends +// Returns node that was created +struct node* appendv_node(struct node*, void*); + +// Counts the number of nodes in a list +int count_nodes(struct node*); + +// Iterates over every node in a list and runs the given function +void iterate_nodes(struct node*, void *func(void*, void*), void*); + +#endif -- 2.45.2 From 708e26cdfd5ea439242a15a2cdc4466ab8d36391 Mon Sep 17 00:00:00 2001 From: stitchy Date: Wed, 15 Nov 2023 11:38:57 +0000 Subject: [PATCH 3/3] Add horrible code --- smallsh/main.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++- smallsh/main.h | 7 ++ 2 files changed, 183 insertions(+), 3 deletions(-) diff --git a/smallsh/main.c b/smallsh/main.c index fbd9ddc..762dcaf 100644 --- a/smallsh/main.c +++ b/smallsh/main.c @@ -1,25 +1,54 @@ #include "main.h" +volatile sig_atomic_t stop_flag = 0; +int stop_msg = 0; + int main() { + /* + struct sigaction sa; + sa.sa_handler = sigstop_handle; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + */ + + signal(SIGINT, sigint_handle); + signal(SIGTSTP, sigstop_handle); + int status = 0; size_t input_size = 2048; size_t getline_len; char* input = malloc(sizeof(char) * input_size); while(1) { + + clean_children(); + + //signal handling + if(stop_msg) { + if(stop_flag) + printf("Entering Stop Mode: Background Processes Do Not Work (&)\n"); + else + printf("Exiting Stop Mode: Background Processes Now Work (&)\n"); + stop_msg = 0; + } + + printf(": "); fflush(stdout); getline_len = getline(&input, &input_size, stdin); input[getline_len - 1] = '\0'; + expand_dollar(input, input_size); + // Ignore whitespace or comments - if(input == NULL || + if(input[0] == '\0' || input[0] == '#') { continue; } + char*** array = malloc(sizeof(char**)); int num_strings = input_format(input, input_size, array); @@ -73,13 +102,95 @@ int run_command(char*** array, int num_array) { char* comb_string = malloc(sizeof(char) * 100); sprintf(comb_string, "/usr/bin/"); strcat(comb_string, (*array)[0]); + + + //Check to see if the process is ran in the background + int background = 0; + if(!strncmp("&", (*array)[num_array - 1], 1)) { + if(!stop_flag) + background = 1; + (*array)[num_array - 1] = NULL; + } + + // Check to see if the process has output redirected + int redirect_o = 0; + char* redirect_o_str = NULL; + for(int i = 0; i < num_array; i++) { + + if((*array)[i] == NULL) + continue; + + if(!strncmp((*array)[i], ">", 1)) { + redirect_o = 1; + redirect_o_str = (*array)[i+1]; + + (*array)[i] = NULL; + (*array)[i+1] = NULL; + + break; + } + } + + sterilize_array(array, num_array); + + // Check to see if the process has input redirected + int redirect_i = 0; + char* redirect_i_str = NULL; + for(int i = 0; i < num_array; i++) { + + if((*array)[i] == NULL) + continue; + + if(!strncmp((*array)[i], "<", 1)) { + redirect_i = 1; + redirect_i_str = (*array)[i+1]; + + (*array)[i] = NULL; + (*array)[i+1] = NULL; + + break; + } + } + + sterilize_array(array, num_array); + int f = fork(); pid_t pid; - if(!f) + if(!f) { + if(redirect_o) { + int fd = open(redirect_o_str, O_WRONLY | O_CREAT, 0666); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + + } + else if(background) { + int fd = open("/dev/null", O_WRONLY, 0666); + //Direct to dev null + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + } + + if(redirect_i) { + int fd = open(redirect_i_str, O_RDONLY); + dup2(fd, STDIN_FILENO); + } + else if(background) { + int fd = open("/dev/null", O_RDONLY); + dup2(fd, STDIN_FILENO); + } + pid = execv(comb_string, (*array)); + perror(comb_string); + exit(1); + } + else if(background){ + printf("background pid is %d\n", f); + } + int status; - waitpid(pid, &status, 0); + if(!background) + waitpid(f, &status, 0); free(comb_string); @@ -88,5 +199,67 @@ int run_command(char*** array, int num_array) { } +void expand_dollar(char* input, size_t input_size) { + char pid[20]; + int pid_size = sprintf(pid, "%d", getpid()); + char* buffer = malloc(sizeof(char*) * (pid_size + input_size + 1)); + char* dollar; + + dollar = strstr(input, "$$"); + while(dollar != NULL) { + + dollar[0] = '%'; + dollar[1] = 's'; + sprintf(buffer, input, pid); + strcpy(input, buffer); + dollar = strstr(input, "$$"); + } +} + +void sterilize_array(char*** array, size_t num_array) { + + for(int i = 0; i < num_array; i++) { + + //check to see if it can be the end of the array + if((*array)[i] == NULL) { + int is_end = 1; + //check if there is any more non-null values in the array + for(int j = i; j < num_array; j++) { + if((*array)[j]) + is_end = 0; + } + + if(!is_end) { + + //shift the array by 1 bit + for(int j = i; j < num_array; j++) { + (*array)[j] = (*array)[j+1]; + + } + } + } + } +} + +void sigint_handle() { +} + +void sigstop_handle(int n) { + stop_flag = !stop_flag; + stop_msg = !stop_msg; +} + +void clean_children() { + + // Clean up any child process that has not been cleaned + int pid = 43110; + while(pid >0) { + int status; + pid = waitpid(0, &status, WNOHANG); + + if(pid > 0) + printf("\nbackground pid %d is done: exit value %d\n", pid, WEXITSTATUS(status)); + } +} diff --git a/smallsh/main.h b/smallsh/main.h index 1795bdf..ad14ecc 100644 --- a/smallsh/main.h +++ b/smallsh/main.h @@ -7,9 +7,16 @@ #include "string.h" #include "unistd.h" #include "sys/wait.h" +#include "fcntl.h" +#include "signal.h" int input_format(char*, size_t, char***); void inbuilt_cd(char*); int run_command(char***, int); +void expand_dollar(char*, size_t); +void sterilize_array(char***, size_t); +void sigint_handle(); +void sigstop_handle(); +void clean_children(); #endif -- 2.45.2