From cc74038008611645e78d23f8ae62669e32f4371f Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 19:13:53 -0400 Subject: [PATCH 01/11] create the shell loop --- Makefile | 13 +++++++------ src/include/loop.h | 2 ++ src/loop.c | 18 ++++++++++++++++++ src/myshellin.c | 5 +++-- 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/include/loop.h create mode 100644 src/loop.c diff --git a/Makefile b/Makefile index 111776e..28fe7d8 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,14 @@ CC=clang -CFLAGS=-Wall -Isrc/include -DDEBUG -g -LDFLAGS=-lm -SRC=src/myshellin.c +CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17 +LDFLAGS= +FILENAME=myshellin +SRC=src/myshellin.c src/loop.c OBJ=$(SRC:.c=.o) -all: myshellin informe +all: myshellin myshellin: $(OBJ) - $(CC) -o $@ $^ $(LDFLAGS) + $(CC) $(CFLAGS) -o $(FILENAME) $^ $(LDFLAGS) informe: # if pdflatex is installed create the informe @@ -16,7 +17,7 @@ ifneq (, $(shell which pdflatex)) mv doc/Informe.pdf Informe.pdf endif -clean: cleanmyshellin cleaninforme +clean: cleanmyshellin cleanmyshellin: rm -f src/*.o myshellin diff --git a/src/include/loop.h b/src/include/loop.h new file mode 100644 index 0000000..87301ec --- /dev/null +++ b/src/include/loop.h @@ -0,0 +1,2 @@ +void print_input_line(); +void loop(); diff --git a/src/loop.c b/src/loop.c new file mode 100644 index 0000000..afbc009 --- /dev/null +++ b/src/loop.c @@ -0,0 +1,18 @@ +#define _GNU_SOURCE +#include +#include +#include + +void print_input_line() { + printf("$ "); +} + +void loop() { + size_t buffer_size = 0; + char *line = NULL; + + while (1) { + print_input_line(); + getline(&line, &buffer_size, stdin); + } +} diff --git a/src/myshellin.c b/src/myshellin.c index 6edf587..5200c5d 100644 --- a/src/myshellin.c +++ b/src/myshellin.c @@ -1,5 +1,6 @@ -int main(int argc, char **argv) -{ +#include "loop.h" +int main(int argc, char **argv) { + loop(); return 0; } From 93ace63885d65448016fe2ac7c55fccd1044634c Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:05:20 -0400 Subject: [PATCH 02/11] add user name printing and newline stripping --- Makefile | 2 +- src/include/loop.h | 1 + src/include/user.h | 1 + src/loop.c | 29 +++++++++++++++++++++++++++-- src/user.c | 9 +++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/include/user.h create mode 100644 src/user.c diff --git a/Makefile b/Makefile index 28fe7d8..5538f97 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=src/myshellin.c src/loop.c src/user.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/src/include/loop.h b/src/include/loop.h index 87301ec..4543551 100644 --- a/src/include/loop.h +++ b/src/include/loop.h @@ -1,2 +1,3 @@ +void remove_new_line(char *line); void print_input_line(); void loop(); diff --git a/src/include/user.h b/src/include/user.h new file mode 100644 index 0000000..b9b383a --- /dev/null +++ b/src/include/user.h @@ -0,0 +1 @@ +char *get_user(); diff --git a/src/loop.c b/src/loop.c index afbc009..3e744aa 100644 --- a/src/loop.c +++ b/src/loop.c @@ -1,10 +1,18 @@ #define _GNU_SOURCE +#include #include #include +#include #include +#include "user.h" + +void remove_new_line(char* line) { + line[strcspn(line, "\n")] = 0; +} void print_input_line() { - printf("$ "); + char *name = get_user(); + printf("%s $ ", name); } void loop() { @@ -13,6 +21,23 @@ void loop() { while (1) { print_input_line(); - getline(&line, &buffer_size, stdin); + if (getline(&line, &buffer_size, stdin) == -1) { + if (feof(stdin)) { + // the stdin was closed, this usually happens for CTRL-D + printf("\n"); + exit(EXIT_SUCCESS); + } + else { + perror("Error: "); + printf("\n"); + exit(EXIT_FAILURE); + } + } + + remove_new_line(line); + + if (strcmp(line, "quit") == 0) { + break; + } } } diff --git a/src/user.c b/src/user.c new file mode 100644 index 0000000..50f2922 --- /dev/null +++ b/src/user.c @@ -0,0 +1,9 @@ +#include +#include +#include + +char *get_user() { + struct passwd *pass; + pass = getpwuid(getuid()); + return pass->pw_name; +} From a1febd2f68d1b45d86a45d535ac7df2aec0326cf Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:05:39 -0400 Subject: [PATCH 03/11] use exit success in main --- src/myshellin.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/myshellin.c b/src/myshellin.c index 5200c5d..5d9b7e0 100644 --- a/src/myshellin.c +++ b/src/myshellin.c @@ -1,6 +1,7 @@ +#include #include "loop.h" int main(int argc, char **argv) { loop(); - return 0; + return EXIT_SUCCESS; } From 819b618e0f5665e5545e2458f02e99f067a76a28 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:23:49 -0400 Subject: [PATCH 04/11] add protection to headers --- src/include/loop.h | 3 +++ src/include/user.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/include/loop.h b/src/include/loop.h index 4543551..24b4c28 100644 --- a/src/include/loop.h +++ b/src/include/loop.h @@ -1,3 +1,6 @@ +#ifndef _MYSHELLIN_LOOP +#define _MYSHELLIN_LOOP void remove_new_line(char *line); void print_input_line(); void loop(); +#endif diff --git a/src/include/user.h b/src/include/user.h index b9b383a..0b4b055 100644 --- a/src/include/user.h +++ b/src/include/user.h @@ -1 +1,4 @@ +#ifndef _MYSHELLIN_USER +#define _MYSHELLIN_USER char *get_user(); +#endif From cf847bf5eb9db6a1e0c573255e73be40792710b3 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:25:27 -0400 Subject: [PATCH 05/11] add copyright to source files --- src/include/loop.h | 15 +++++++++++++++ src/include/user.h | 15 +++++++++++++++ src/loop.c | 15 +++++++++++++++ src/myshellin.c | 15 +++++++++++++++ src/user.c | 15 +++++++++++++++ 5 files changed, 75 insertions(+) diff --git a/src/include/loop.h b/src/include/loop.h index 24b4c28..89a9364 100644 --- a/src/include/loop.h +++ b/src/include/loop.h @@ -1,3 +1,18 @@ +/* + * 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_LOOP #define _MYSHELLIN_LOOP void remove_new_line(char *line); diff --git a/src/include/user.h b/src/include/user.h index 0b4b055..9a3b832 100644 --- a/src/include/user.h +++ b/src/include/user.h @@ -1,3 +1,18 @@ +/* + * 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_USER #define _MYSHELLIN_USER char *get_user(); diff --git a/src/loop.c b/src/loop.c index 3e744aa..f6e6b27 100644 --- a/src/loop.c +++ b/src/loop.c @@ -1,3 +1,18 @@ +/* + * 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 diff --git a/src/myshellin.c b/src/myshellin.c index 5d9b7e0..79024c1 100644 --- a/src/myshellin.c +++ b/src/myshellin.c @@ -1,3 +1,18 @@ +/* + * 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 "loop.h" diff --git a/src/user.c b/src/user.c index 50f2922..0193313 100644 --- a/src/user.c +++ b/src/user.c @@ -1,3 +1,18 @@ +/* + * 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 From e717917e950d573e81f4396058210d7ae06abf93 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:28:53 -0400 Subject: [PATCH 06/11] don't exit on success, let cleanup happen --- src/loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/loop.c b/src/loop.c index f6e6b27..c16b63a 100644 --- a/src/loop.c +++ b/src/loop.c @@ -40,7 +40,7 @@ void loop() { if (feof(stdin)) { // the stdin was closed, this usually happens for CTRL-D printf("\n"); - exit(EXIT_SUCCESS); + break; } else { perror("Error: "); From b5e4de3f823c581d3aa6b42b0286906781662c9d Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:30:08 -0400 Subject: [PATCH 07/11] free up the line pointer after we are finished --- src/loop.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/loop.c b/src/loop.c index c16b63a..2f3697c 100644 --- a/src/loop.c +++ b/src/loop.c @@ -40,6 +40,7 @@ void loop() { if (feof(stdin)) { // the stdin was closed, this usually happens for CTRL-D printf("\n"); + free(line); break; } else { @@ -52,6 +53,7 @@ void loop() { remove_new_line(line); if (strcmp(line, "quit") == 0) { + free(line); break; } } From 21448cee37e663a268e7f2b59543bfce83e690f0 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:32:26 -0400 Subject: [PATCH 08/11] free line on error in loop --- src/loop.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/loop.c b/src/loop.c index 2f3697c..44ad49f 100644 --- a/src/loop.c +++ b/src/loop.c @@ -46,6 +46,9 @@ void loop() { else { perror("Error: "); printf("\n"); + if (line != NULL) { + free(line); + } exit(EXIT_FAILURE); } } From 7c277d99bd8be6dd6b5bbbfb2faf3429054b21fe Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:52:42 -0400 Subject: [PATCH 09/11] get current working directory for the console line --- src/include/loop.h | 1 + src/loop.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/include/loop.h b/src/include/loop.h index 89a9364..3a6a208 100644 --- a/src/include/loop.h +++ b/src/include/loop.h @@ -16,6 +16,7 @@ #ifndef _MYSHELLIN_LOOP #define _MYSHELLIN_LOOP void remove_new_line(char *line); +char *get_working_directory(); void print_input_line(); void loop(); #endif diff --git a/src/loop.c b/src/loop.c index 44ad49f..35dedea 100644 --- a/src/loop.c +++ b/src/loop.c @@ -15,19 +15,36 @@ #define _GNU_SOURCE #include +#include #include #include #include #include +#include #include "user.h" void remove_new_line(char* line) { line[strcspn(line, "\n")] = 0; } +char *get_working_directory() { + char *cwd = NULL; + cwd = getcwd(NULL, PATH_MAX); + if (cwd != NULL) { + return cwd; + } + else { + perror("getcwd() error: "); + exit(EXIT_FAILURE); + } +} + void print_input_line() { char *name = get_user(); - printf("%s $ ", name); + char *cwd = get_working_directory(); + printf("%s:%s $ ", name, cwd); + free(name); + free(cwd); } void loop() { @@ -44,8 +61,7 @@ void loop() { break; } else { - perror("Error: "); - printf("\n"); + perror("getline() error: "); if (line != NULL) { free(line); } From 990a1d2b3ed5753c65654e433d89f934d37660c6 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 20:59:34 -0400 Subject: [PATCH 10/11] cleanup based on filename variable --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5538f97..209d10f 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ endif clean: cleanmyshellin cleanmyshellin: - rm -f src/*.o myshellin + rm -f src/*.o $(FILENAME) cleaninforme: make -C doc clean From a7cf48763149cf3a882b43384d20a0174b8f4007 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 19 Jun 2021 22:58:57 -0400 Subject: [PATCH 11/11] refactor the console line code --- Makefile | 2 +- src/{user.c => console_line.c} | 24 ++++++++++++++++++ src/include/{user.h => console_line.h} | 6 +++-- src/include/loop.h | 2 -- src/loop.c | 34 ++++++-------------------- 5 files changed, 36 insertions(+), 32 deletions(-) rename src/{user.c => console_line.c} (78%) rename src/include/{user.h => console_line.h} (92%) diff --git a/Makefile b/Makefile index 209d10f..1350e3c 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/user.c +SRC=src/myshellin.c src/loop.c src/console_line.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/src/user.c b/src/console_line.c similarity index 78% rename from src/user.c rename to src/console_line.c index 0193313..30f71eb 100644 --- a/src/user.c +++ b/src/console_line.c @@ -13,8 +13,12 @@ * 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 #include char *get_user() { @@ -22,3 +26,23 @@ char *get_user() { pass = getpwuid(getuid()); return pass->pw_name; } + +char *get_working_directory() { + char *cwd = NULL; + cwd = getcwd(NULL, PATH_MAX); + if (cwd != NULL) { + return cwd; + } + else { + perror("getcwd() error: "); + exit(EXIT_FAILURE); + } +} + +void print_input_line() { + char *name = get_user(); + char *cwd = get_working_directory(); + printf("%s:%s $ ", name, cwd); + free(name); + free(cwd); +} diff --git a/src/include/user.h b/src/include/console_line.h similarity index 92% rename from src/include/user.h rename to src/include/console_line.h index 9a3b832..6b0803c 100644 --- a/src/include/user.h +++ b/src/include/console_line.h @@ -13,7 +13,9 @@ * 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_USER -#define _MYSHELLIN_USER +#ifndef _MYSHELLIN_CONSOLE_LINE +#define _MYSHELLIN_CONSOLE_LINE char *get_user(); +char *get_working_directory(); +void print_input_line(); #endif diff --git a/src/include/loop.h b/src/include/loop.h index 3a6a208..adc2695 100644 --- a/src/include/loop.h +++ b/src/include/loop.h @@ -16,7 +16,5 @@ #ifndef _MYSHELLIN_LOOP #define _MYSHELLIN_LOOP void remove_new_line(char *line); -char *get_working_directory(); -void print_input_line(); void loop(); #endif diff --git a/src/loop.c b/src/loop.c index 35dedea..f5233e7 100644 --- a/src/loop.c +++ b/src/loop.c @@ -14,39 +14,15 @@ */ #define _GNU_SOURCE -#include -#include #include #include #include -#include -#include -#include "user.h" +#include "console_line.h" void remove_new_line(char* line) { line[strcspn(line, "\n")] = 0; } -char *get_working_directory() { - char *cwd = NULL; - cwd = getcwd(NULL, PATH_MAX); - if (cwd != NULL) { - return cwd; - } - else { - perror("getcwd() error: "); - exit(EXIT_FAILURE); - } -} - -void print_input_line() { - char *name = get_user(); - char *cwd = get_working_directory(); - printf("%s:%s $ ", name, cwd); - free(name); - free(cwd); -} - void loop() { size_t buffer_size = 0; char *line = NULL; @@ -57,7 +33,9 @@ void loop() { if (feof(stdin)) { // the stdin was closed, this usually happens for CTRL-D printf("\n"); - free(line); + if (line != NULL) { + free(line); + } break; } else { @@ -72,7 +50,9 @@ void loop() { remove_new_line(line); if (strcmp(line, "quit") == 0) { - free(line); + if (line != NULL) { + free(line); + } break; } }