From ccd5a1d1008b9d233f7ba06e1f6f743d1a7f5c50 Mon Sep 17 00:00:00 2001 From: tijesus Date: Wed, 6 Dec 2023 18:11:19 +0100 Subject: [PATCH 1/4] Function handling environmantal variables --- exec_env.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 exec_env.c diff --git a/exec_env.c b/exec_env.c new file mode 100644 index 0000000..e7cec01 --- /dev/null +++ b/exec_env.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include + +/** + * main - function to execute environment variables +*/ + +int main(int argc, char **argv, char **environ) { + char **env = environ; + + if (argc > 1 && strcmp(argv[1], "invoke_env") == 0) { + /* Print environment variables */ + while (*env != NULL) { + printf("%s\n", *env); + env++; + } + } else { + /* Construct the command without using system, avoiding shell injection */ + char *command[] = {"sh", "-c", "env", NULL}; + + // Create a child process + pid_t pid = fork(); + + if (pid == 0) { + /* This is the child process */ + execve("/bin/sh", command, environ); + /* execve only returns if an error occurs */ perror("execve"); + exit(EXIT_FAILURE); + } else if (pid < 0) { + /* Fork failed */ + perror("fork"); + exit(EXIT_FAILURE); + } else { + /* This is the parent process */ + /* Wait for the child to complete */ + waitpid(pid, NULL, 0); + } + } + + return 0; +} From 5614c85756805e7e7cf192faa2e0c845d3e3595f Mon Sep 17 00:00:00 2001 From: tijesus Date: Wed, 6 Dec 2023 18:25:34 +0100 Subject: [PATCH 2/4] Function handling environmantal variables --- exec_env.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/exec_env.c b/exec_env.c index e7cec01..8f78daa 100644 --- a/exec_env.c +++ b/exec_env.c @@ -7,6 +7,10 @@ /** * main - function to execute environment variables + * @argc: number of arguments + * @argv: vector of environment variables + * @environ: environment variables + * Return: 0 on success */ int main(int argc, char **argv, char **environ) { From a778837508132c98330a1fbd5353438dd580bf0d Mon Sep 17 00:00:00 2001 From: tijesus Date: Wed, 6 Dec 2023 18:27:43 +0100 Subject: [PATCH 3/4] Function handling environmantal variables --- exec_env.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec_env.c b/exec_env.c index 8f78daa..bb16e7f 100644 --- a/exec_env.c +++ b/exec_env.c @@ -8,8 +8,8 @@ /** * main - function to execute environment variables * @argc: number of arguments - * @argv: vector of environment variables - * @environ: environment variables + * @argv: Array of strings containing command-line arguments. + * @environ: Array of strings representing the environment variables. * Return: 0 on success */ From 507c8ffa946e22716195621cee62ae33a8eddc97 Mon Sep 17 00:00:00 2001 From: tijesus Date: Thu, 7 Dec 2023 12:31:59 +0100 Subject: [PATCH 4/4] seperated the exec_env function from main --- exec_env.c | 60 ++++++++++++++++++------------------------------------ main.c | 25 +++++++++++++++++++++++ main.h | 15 ++++++++++++++ 3 files changed, 60 insertions(+), 40 deletions(-) create mode 100644 main.c create mode 100644 main.h diff --git a/exec_env.c b/exec_env.c index bb16e7f..d0c418b 100644 --- a/exec_env.c +++ b/exec_env.c @@ -1,49 +1,29 @@ -#include -#include -#include -#include -#include -#include +#include "main.h" /** - * main - function to execute environment variables - * @argc: number of arguments - * @argv: Array of strings containing command-line arguments. - * @environ: Array of strings representing the environment variables. - * Return: 0 on success + * exec_env - function to execute environment variables */ -int main(int argc, char **argv, char **environ) { - char **env = environ; +void exec_env() { + char *command[] = {"sh", "-c", "env", NULL}; + char **env = environ; - if (argc > 1 && strcmp(argv[1], "invoke_env") == 0) { - /* Print environment variables */ - while (*env != NULL) { - printf("%s\n", *env); - env++; - } - } else { - /* Construct the command without using system, avoiding shell injection */ - char *command[] = {"sh", "-c", "env", NULL}; + /* Create a child process */ + pid_t pid = fork(); - // Create a child process - pid_t pid = fork(); - - if (pid == 0) { - /* This is the child process */ - execve("/bin/sh", command, environ); - /* execve only returns if an error occurs */ perror("execve"); - exit(EXIT_FAILURE); - } else if (pid < 0) { - /* Fork failed */ - perror("fork"); - exit(EXIT_FAILURE); - } else { + if (pid == 0) { + /* This is the child process */ + execve("/bin/sh", command, env); + /* execve only returns if an error occurs */ + perror("execve"); + exit(EXIT_FAILURE); + } else if (pid < 0) { + /* Fork failed */ + perror("fork"); + exit(EXIT_FAILURE); + } else { /* This is the parent process */ /* Wait for the child to complete */ - waitpid(pid, NULL, 0); - } - } - - return 0; + waitpid(pid, NULL, 0); + } } diff --git a/main.c b/main.c new file mode 100644 index 0000000..cee42e0 --- /dev/null +++ b/main.c @@ -0,0 +1,25 @@ +#include "main.h" + +/** + * main - function to execute environment variables + * @argc: number of arguments + * @argv: Array of strings containing command-line arguments. + * @environ: Array of strings representing the environment variables. + * Return: 0 on success +*/ + +int main(int argc, char **argv, char **environ) { + char **env = environ; + + if (argc > 1 && strcmp(argv[1], "invoke_env") == 0) { + /* Print environment variables */ + while (*env != NULL) { + printf("%s\n", *env); + env++; + } + } else { + exec_env(); + } + + return (0); +} diff --git a/main.h b/main.h new file mode 100644 index 0000000..1501e0d --- /dev/null +++ b/main.h @@ -0,0 +1,15 @@ +#ifndef MAIN_H +#define MAIN_H + +#include +#include +#include +#include +#include +#include + +extern char **environ; + +void exec_env(); + +#endif \ No newline at end of file