Utility

Team-FLY

2.12 Exercise: An env Utility

The env utility examines the environment and modifies it to execute another command. When called without arguments, the env command writes the current environment to standard output. The optional utility argument specifies the command to be executed under the modified environment. The optional -i argument means that env should ignore the environment inherited from the shell when executing utility . Without the -i option, env uses the [ name =value] arguments to modify rather than replace the current environment to execute utility . The env utility does not modify the environment of the shell that executes it.

  SYNOPSIS  env [-i] [name=value] ... [utility [argument ...]]  POSIX:Shell and Utilities  
Example 2.25

Calling env from the C shell on a machine running Sun Solaris produced the following output.

 HOME=/users/srobbins USER=srobbins LOGNAME=srobbins PATH=/bin:/usr/bin:/usr/ucb:/usr/bin/X11:/usr/local/bin MAIL=/var/mail/srobbins TZ=US/Central SSH2_CLIENT=129.115.12.131 41064 129.115.12.131 22 TERM=sun-cmd DISPLAY=sqr3:12.0 SSH2_SFTP_LOG_FACILITY=-1 PWD=/users/srobbins 

Write a program called doenv that behaves in the same way as the env utility when executing another program.

  1. When called with no arguments, the doenv utility calls the getenv function and outputs the current environment to standard output.

  2. When doenv is called with the optional -i argument, the entire environment is replaced by the name=value pairs. Otherwise, the pairs modify or add to the current environment.

  3. If the utility argument is given, use system to execute utility after the environment has been appropriately changed. Otherwise, print the changed environment to standard output, one entry per line.

  4. One way to change the current environment in a program is to overwrite the value of the environ external variable. If you are completely replacing the old environment ( -i option), count the number of name=value pairs, allocate enough space for the argument array (don't forget the extra NULL entry), copy the pointers from argv into the array, and set environ .

  5. If you are modifying the current environment by overwriting environ , allocate enough space to hold the old entries and any new entries to be added. Copy the pointers from the old environ into the new one. For each name=value pair, determine whether the name is already in the old environment. If name appears, just replace the pointer. Otherwise, add the new entry to the array.

  6. Note that it is not safe to just append new entries to the old environ , since you cannot expand the old environ array with realloc . If all name=value pairs correspond to entries already in the environment, just replace the corresponding pointers in environ .

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net