diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-02 17:13:20 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-02 17:13:20 +1300 |
commit | 2a6a79d213b5940cb190f328d65a920d6be55cc9 (patch) | |
tree | 58365958f41e98103c7a6bba42e65bdb3b978041 | |
parent | Remove cuddled else (diff) | |
download | spsh-2a6a79d213b5940cb190f328d65a920d6be55cc9.tar.gz spsh-2a6a79d213b5940cb190f328d65a920d6be55cc9.zip |
Pass shell environment on to commands
-rw-r--r-- | cmd.c | 8 | ||||
-rw-r--r-- | loop.c | 4 | ||||
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | spsh.h | 5 |
4 files changed, 8 insertions, 13 deletions
@@ -1,21 +1,17 @@ #include "spsh.h" /* Process a read line into a command and arguments and try to execute it */ -void cmd(char *line) { +void cmd(char *line, char **environ) { char *cmd, *cmd_arg; pid_t pid; int status, cmd_argc; char *cmd_argv[MAX_ARGS]; - char *cmd_envp[MAX_ENVS]; /* First argument is always the executable file itself; we make it NULL to * start with, and then terminate the array with NULL */ cmd_argv[0] = cmd_argv[1] = NULL; cmd_argc = 1; - /* Environment is just empty for now, I'll figure this out later */ - cmd_envp[0] = NULL; - /* Read the command as the first token of the line */ cmd = strtok(line, ARG_DELIM); @@ -41,7 +37,7 @@ void cmd(char *line) { pid = fork(); if (pid == 0) { cmd_argv[0] = cmd; - execve(cmd, cmd_argv, cmd_envp); + execve(cmd, cmd_argv, environ); } waitpid(pid, &status, 0); } @@ -1,7 +1,7 @@ #include "spsh.h" /* Loop through reading commands until we see an EOF (^D) */ -void loop() { +void loop(char **environ) { char *line; /* Loop until we break */ @@ -13,7 +13,7 @@ void loop() { /* If the line is valid, try to run it as a command */ if (line != NULL) { if (strlen(line) > 0) { - cmd(line); + cmd(line, environ); } } @@ -1,13 +1,13 @@ #include "spsh.h" /* Entry function */ -int main(int argc, char *argv[]) { +int main(int argc, char *argv[], char **environ) { /* Show the banner with the warning */ banner(); /* Start looping through commands */ - loop(); + loop(environ); /* If we get to this point, things should be good */ exit(EXIT_SUCCESS); @@ -17,7 +17,6 @@ /* Maximum number of arguments and environment variables */ #define MAX_ARGS 64 -#define MAX_ENVS 64 /* The prompt to use */ #define PROMPT "spsh$ " @@ -27,8 +26,8 @@ /* Function prototypes to soothe separate files */ void banner(); -void loop(); -void cmd(char *line); +void loop(char **environ); +void cmd(char *line, char **environ); #endif |