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
o argv - an array of character pointers (strings) 2nd argument to main the command line parameters include the entire parsed command line (including the program name).
o argc - number of command line parameters
• Environment
o environ - an array of character pointers (strings) in the format
"name=value"
the list is terminated with a NULL pointer.
Command Line Parameters
int 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 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\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 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 . If the argument is not present, report the current directory. This command should also change the PWDenvironment 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 (orstrduped).
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.:
>nice a.out