Debian Codegraph @

listings/chapter-11/main.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <assert.h>
8 #include <getopt.h>
9 #include <netdb.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15
16 #include "server.h"
17
18 /* Description of long options for getopt_long. */
19
20 static const struct option long_options[] = {
21 { "address", 1, NULL, 'a' },
22 { "help", 0, NULL, 'h' },
23 { "module-dir", 1, NULL, 'm' },
24 { "port", 1, NULL, 'p' },
25 { "verbose", 0, NULL, 'v' },
26 };
27
28 /* Description of short options for getopt_long. */
29
30 static const char* const short_options = "a:hm:p:v";
31
32 /* Usage summary text. */
33
34 static const char* const usage_template =
35 "Usage: %s [ options ]\n"
36 " -a, --address ADDR Bind to local address (by default, bind\n"
37 " to all local addresses).\n"
38 " -h, --help Print this information.\n"
39 " -m, --module-dir DIR Load modules from specified directory\n"
40 " (by default, use executable directory).\n"
41 " -p, --port PORT Bind to specified port.\n"
42 " -v, --verbose Print verbose messages.\n";
43
44 /* Print usage information and exit. If IS_ERROR is non-zero, write to
45 stderr and use an error exit code. Otherwise, write to stdout and
46 use a non-error termination code. Does not return. */
47
48 static void print_usage (int is_error)
49 {
50 fprintf (is_error ? stderr : stdout, usage_template, program_name);
51 exit (is_error ? 1 : 0);
52 }
53
54 int main (int argc, char* const argv[])
55 {
56 struct in_addr local_address;
57 uint16_t port;
58 int next_option;
59
60 /* Store the program name, which we'll use in error messages. */
61 program_name = argv[0];
62
63 /* Set defaults for options. Bind the server to all local addresses,
64 and assign an unused port automatically. */
65 local_address.s_addr = INADDR_ANY;
66 port = 0;
67 /* Don't print verbose messages. */
68 verbose = 0;
69 /* Load modules from the directory containing this executable. */
70 module_dir = get_self_executable_directory ();
71 assert (module_dir != NULL);
72
73 /* Parse options. */
74 do {
75 next_option =
76 getopt_long (argc, argv, short_options, long_options, NULL);
77 switch (next_option) {
78 case 'a':
79 /* User specified -a or --address. */
80 {
81 struct hostent* local_host_name;
82
83 /* Look up the host name the user specified. */
84 local_host_name = gethostbyname (optarg);
85 if (local_host_name == NULL || local_host_name->h_length == 0)
86 /* Could not resolve the name. */
87 error (optarg, "invalid host name");
88 else
89 /* Host name is OK, so use it. */
90 local_address.s_addr =
91 *((int*) (local_host_name->h_addr_list[0]));
92 }
93 break;
94
95 case 'h':
96 /* User specified -h or --help. */
97 print_usage (0);
98
99 case 'm':
100 /* User specified -m or --module-dir. */
101 {
102 struct stat dir_info;
103
104 /* Check that it exists. */
105 if (access (optarg, F_OK) != 0)
106 error (optarg, "module directory does not exist");
107 /* Check that it is accessible. */
108 if (access (optarg, R_OK | X_OK) != 0)
109 error (optarg, "module directory is not accessible");
110 /* Make sure that it is a directory. */
111 if (stat (optarg, &dir_info) != 0 || !S_ISDIR (dir_info.st_mode))
112 error (optarg, "not a directory");
113 /* It looks OK, so use it. */
114 module_dir = strdup (optarg);
115 }
116 break;
117
118 case 'p':
119 /* User specified -p or --port. */
120 {
121 long value;
122 char* end;
123
124 value = strtol (optarg, &end, 10);
125 if (*end != '\0')
126 /* The user specified non-digits in the port number. */
127 print_usage (1);
128 /* The port number needs to be converted to network (big endian)
129 byte order. */
130 port = (uint16_t) htons (value);
131 }
132 break;
133
134 case 'v':
135 /* User specified -v or --verbose. */
136 verbose = 1;
137 break;
138
139 case '?':
140 /* User specified an nrecognized option. */
141 print_usage (1);
142
143 case -1:
144 /* Done with options. */
145 break;
146
147 default:
148 abort ();
149 }
150 } while (next_option != -1);
151
152 /* This program takes no additional arguments. Issue an error if the
153 user specified any. */
154 if (optind != argc)
155 print_usage (1);
156
157 /* Print the module directory, if we're running verbose. */
158 if (verbose)
159 printf ("modules will be loaded from %s\n", module_dir);
160
161 /* Run the server. */
162 server_run (local_address, port);
163
164 return 0;
165 }