Question
Operating Systems - Projects and Exercises Exercise 3 - Enhancing Your Shell Environment & Command Line Arguments Basic information can be passed to a C
Operating Systems - Projects and Exercises
Exercise 3 - Enhancing Your Shell
Environment & Command Line Arguments Basic information can be passed to a C process via:
Command Line Parameters int main(int argc, char * argv[]) { int i; printf("argc = %d "); // print arg count for (i = 0; i < argc; i++) // print args printf("argv[%d]: %s ",i, argv[i]); exit(0); } invoked through the command line: echoarg arg1 TEST fooo will result in the following output: argc = 4 argv[0]: echoarg argv[1]: arg1 argv[2]: TEST argv[3]: fooo Accessing The Environment While some implementations of C provide a 3rd argument to main (char * envp[] ) with which to pass the environment, ANSI C only defines two arguments to main so the preferred way of passing the environment is by a built-in pointer (POSIX): extern char **environ; // NULL terminated array of char * main(int argc, char *argv[]) { int i; for (i = 0; environ[i] != NULL; i++) printf("%s ",environ[i]); exit(0); } example output: GROUP=staff HOME=/usr/user LOGNAME=user PATH=/bin:/usr/bin:usr/user/bin PWD=/usr/user/work SHELL=/bin/tcsh USER=user Updating The Environment You can use the functions getenv, putenv, setenv to access and/or change individual environment variables. Internal storage is the exclusive property of the process and may be modified freely, but there is not necessarily room for new variables or larger values. If the pointer environ is made to point to a new environment space it will be passed on to any programs subsequently envoked. If copying the environment, you should find out how many entries are in the environ table and malloc enough space to hold that table and any extra string pointers you may want to put in it. Remember, the environment is an array of pointers. The strings pointed to by those pointers need to exist in their own malloced memory space. Extending Your Shell Last week you wrote a simple shell that looped reading a line from standard input and checked the first word of the input line. This week you should try adding the extra functionality listed below. Internal Commands/Aliases: Add the capability to change the current directory and set and change environment strings: cd Change the current default directory to While you are at it you might as well put the name of the current working directory in the shell prompt! Be careful using putenv - the string you provide becomes part of the environment and should not be changed (or freed!). i.e. it should be made static - global or malloced (or strduped). You should now be looking at the specifications for the first project to see what extensions you will need to add to the code you have generated in these exercises. Code should be in 'straight' C using the compiler of your choice (cc or gcc). |
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