diff --git a/Makefile b/Makefile index b6b61db..6ea739d 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=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/src/include/launch.h b/src/include/launch.h new file mode 100644 index 0000000..d259664 --- /dev/null +++ b/src/include/launch.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. + */ + +#include "array.h" + +#ifndef _MYSHELLIN_LAUNCH +#define _MYSHELLIN_LAUNCH +void launch_program(StringArray *args); +#endif diff --git a/src/launch.c b/src/launch.c new file mode 100644 index 0000000..7a13621 --- /dev/null +++ b/src/launch.c @@ -0,0 +1,50 @@ +/* + * 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 "array.h" + +/** + * Launch programs from the OS. + * @param args The arguments to launch. + */ +void launch_program(StringArray *args) { + pid_t child = 0; + + child = fork(); + + if (child == 0) { + // Copy the array and add a NULL to the end of it + char *argv[args->size + 1]; + for (int i = 0; i < args->size; i++) { + argv[i] = args->array[i]; + } + argv[args->size] = NULL; + + execvp(args->array[0], argv); + perror("execvpe"); + } + else if (child < 0) { + perror("fork"); + } + else { + int child_status; + waitpid(child, &child_status, 0); + } +} diff --git a/src/loop.c b/src/loop.c index e7b6087..67b8105 100644 --- a/src/loop.c +++ b/src/loop.c @@ -20,6 +20,7 @@ #include "array.h" #include "builtins.h" #include "console_line.h" +#include "launch.h" /** * This is the loop that checks for user input and acts on it. @@ -52,6 +53,9 @@ void loop() { if (is_builtin(args.array[0])) { run_builtin(&args); } + else { + launch_program(&args); + } free_string_array(&args); }