Question: PROMPT: Write a program to display the value of the environment variables set for your shell. You can display these using the UNIX command: $

PROMPT:

Write a program to display the value of the environment variables set for your shell. You can display these using the UNIX command: $ 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. Warning: if your environment has an entry on the lxcluster for LS_COLORS, it will mess up the parsed display as the value of that environment variable is way too many characters, and it will require a char buffer way too big! You should null out the value of the LS_COLORS variables value with: $ export LS_COLORS= (where there is a newline after the = sign). This will leave in the variable name but leave out its value (which will reappear when you next log in to the lxcluster). In your program declare an extern char **environ (see the text section 7.5). Use this to count the number of entries defined in the environment. Next malloc (3) an array big enough to contain count character pointers. Copy the environments character pointers into this malloced array. (Be sure to free (3) this memory before exiting.) Using the copied array, sort the strings in alphabetically ascending order (use the ASCII collating sequence, and strcmp (3) to determine when one string is alphabetically greater than another). Note that you will be sorting char pointers so the strings they point to are sorted in the ASCII sequence. Depending on the value of an environment variable, FORMAT, you will then display the sorted contents of the environment in either name-value order or value-name order. When the FORMAT environment variable is set to REVERSE print out the environment in value-name order; if it is set to anything else OR not set, print out the environment in name-value order. 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: rcm$ export FORMAT=REVERSE And you unset an environment variable with the command line: rcm$ export FORMAT= Lastly, you display the value of an environment variable with the command line: rcm$ echo $FORMAT

SAMPLE OUTPUT: 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: rcm . . . REVERSE ORDER: value: /tmp/launch-DU1xCd/Render , name: Apple_PubSub_Socket_Render value: vi , name: EDITOR value: REVERSE , name: FORMAT value: /Users/rcm , name: HOME value: en_US.UTF-8 , name: LANG value: rcm , name: LOGNAME . . .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!