![]() |
![]() |
Environment & Command Line ArgumentsBasic information can be passed to a C process via:
Command Line Parametersint main(int argc, char * argv[]) { int i; printf("argc = %d\n"); // print arg count for (i = 0; i < argc; i++) // print args printf("argv[%d]: %s\n",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 EnvironmentWhile 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\n",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 EnvironmentYou 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 ShellLast 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:
Change the current default directory to <directory>. If the <directory> argument is not present, report the current directory. This command should also change the PWD environment string. For this you will need to study the chdir (Glass p 431), getcwd and putenv functions. 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). Always use nice to
execute your test programs at lower priority to ensure they do not inconvenience
other users if they go 'haywire'. e.g.: |
![]() | ![]() | ![]() |
For use only by students and instructors using the supplementary material available with the text book: "Operating Systems - Internals and Design Principles", William Stallings, Prentice Hall, 5th Edition, 2004. Not to be printed out or copied by any other persons or used for any other purpose without written permission of the author(s).
©