Question: 2. Write a C program named assignment2_yourname.c and submit the code in blackboard. The program takes as argument a new environment variable in the form
2. Write a C program named assignment2_yourname.c and submit the code in blackboard.
The program takes as argument a new environment variable in the form var_name=value (i.e., MYENV=myenv) and has to print out the following information:
Its PID and its parent process's PID (5 points)
e.g.,
My PID is 2554
My parent process PID is 2553
#######################
Its original environment before setting the new variable (5 points)
e.g.,
My original environment is:
TERM=xterm-256color
[.]
#######################
The updated environment list after setting the new environment variable (10 points)
e.g.,
My updated environment is:
TERM=xterm-256color
MYENV=myenv
[.]
#######################
The program has to handle the different errors
Usage error check that the program has one argument of the form var=name and give a usage error if not (5 points)
Check that the set of the environment variable is successful and return errExit if not succesful (5 points)
NOTE: You can build on /tlpi-dist/proc/modify_env.c and you can compile your new program the same way.
Modify_env.c
#define _GNU_SOURCE /* Get various declarations from*/ #include #include "tlpi_hdr.h" extern char **environ; int main(int argc, char *argv[]) { int j; char **ep; clearenv(); /* Erase entire environment */ + /* Add any definitions specified on command line to environment */ + for (j = 1; j < argc; j++) if (putenv(argv[j]) != 0) errExit("putenv: %s", argv[j]); + /* Add a definition for GREET if one does not already exist */ + if (setenv("GREET", "Hello world", 0) == -1) errExit("setenv"); + /* Remove any existing definition of BYE */ + unsetenv("BYE"); + /* Display current environment */ + for (ep = environ; *ep != NULL; ep++) puts(*ep); exit(EXIT_SUCCESS); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
