Skip to content
Snippets Groups Projects
Commit 308baa8d authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Refactored main seems to be working

parent 0efc3a9d
No related branches found
No related tags found
No related merge requests found
Pipeline #20022 failed
......@@ -16,6 +16,7 @@
#include <unistd.h>
pid_t fork_n_exec(cmd_t command);
int spawn_process(cmd_t command);
bool is_available(cmd_t const command);
......
total 60K
drwxr-xr-x 9 tpo tpo 4.0K 20 oct 00:18 .
drwxr-xr-x 5 tpo tpo 4.0K 11 oct 23:05 ..
drwxr-xr-x 2 tpo tpo 4.0K 20 oct 00:18 bin
-rw-r--r-- 1 tpo tpo 1.6K 2 oct 17:08 .clang-format
drwxr-xr-x 2 tpo tpo 4.0K 11 oct 23:05 docs
drwxr-xr-x 8 tpo tpo 4.0K 19 oct 23:36 .git
-rw-r--r-- 1 tpo tpo 13 2 oct 18:34 .gitignore
-rw-r--r-- 1 tpo tpo 1.3K 18 oct 14:24 .gitlab-ci.yml
drwxr-xr-x 2 tpo tpo 4.0K 11 oct 23:05 include
-rw-r--r-- 1 tpo tpo 52 20 oct 00:18 ls.txt
-rw-r--r-- 1 tpo tpo 666 2 oct 21:11 Makefile
-rw-r--r-- 1 tpo tpo 2.1K 20 oct 00:17 readme.md
drwxr-xr-x 2 tpo tpo 4.0K 19 oct 23:17 src
drwxr-xr-x 2 tpo tpo 4.0K 19 oct 23:17 test
drwxr-xr-x 2 tpo tpo 4.0K 11 oct 23:05 .vscode
......@@ -49,9 +49,8 @@ bool blt_help(__attribute__((unused)) cmd_t command)
* Returns -1 if not builtin
*
* @param command - Command to check
* @param blt_commands - Builtin commands
* @return true if its builtin
* @return false otherwise
* @return Index of bulting command
* @return -1 not a builtin
*/
int is_builtin(cmd_t command)
{
......
......@@ -25,46 +25,32 @@ int main()
// ! PARSING THE USER INPUT
char usr_in[ARG_MAX];
int err_usr_in = ask_user_input(usr_in);
if ((err_usr_in == -1) || (strlen(usr_in) <= 0))
{
continue;
}
if (parse_command(usr_in, &command) == -1)
{
fprintf(stderr, "Couldnt parse the command\n");
}
// ? CHECK IF ITS BUILTIN
// ! BUILTIN OR JOB
int index_blt = is_builtin(command);
if (index_blt >= 0) // ! BUILTIN
// ! BUILTIN
if (index_blt >= 0)
{
run = g_builtin[index_blt].p_func(command);
}
else // ! JOB
// ! JOB
else
{
if (is_available(command))
{
pid_t pid = fork();
pid_t pid;
if ((pid = fork_n_exec(command)) == -1)
continue; // No child created, no need to wait
switch (pid)
{
case -1: // ERROR
die_errno("Could not fork");
break;
case 0: // CHILD
if (spawn_process(command) == -1)
{
dispose_command(&command);
continue;
}
break;
default: // PARENT
int status;
pid_t pid_dead_child = waitpid(pid, &status, 0);
printf("Process %d exited with code %d\n", pid_dead_child, status >> 8);
break;
}
}
else
{
......
......@@ -10,6 +10,30 @@
#include <fcntl.h>
#include <string.h>
int fork_n_exec(cmd_t command)
{
pid_t pid = fork();
switch (pid)
{
case -1: // ERROR - No child created, no need to wait
die_errno("Could not fork");
return -1;
case 0: // CHILD
if (spawn_process(command) == -1)
{
dispose_command(&command);
fprintf(stderr, "Could not execute the command properly\n");
return -1;
}
break;
default: // PARENT
return pid;
}
return -1;
}
/**
* @brief Check if a command is available to the user
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment