Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 17.6 An Open Server,Version 2 663


Theargcandargvarguments arethe same ones passed to themainfunction of the
program. Theoptionsargument is a string containing the option characters supported
by the command. If an option character is followed by a colon, then the option takes an
argument. Otherwise, the option exists by itself. For example, if the usage statement
for a command was
command [-i] [-u username] [-z] filename

we would pass"iu:z"as theoptionsstring togetopt.
Thegetoptfunction is normally used in a loop that terminates when getopt
returns−1. During each iteration of the loop,getoptwill return the next option
processed. It is up to the application to sort out any conflict in options, however;
getoptsimply parses the options and enforces a standardformat.
When it encounters an invalid option,getoptreturns a question mark instead of
the character.If an option’s argument is missing,getoptwill also return a question
mark, but if the first character in the options string is a colon,getoptreturns a colon
instead. The special pattern--will cause getoptto stop processing options and
return−1. This allows users to provide command arguments that start with a minus
sign but aren’t options. For example, if you have a file named-bar,you can’t remove
it by typing
rm -bar

becausermwill try to interpret-baras options. The way to remove the file is to type
rm -- -bar

Thegetoptfunction supports four external variables.

optarg If an option takes an argument,getoptsetsoptargto point to the
option’s argument string when an option is processed.
opterr If an option error is encountered,getoptwill print an error message by
default. Todisable this behavior,applications can setopterrto 0.
optind The index in theargvarray of the next string to be processed. It starts at
1and is incremented for each argument processed bygetopt.
optopt If an error is encountered during options processing,getoptwill set
optoptto point to the option string that caused the error.

The open server’s main function (Figure17.28) defines the global variables,
processes the command-line options, and calls the functionloop.If we invoke the
server with the-doption, the server runs interactively instead of as a daemon. This
option is used when testing the server.
#include "opend.h"
#include <syslog.h>

int debug, oflag, client_size, log_to_stderr;
char errmsg[MAXLINE];
char *pathname;
Free download pdf