From d72773294778c407f84aad36036e5fca3f0dcb32 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 17:42:46 -0400 Subject: [PATCH 1/6] fix invalid free --- src/console_line.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/console_line.c b/src/console_line.c index dab2281..7ea5e34 100644 --- a/src/console_line.c +++ b/src/console_line.c @@ -55,6 +55,5 @@ void print_input_line() { char *name = get_user(); char *cwd = get_working_directory(); printf(BRIGHT_CYAN "%s" MAGENTA "@" RED "localhost" MAGENTA ":" BLUE "%s" MAGENTA "$ " RESET, name, cwd); - free(name); free(cwd); } From 79e2b6697b29779bca5dfa9e9834deb823c7f3b2 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 18:47:02 -0400 Subject: [PATCH 2/6] add a string array helper --- Makefile | 2 +- src/array.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ src/include/array.h | 33 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/array.c create mode 100644 src/include/array.h diff --git a/Makefile b/Makefile index 1350e3c..8327113 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=clang CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17 LDFLAGS= FILENAME=myshellin -SRC=src/myshellin.c src/loop.c src/console_line.c +SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/src/array.c b/src/array.c new file mode 100644 index 0000000..c09ab80 --- /dev/null +++ b/src/array.c @@ -0,0 +1,59 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "array.h" + +void create_string_array(StringArray *string_array) { + string_array->array = NULL; + string_array->size = 0; +} + +void insert_string_array(StringArray *string_array, char *string) { + if (string_array->size == 0) { + string_array->array = malloc(sizeof(char *)); + } + else { + string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *)); + } + string_array->array[string_array->size] = malloc(sizeof(string)); + strcpy(string_array->array[string_array->size], string); + string_array->size++; +} + +void delete_string_array(StringArray *string_array, int index) { + if (string_array->size > 0 && string_array->size > index) { + for (int i = index; i < string_array->size - 1; i++) { + free(string_array->array[i]); + string_array->array[i] = NULL; + string_array->array[i] = malloc(sizeof(string_array->array[i + 1])); + strcpy(string_array->array[i], string_array->array[i + 1]); + } + free(string_array->array[string_array->size - 1]); + string_array->array[string_array->size - 1] = NULL; + string_array->array = realloc(string_array->array, (string_array->size - 1) * sizeof(char *)); + string_array->size--; + } +} + +void free_string_array(StringArray *string_array) { + for (int i = 0; i < string_array->size; i++) { + free(string_array->array[i]); + string_array->array[i] = NULL; + } + free(string_array->array); + string_array->array = NULL; + string_array->size = 0; +} diff --git a/src/include/array.h b/src/include/array.h new file mode 100644 index 0000000..fef9b3a --- /dev/null +++ b/src/include/array.h @@ -0,0 +1,33 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#ifndef _MYSHELLIN_ARRAY +#define _MYSHELLIN_ARRAY +typedef struct { + char **array; + size_t size; +} StringArray; + +void create_string_array(StringArray *string_array); + +void insert_string_array(StringArray *string_array, char *string); + +void delete_string_array(StringArray *string_array, int index); + +void free_string_array(StringArray *string_array); + +#endif From 5ce2096b2ccad0419b0a547899bcbcc8b29e5fca Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 19:06:30 -0400 Subject: [PATCH 3/6] add builtins --- Makefile | 2 +- src/builtins.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/include/builtins.h | 24 ++++++++++++++++++++++++ src/loop.c | 34 ++++++++++++++++++++++++++++------ 4 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 src/builtins.c create mode 100644 src/include/builtins.h diff --git a/Makefile b/Makefile index 8327113..f322133 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=clang CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17 LDFLAGS= FILENAME=myshellin -SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c +SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/src/builtins.c b/src/builtins.c new file mode 100644 index 0000000..8bb2d8f --- /dev/null +++ b/src/builtins.c @@ -0,0 +1,41 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include "array.h" + +bool is_builtin(char *command) { + if (strcmp(command, "exit") == 0) { + return true; + } + + if (strcmp(command, "cd") == 0) { + return true; + } + + return false; +} + +void run_builtin(StringArray *string_array) { + if (strcmp(string_array->array[0], "exit") == 0) { + free_string_array(string_array); + exit(EXIT_SUCCESS); + } +} + +void exit_shell() { + +} diff --git a/src/include/builtins.h b/src/include/builtins.h new file mode 100644 index 0000000..e315dc4 --- /dev/null +++ b/src/include/builtins.h @@ -0,0 +1,24 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "array.h" + +#ifndef _MYSHELLIN_BUILTINS +#define _MYSHELLIN_BUILTINS +bool is_builtin(char *command); +void run_builtin(StringArray *string_array); +void exit_shell(); +#endif diff --git a/src/loop.c b/src/loop.c index cd41cfe..cfaf81f 100644 --- a/src/loop.c +++ b/src/loop.c @@ -17,6 +17,8 @@ #include #include #include +#include "array.h" +#include "builtins.h" #include "console_line.h" /** @@ -42,13 +44,15 @@ void loop() { printf("\n"); if (line != NULL) { free(line); + line = NULL; } - break; + exit(EXIT_SUCCESS); } else { perror("getline() error: "); if (line != NULL) { free(line); + line = NULL; } exit(EXIT_FAILURE); } @@ -56,11 +60,29 @@ void loop() { remove_new_line(line); - if (strcmp(line, "quit") == 0) { - if (line != NULL) { - free(line); - } - break; + StringArray string_array; + create_string_array(&string_array); + + char *saveptr = NULL; + char *token = strtok_r(line, " ", &saveptr); + while (token) { + insert_string_array(&string_array, token); + token = strtok_r(NULL, " ", &saveptr); } + if (line != NULL) { + free(line); + line = NULL; + } + + // The user didn't type anything so restart the loop + if (string_array.size == 0) { + continue; + } + + if (is_builtin(string_array.array[0])) { + run_builtin(&string_array); + } + + free_string_array(&string_array); } } From 5e5c9d8db30ca8eb243a73bfe94ba0a2a55e29db Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 19:09:41 -0400 Subject: [PATCH 4/6] move code to exit_shell function --- src/builtins.c | 9 +++++---- src/include/builtins.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/builtins.c b/src/builtins.c index 8bb2d8f..4648ad9 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -16,6 +16,7 @@ #include #include #include "array.h" +#include "builtins.h" bool is_builtin(char *command) { if (strcmp(command, "exit") == 0) { @@ -31,11 +32,11 @@ bool is_builtin(char *command) { void run_builtin(StringArray *string_array) { if (strcmp(string_array->array[0], "exit") == 0) { - free_string_array(string_array); - exit(EXIT_SUCCESS); + exit_shell(string_array); } } -void exit_shell() { - +void exit_shell(StringArray *string_array) { + free_string_array(string_array); + exit(EXIT_SUCCESS); } diff --git a/src/include/builtins.h b/src/include/builtins.h index e315dc4..ccaebbf 100644 --- a/src/include/builtins.h +++ b/src/include/builtins.h @@ -20,5 +20,5 @@ #define _MYSHELLIN_BUILTINS bool is_builtin(char *command); void run_builtin(StringArray *string_array); -void exit_shell(); +void exit_shell(StringArray *string_array); #endif From d7fe743d5c999beb98d96ce93ba9d0b8107d967b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 19:17:50 -0400 Subject: [PATCH 5/6] drop down to c11 standard to work on older distros --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f322133..0fb0bab 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=clang -CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17 +CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c11 LDFLAGS= FILENAME=myshellin SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c From 41e3b341752df96f7fb114dde9fc6f33292e2210 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 21:03:23 -0400 Subject: [PATCH 6/6] update readme --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 8b8290e..7ab650b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ # myshellin +myshellin es un proyecto semestral del ramo Sistemas Operativos. El propósito de este proyecto es crear un shell básico. +## Autores +- Christopher Cromer +- Raúl Raúl Hernandez + +## Requisitos software +- glibc +- make +- clang + +## Requisitos informe +- latex +- pdflatex + +## Compilar + +### Software +make myshellin + +### Informe +make informe + +## Licencia +El proyecto es bajo la licencia "[The 3-Clause BSD License](LICENSE)".