5.2.2.1 Receiving Command-line Options

When a server is booted, its first task is to read the server options specified in the configuration file up to the point that it receives an EOF indication. To do so, the server calls the getopt(3) UNIX function. The presence of a double dash (--) on the command line causes the getopt() function to return an EOF. The getopt function places the argv index of the next argument to be processed in the external variable optind. The predefined main() then calls tpsvrinit().

The following listing shows how the tpsvrinit() function is used to receive command-line options.

Listing Receiving Command-line Options in tpsvrinit( )

tpsvrinit(argc, argv)
int argc;
char **argv;
{
       int c;
       extern char *optarg;
       extern int optind;
       .
       .
       .
       while((c = getopt(argc, argv, "f:x:")) != EOF)
         switch(c){
         .
         .
         .
         }
      .
      .
      .
}

When main() calls tpsvrinit(), it picks up any arguments that follow the double dash (--) on the command line. In the example above, options f and x each takes an argument, as indicated by the colon. optarg points to the beginning of the option argument. The switch statement logic is omitted.