ptg10805159
662 Advanced IPC Chapter 17
return;
}
}
log_quit("can’t find client entry for fd %d", fd);
}
Figure 17.27Functions to manipulateclientarray
The first timeclient_addis called, it callsclient_alloc,which in turn calls
mallocto allocate space for ten entries in the array.After these ten entries areall in
use, a later call toclient_addcausesreallocto allocate additional space. By
dynamically allocating space this way, we have not limited the size of theclientarray
at compile time to some value that we guessed and put into a header.These functions
call thelog_functions (Appendix B) if an error occurs, since we assume that the server
is a daemon.
Normally the server will run as a daemon, but we want to provide an option that
allows it to be run in the foreground, with diagnostic messages sent to the standard
error.This should make the server easier to test and debug, especially if we don’t have
permission to read the log file wherethe diagnostic messages arenormally written.
We’ll use a command-line option to control whether the server runs in the foreground
or as a daemon in the background.
It is important that all commands on a system follow the same conventions, because
this makes them easier to use. If someone is familiar with the way command-line
options areformed with one command, it would create morechances for mistakes if
another command followed different conventions.
This problem is sometimes visible when dealing with white space on the command
line. Some commands requirethat an option be separated from its argument by white
space, but other commands requirethe argument to follow immediately after its option,
without any intervening spaces.Without a consistent set of rules to follow,users either
have to memorize the syntax of all commands or resort to a trial-and-error process
when invoking them.
The Single UNIX Specification includes a set of conventions and guidelines that
promote consistent command-line syntax. They include such suggestions as ‘‘Restrict
each command-line option to a single alphanumeric character’’ and ‘‘All options should
be preceded by a−character.’’
Luckily,the getopt function exists to help command developers process
command-line options in a consistent manner.
#include <unistd.h>
int getopt(intargc,char * const argv[], const char *options);
extern int optind, opterr, optopt;
extern char *optarg;
Returns: the next option character,or
−1when all options have been processed