/[osn-commons]/trunk/freehttpd/main.c
ViewVC logotype

Annotation of /trunk/freehttpd/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (hide annotations)
Sat Aug 10 18:33:37 2024 UTC (7 months, 3 weeks ago) by rakinar2
File MIME type: text/x-c
File size: 2745 byte(s)
feat: add freehttpd source files to the project

1 rakinar2 44 #include "freehttpd.h"
2     #include <errno.h>
3     #include <getopt.h>
4     #include <stdbool.h>
5     #include <stdio.h>
6     #include <stdlib.h>
7     #include <string.h>
8    
9     static struct option const long_options[] = {
10     { "help", no_argument, NULL, 'h' },
11     { "version", no_argument, NULL, 'v' },
12     { "config", required_argument, NULL, 'c' },
13     { NULL, 0, NULL, 0 }
14     };
15    
16     static const char *const short_options = "hvc:";
17    
18     struct config
19     {
20     const char *progname;
21     const char *config_file;
22     };
23    
24     static struct config config = { 0 };
25    
26     static void
27     usage (void)
28     {
29     printf ("Usage: %s [OPTIONS]\n", config.progname);
30     printf ("Options:\n");
31     printf (" -h, --help Print this help message\n");
32     printf (" -v, --version Print version information\n");
33     printf (" -c, --config Specify configuration file\n");
34     }
35    
36     static void
37     version (void)
38     {
39     printf ("freehttpd 0.0.0\n");
40     }
41    
42     static bool
43     validate_settings (void)
44     {
45     return true;
46     }
47    
48     static void
49     initialize (const char *progname)
50     {
51     config.progname = progname;
52     config.config_file = "freehttpd.conf";
53     }
54    
55     int
56     main (int argc, char **argv)
57     {
58     initialize (argv[0]);
59    
60     while (true)
61     {
62     int option_index = 0;
63     int c = getopt_long (argc, argv, short_options, long_options,
64     &option_index);
65    
66     if (c == -1)
67     break;
68    
69     switch (c)
70     {
71     case 'h':
72     usage ();
73     exit (EXIT_SUCCESS);
74    
75     case 'v':
76     version ();
77     exit (EXIT_SUCCESS);
78    
79     case 'c':
80     config.config_file = optarg;
81     break;
82    
83     case '?':
84     default:
85     exit (EXIT_FAILURE);
86     }
87     }
88    
89     if (!validate_settings ())
90     exit (EXIT_FAILURE);
91    
92     freehttpd_t *freehttpd = freehttpd_init ();
93     unsigned int port = 8080;
94     size_t uri_len = 1024;
95     ecode_t code = E_OK;
96    
97     code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_PORT, &port);
98    
99     if (code != E_OK)
100     goto error;
101    
102     code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_ADDR, NULL);
103    
104     if (code != E_OK)
105     goto error;
106    
107     code = freehttpd_setopt (freehttpd, FREEHTTPD_CONFIG_MAX_URI_LEN, &uri_len);
108    
109     if (code != E_OK)
110     goto error;
111    
112     code = freehttpd_start (freehttpd);
113    
114     if (code != E_OK)
115     {
116     error:
117     fprintf (stderr, "%s: failed to start: %d: %s\n", config.progname,
118     code, strerror (errno));
119     freehttpd_free (freehttpd);
120     exit (EXIT_FAILURE);
121     }
122    
123     freehttpd_free (freehttpd);
124     return 0;
125     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26