From f867a46565674e88a92cac5726e32e970d9de378 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 17 Jul 2021 17:36:38 -0400 Subject: [PATCH] refactor utils --- Makefile | 2 +- src/builtins.c | 2 +- src/console_line.c | 15 +-------------- src/include/console_line.h | 12 ------------ src/include/utils.h | 30 ++++++++++++++++++++++++++++++ src/loop.c | 1 + src/utils.c | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 src/include/utils.h create mode 100644 src/utils.c diff --git a/Makefile b/Makefile index 6ea739d..28b5edb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=clang 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 src/launch.c +SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c src/utils.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/src/builtins.c b/src/builtins.c index 19afbd7..ce14126 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -20,7 +20,7 @@ #include #include "array.h" #include "builtins.h" -#include "console_line.h" +#include "utils.h" bool is_builtin(char *command) { if (strcmp(command, "exit") == 0) { diff --git a/src/console_line.c b/src/console_line.c index 042bb07..cce2cf2 100644 --- a/src/console_line.c +++ b/src/console_line.c @@ -24,10 +24,7 @@ #include "array.h" #include "color.h" #include "console_line.h" - -void remove_new_line(char* line) { - line[strcspn(line, "\n")] = 0; -} +#include "utils.h" char *get_username() { struct passwd *pass = NULL; @@ -51,16 +48,6 @@ char *get_hostname() { return result; } -char *get_working_directory() { - char *cwd = malloc(PATH_MAX * sizeof(char *)); - getcwd(cwd, PATH_MAX); - if (cwd == NULL) { - perror("getcwd"); - exit(EXIT_FAILURE); - } - return cwd; -} - void print_input_line() { char *username = get_username(); char *hostname = get_hostname(); diff --git a/src/include/console_line.h b/src/include/console_line.h index e0f12ad..574f96c 100644 --- a/src/include/console_line.h +++ b/src/include/console_line.h @@ -16,12 +16,6 @@ #ifndef _MYSHELLIN_CONSOLE_LINE #define _MYSHELLIN_CONSOLE_LINE -/** - * Remove new line from the end of a string. - * @param line The string to remove the new line from. - */ -void remove_new_line(char *line); - /** * Get the logged in user's username. * @return Returns the logged in user's username. @@ -34,12 +28,6 @@ char *get_username(); */ char *get_hostname(); -/** - * Get the current working directory of the shell. - * @return Returns the current working directory. - */ -char *get_working_directory(); - /** * Print the console line before the user input. */ diff --git a/src/include/utils.h b/src/include/utils.h new file mode 100644 index 0000000..6129c68 --- /dev/null +++ b/src/include/utils.h @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#ifndef _MYSHELLIN_UTILS +#define _MYSHELLIN_UTILS + +/** + * Remove new line from the end of a string. + * @param line The string to remove the new line from. + */ +void remove_new_line(char *line); + +/** + * Get the current working directory of the shell. + * @return Returns the current working directory. + */ +char *get_working_directory(); +#endif diff --git a/src/loop.c b/src/loop.c index 5344420..caeed97 100644 --- a/src/loop.c +++ b/src/loop.c @@ -21,6 +21,7 @@ #include "builtins.h" #include "console_line.h" #include "launch.h" +#include "utils.h" void add_to_cleanup(void *data) { clean.array = realloc(clean.array, (clean.size + 1) * sizeof(void *)); diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..ff320b5 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,35 @@ +/* + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +void remove_new_line(char* line) { + line[strcspn(line, "\n")] = 0; +} + +char *get_working_directory() { + char *cwd = malloc(PATH_MAX * sizeof(char *)); + getcwd(cwd, PATH_MAX); + if (cwd == NULL) { + perror("getcwd"); + exit(EXIT_FAILURE); + } + return cwd; +}