launch #13

Merged
cromer merged 3 commits from launch into master 2021-06-28 16:05:54 -04:00
5 changed files with 79 additions and 3 deletions

View File

@ -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

View File

@ -20,7 +20,7 @@
#define _MYSHELLIN_BUILTINS
bool is_builtin(char *command);
void run_builtin(StringArray *string_array);
void run_builtin(StringArray *args);
void exit_shell(StringArray *string_array);
void exit_shell(StringArray *args);
#endif

21
src/include/launch.h Normal file
View File

@ -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

51
src/launch.c Normal file
View File

@ -0,0 +1,51 @@
/*
* 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 <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#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);
fprintf(stderr, "%s: command not found\n", args->array[0]);
exit(EXIT_FAILURE);
}
else if (child < 0) {
perror("fork");
}
else {
int child_status;
waitpid(child, &child_status, 0);
}
}

View File

@ -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);
}