We are having a lot of trouble interpreting our teacher. We asked for clarification and got the following back from him
For execve, send it a environment you setup with your exported variables and create a builtin command to spawn a subshell of /bin/bash, that way you can see your exported variables using env.
(He is talking about creating our own environment vars here.)
- Yes create your own. You can start by copying environ when your shell starts and add only exported variables
This is related to the following post on Stack Overflow by me (reading this other post will help you understand what I am trying to do):
using a new path with execve to run ls command
We are just very confused about this. One more time I will explain what we are trying to do now. Similar to how your Linux shell does this, we need to write our own program that can set environment variables like PATH and USER and whatever other vars the user wants to define.
An example of how you would call this would be (inside your program at its prompt):
mysetenv dog spike
which would create an environment variable looking like "dog=spike"
More importantly, we need to be able to set our own PATH variable and send it to an exec
command. This is the confusing part because, based on all of our questions, we don't understand what we are supposed to do.
I'm a little late to the party here, but if you want to preserve the old environment variables as well as creating your own, use
setenv
, and then passenviron
toexecve()
.environ
is a variable declared inunistd.h
, and it keeps track of the environment variables during this running process.setenv()
andputenv()
modifyenviron
, so when you pass it overexecve()
, the environment variables will be just as you'd expect.It is actually very simple. You already know that your arguments are a list of
char *
, terminated by a NULL pointer. Similarly, the environment is simply a list ofchar *
, terminated by a NULL pointer. Conventionally, the values in the list take the formVARNAME=var-value
, though you can pass other formats if you wish.So, to take a simple case:
In this example, the program will run
/bin/sh
with arguments-c
andenv
, which means that the shell will run theenv
program found on its current PATH. The environment here is set to contain 5 values in the orthodox format. If you changeenv
todate
(orenv; date
), you will see the effect of the TZ setting, for example. When I run that on my MacOS X machine, the output is:The shell has added environment variables
SHLVL
,_
andPWD
to the ones I set explicitly in theexecve()
call.You can also do fancier things, such as copy in some of the other environment variables from your genuine environment where they do not conflict with the ones you want to set explicitly. You can also play games like having two values for a single variable in the environment - which one takes effect? And you can play games with variable names that contain spaces (the shell doesn't like that much), or entries that do not match the 'varname=value' notation at all (no equals sign).
The code from Jonathan Leffler works great, except if you want to change the
PWD
(working directory) variable.What I did, in order to change the working directory, was to put a
chdir(..)
beforeexecve(..)
and call: