Exercises
-
Assume a program accepts options -a, -b, and -c, and that -b requires an argument. Write the manual argument parsing code for this program, without using getopt() or getopt_long(). Accept -- to end option processing. Make sure that -ac works, as do -bYANKEES, -b YANKEES, and -abYANKEES. Test your program.
-
Implement getopt(). For the first version, don't worry about the case in which 'optstring[0] == ':'. You may also ignore opterr.
-
Add code for 'optstring[0] == ':' and opterr to your version of getopt().
-
Print and read the GNU getopt.h, getopt.c and getopt1.c files.
-
Write a program that declares both environ and envp and compares their values.
-
Parsing command line arguments and options is a wheel that many people can't refrain from reinventing. Besides getopt() and getopt_long(), you may wish to examine different argument-parsing packages, such as:
-
Extra credit: Why can't a C compiler completely ignore the register keyword? Hint: What operation cannot be applied to a register variable?