diff --git a/exec_env.c b/exec_env.c new file mode 100644 index 0000000..d0c418b --- /dev/null +++ b/exec_env.c @@ -0,0 +1,29 @@ +#include "main.h" + +/** + * exec_env - function to execute environment variables +*/ + +void exec_env() { + char *command[] = {"sh", "-c", "env", NULL}; + char **env = environ; + + /* Create a child process */ + pid_t pid = fork(); + + 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); + } +} 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