Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 7 Implement the Linux env utility in Go. The env utility examines the e

ID: 3742689 • Letter: E

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 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 system’s 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

Explanation / Answer

/*****************************************************

Compile it using go build env.go

And run ./env

Then run ./env ABC=DEF env

You can see it work

Do give a feedback because it would really be heloful

**************************************************/

/************env.go********************/

package main

//Importing packages

import (

"bytes"

"os"

"os/exec"

"strings"

"fmt"

)

func main() {

// Fetch all command line arguments except the program name

args := os.Args[1:]

// Iterate over the command line arguments

for i := 0; i < len(args); i++ {

if args[i] == "-i" {

//Clear the environment variables if -i flag is found

os.Clearenv()

} else {

//If not a flag then get a key=value pair and split it around =

pair := strings.Split(args[i], "=")

// If there is only one value, it means we have a command

// If not then set the environment variable

if(len(pair) == 2) {

os.Setenv(pair[0], pair[1])

} else {

// Else run the command with the rest of the array as arguments

Excecute(args[i], args[i + 1:])

break;

}

}

}

if len(args) == 0 {

// If there were no arguments then print the environment variables

Printenv()

}

}

func Printenv() {

// Get the list of environment variables and iterate over it

// Split it around - and print it

for _, e := range os.Environ() {

pair := strings.Split(e, "-")

fmt.Println(pair[0])

}

}

func Excecute(s string, args []string) {

// Set the command and the argument list

cmd := exec.Command(s, args...)

var out bytes.Buffer

var stderr bytes.Buffer

cmd.Stdout = &out

cmd.Stderr = &stderr

//Sets the STDOUT and STDERR buffers and runs the command

cmd.Run()

// If the STDOUT buffer is not empty, print everything

if s := out.String(); len(s) != 0 {

fmt.Printf("%v", s)

}

// If the STDERR buffer is not empty, print everything

if s := stderr.String(); len(s) != 0 {

fmt.Printf("%v", s)

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote