aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-03-02 17:13:20 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-03-02 17:13:20 +1300
commit2a6a79d213b5940cb190f328d65a920d6be55cc9 (patch)
tree58365958f41e98103c7a6bba42e65bdb3b978041
parentRemove cuddled else (diff)
downloadspsh-2a6a79d213b5940cb190f328d65a920d6be55cc9.tar.gz
spsh-2a6a79d213b5940cb190f328d65a920d6be55cc9.zip
Pass shell environment on to commands
-rw-r--r--cmd.c8
-rw-r--r--loop.c4
-rw-r--r--main.c4
-rw-r--r--spsh.h5
4 files changed, 8 insertions, 13 deletions
diff --git a/cmd.c b/cmd.c
index 4834bfb..69b9bf3 100644
--- a/cmd.c
+++ b/cmd.c
@@ -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);
}
diff --git a/loop.c b/loop.c
index 2067e21..7ff2b3e 100644
--- a/loop.c
+++ b/loop.c
@@ -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);
}
}
diff --git a/main.c b/main.c
index 83cb686..8b983db 100644
--- a/main.c
+++ b/main.c
@@ -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);
diff --git a/spsh.h b/spsh.h
index 8318e0f..bf8a817 100644
--- a/spsh.h
+++ b/spsh.h
@@ -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