Question
Exercise 7 Implement the Linux env utility in Go. The env utility examines the environment and modifies it to ex- ecute another command. When called
Exercise 7 Implement the Linux env utility in Go.
The env utility examines the environment and modifies it to ex- ecute 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 exe- cuted 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 cur- rent environment to execute utility. The env command does not modify the environment of the shell which executes it. [See theenv manpage for more information.]
SYNOPSIS env [-i] [name=value] ... [utility [argument ...]] POSIX: Shell and Utilities
Requirements:
Write a program which behaves in the same way as the env utility when executing another program.
a) [This exercise is asking you to implement env from scratch, not just call the systems installed version of env from a C program.]
b) When called with no arguments, the env utility calls thegetenv function and outputs the current environment to stan- dard output.
c) When env 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.
d) If the utility argument is given, use system to exe- cute utility after the environment has been appropriately changed. Otherwise, print the changed environment to standard output, one entry per line. Check the return value of system to handle any errors.
e) 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 (do not forget the extra NULL entry), copy the pointers for argv into the array, and set environ.
f) If you are modifying the current environment by overwritingenviron, allocate enough space to hold the old environ into the new one. For each name=value pair, determine whether the name is already in the old environment. If the name appears,
just replace the pointer. Otherwise, add the new entry to the array.
g) Note that it not safe to just append new entries to the oldenviron, since you cannot expand the old environ array withrealloc. If all the name=value pairs correspond to entries al- ready in the environment, just replace the corresponding point- ers in environ.
h) [Return a different integer as an exit status for an invalid option as that returned for an invalid utility. Mimic the behavior of envon a Linux system.]
i) [Your program must be written in Go and compile without errors or warnings using gcc on a Linux system.]
Use the env command on the system as a reference executable for this exercise. Explore and explain the output of the following command lines:
1 env
2 env i
3 env a=42
4 env a=42 b="forty-two"
5 env ls
6 env ls lh t
7 env ia=42
8 env i a=42 b="forty-two"
9 env i env
10 env i env lslht
11 env a=42 env
12 env a=42 b="forty-two" env
13 env a=42 env ls lh t
14 env a=42 b="forty-two" env ls lh t
15 env not_there
16 env HOST="wrong_hostname" env
17 env z
18 envils
19 env i env ls
20 env i A=1 B=2 env
21 env A=1 B=2 env env env env
22 env i A=1 B=2 env env env env ls
23 env A=1 B=2 env env i env env ls
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started