Understanding requirements for execve and setting environment vars?

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 of char terminated by a NULL pointer.

Conventionally, the values in the list take the form VARNAME=var-value though you can pass other formats if you wish.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

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) 2.

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 and 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. C path exec execve link|improve this question asked Oct 5 '11 at 3:56milly525 82% accept rate.

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 of char *, terminated by a NULL pointer.

Conventionally, the values in the list take the form VARNAME=var-value, though you can pass other formats if you wish. So, to take a simple case: #include #include int main(void) { char *argv = { "/bin/sh", "-c", "env", 0 }; char *envp = { "HOME=/", "PATH=/bin:/usr/bin", "TZ=UTC0", "USER=beelzebub", "LOGNAME=tarzan", 0 }; execve(argv0, &argv0, envp); fprintf(stderr, "Oops! \n"); return -1; } In this example, the program will run /bin/sh with arguments -c and env, which means that the shell will run the env program found on its current PATH.

The environment here is set to contain 5 values in the orthodox format. If you change env to date (or env; date), you will see the effect of the TZ setting, for example. When I run that on my MacOS X machine, the output is: USER=beelzebub PATH=/bin:/usr/bin PWD=/Users/jleffler/tmp/soq TZ=UTC0 SHLVL=1 HOME=/ LOGNAME=tarzan _=/usr/bin/env The shell has added environment variables SHLVL, _ and PWD to the ones I set explicitly in the execve() 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).

You really cleared that all up for me. I already turned everything in, but thanks! I get it now.

– milly Oct 8 '11 at 17:37.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions