diff --git a/Makefile b/Makefile index 111776e..1350e3c 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 src/console_line.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,10 +17,10 @@ ifneq (, $(shell which pdflatex)) mv doc/Informe.pdf Informe.pdf endif -clean: cleanmyshellin cleaninforme +clean: cleanmyshellin cleanmyshellin: - rm -f src/*.o myshellin + rm -f src/*.o $(FILENAME) cleaninforme: make -C doc clean diff --git a/src/console_line.c b/src/console_line.c new file mode 100644 index 0000000..30f71eb --- /dev/null +++ b/src/console_line.c @@ -0,0 +1,48 @@ +/* + * 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 +#include + +char *get_user() { + struct passwd *pass; + 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/console_line.h b/src/include/console_line.h new file mode 100644 index 0000000..6b0803c --- /dev/null +++ b/src/include/console_line.h @@ -0,0 +1,21 @@ +/* + * 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_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 new file mode 100644 index 0000000..adc2695 --- /dev/null +++ b/src/include/loop.h @@ -0,0 +1,20 @@ +/* + * 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); +void loop(); +#endif diff --git a/src/loop.c b/src/loop.c new file mode 100644 index 0000000..f5233e7 --- /dev/null +++ b/src/loop.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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include "console_line.h" + +void remove_new_line(char* line) { + line[strcspn(line, "\n")] = 0; +} + +void loop() { + size_t buffer_size = 0; + char *line = NULL; + + while (1) { + print_input_line(); + if (getline(&line, &buffer_size, stdin) == -1) { + if (feof(stdin)) { + // the stdin was closed, this usually happens for CTRL-D + printf("\n"); + if (line != NULL) { + free(line); + } + break; + } + else { + perror("getline() error: "); + if (line != NULL) { + free(line); + } + exit(EXIT_FAILURE); + } + } + + remove_new_line(line); + + if (strcmp(line, "quit") == 0) { + if (line != NULL) { + free(line); + } + break; + } + } +} diff --git a/src/myshellin.c b/src/myshellin.c index 6edf587..79024c1 100644 --- a/src/myshellin.c +++ b/src/myshellin.c @@ -1,5 +1,22 @@ -int main(int argc, char **argv) -{ +/* + * 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. + */ - return 0; +#include +#include "loop.h" + +int main(int argc, char **argv) { + loop(); + return EXIT_SUCCESS; }