/* File: ~liux/public_html/courses/cop4610/examples/simple-env.cc
Purpose: Demonstrate how to use list all environmental variables
for COP4610
Author: Xiuwen Liu, based on an example in Advanced Programming
in the UNIX Environment, by W. Richard Stevens,
Addison-Wesley, 1992
On the web:
http://www.cs.fsu.edu/~liux/courses/cop4610/examples/simple-env.cc
Compiling command:
g++ -o simple-env simple-env.cc
*/
#include
#include
#include
#include
#include
#include
#include
#include
extern char **environ;
int interrupted;
int main(int argc, char *argv[], char *envp[])
{
int i;
char env_name[256];
char env_value[256];
char env_str[256];
cout << "There are two ways to show environmental variables.\n";
cout << "First through external variable \"environ\"\n";
cout << "Name for a new environment variable: ";
cin.getline((char *)env_name, (int)256);
cin.clear();
cout << "Value for the new environment variable: ";
cin.getline((char *)env_value, (int)256);
sprintf(env_str,"%s=%s", env_name, env_value);
if (putenv(env_str) < 0) {
perror("putenv");
}
cout << endl;
i = 0;
while(environ[i] != NULL) {
cout << "environ " << i << " -> " << environ[i] << endl;
i++;
}
cout << "Second way through the third argument of main \"envp\"\n";
i =0;
while(envp[i] != NULL) {
cout << "envp " << i << " -> " << envp[i] << endl;
i++;
}
execlp("env","env", (char *)NULL);
return 0;
}