I need to write a program to display the value of the environment variables set
ID: 3677736 • Letter: I
Question
I need to write a program to display the value of the environment variables set for your shell. You can display these using the UNIX command:
jvm$ env
Your program will display them differently (i.e., sorted by variable name) and will also involve using the getenv(3) and malloc(3) functions.
You will need to parse the strings in the environment separating the name and value on either side of the equal sign (=). Do not try to use sscanf(3) to do this as sscanf expects to see white space between strings! You will need to write your own parsing function.
You set an environment variable with the command line:
jvm$ export FORMAT=REVERSE
And you unset an environment variable with the command line:
jvm$ export FORMAT
Lastly, you display the value of an environment variable with the command line:
jvm$ echo $FORMAT
Here are two abbreviated examples of the output expected; in name-value order:
name: Apple_PubSub_Socket_Render , value: /tmp/launch-DU1xCd/Render
name: EDITOR , value: vi
name: FORMAT , value:
name: HOME , value: /Users/rcm
name: LANG , value: en_US.UTF-8
name: LOGNAME , value: jvm
name: OLDPWD , value: /Users/jvm/IST332
And in value-name order:
value: /tmp/launch-DU1xCd/Render , name: Apple_PubSub_Socket_Render
value: vi , name: EDITOR
value: REVERSE , name: FORMAT
value: /Users/jmm , name: HOME
value: en_US.UTF-8 , name: LANG
value: rcm , name: LOGNAME
value: /Users/jvm/Exercises2 name: OLDPWD
I used field widths of 32 characters (i.e., with %-32s); you are not expected to get everything lined up perfectly.
Note that the value of the _ environment variable is set one way when using the env command, and set another way when you execute your program. Why do you think that is?
Any hwlp on accomplishing this?
Explanation / Answer
public class Test
{
public static void main(String args[])
{
ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
Map<String, String> env = pb.environment();
String arg=args[0]; // To input
String token[]=arg.split("=");
if(token.length>0)
{
env.put(token[0], token[1]);
Process p = pb.start();
InputStreamReader input = new InputStreamReader(p.getInputStream());
char[] buffer = new char[1024];
while (!input.ready()) {
;
}
while (input.read(buffer) != -1) {
System.out.println(buffer);
}}
else
{
if(args[0].contains("$"))
{
String newStr=args[0].replace("$","");
Map env = System.getenv();
TreeMap<String,String> mp=new TreeMap<String,String>();
for (Iterator it=env.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
if(entry.getKey().equals(newStr))
System.out.println(entry.getValue());
}
}
else
{
//unset it by removing it from the environment
}
}
System.out.println("PATH = " + System.getenv("PATH"));
// all of them
Map env = System.getenv();
TreeMap<String,String> mp=new TreeMap<String,String>();
for (Iterator it=env.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
mp.put(entry.getKey(),entry.getValue()); // To have sorted ordering of your environment variables.
}
// To display the Env Variables in sorted format:
Set<String> keys = mp.keySet();
for (String key : keys)
System.out.println(key + " = " + mp.get(key));
}
}