Extract cmdline parsing code into a function

This commit is contained in:
Mukund Sivaraman 2009-09-15 01:34:30 +05:30
parent f25b0e2872
commit 5ec5449194
2 changed files with 38 additions and 22 deletions

View File

@ -161,36 +161,32 @@ static int get_id (char *str)
return atoi (str); return atoi (str);
} }
int main (int argc, char **argv) /**
* process_cmdline:
* @argc: argc as passed to main()
* @argv: argv as passed to main()
*
* This function parses command line arguments.
**/
static void
process_cmdline (int argc, char **argv)
{ {
int optch; int opt;
unsigned int godaemon = TRUE; /* boolean */
struct passwd *thisuser = NULL;
struct group *thisgroup = NULL;
FILE *config_file;
/* Only allow u+rw bits. This may be required for some versions while ((opt = getopt (argc, argv, "c:vldh")) != EOF) {
* of glibc so that mkstemp() doesn't make us vulnerable. switch (opt) {
*/
umask (0177);
/* Default configuration file location */
config.config_file = DEFAULT_CONF_FILE;
/*
* Process the various options
*/
while ((optch = getopt (argc, argv, "c:vldh")) != EOF) {
switch (optch) {
case 'v': case 'v':
display_version (); display_version ();
exit (EX_OK); exit (EX_OK);
case 'l': case 'l':
display_license (); display_license ();
exit (EX_OK); exit (EX_OK);
case 'd': case 'd':
godaemon = FALSE; config.godaemon = FALSE;
break; break;
case 'c': case 'c':
config.config_file = safestrdup (optarg); config.config_file = safestrdup (optarg);
if (!config.config_file) { if (!config.config_file) {
@ -200,12 +196,31 @@ int main (int argc, char **argv)
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
break; break;
case 'h': case 'h':
default: default:
display_usage (); display_usage ();
exit (EX_OK); exit (EX_OK);
} }
} }
}
int
main (int argc, char **argv)
{
struct passwd *thisuser = NULL;
struct group *thisgroup = NULL;
FILE *config_file;
/* Only allow u+rw bits. This may be required for some versions
* of glibc so that mkstemp() doesn't make us vulnerable.
*/
umask (0177);
config.config_file = DEFAULT_CONF_FILE;
config.godaemon = TRUE;
process_cmdline (argc, argv);
log_message (LOG_INFO, "Initializing " PACKAGE " ..."); log_message (LOG_INFO, "Initializing " PACKAGE " ...");
@ -244,7 +259,7 @@ int main (int argc, char **argv)
} }
config.syslog = FALSE; /* disable syslog */ config.syslog = FALSE; /* disable syslog */
} else if (config.syslog) { } else if (config.syslog) {
if (godaemon == TRUE) if (config.godaemon == TRUE)
openlog ("tinyproxy", LOG_PID, LOG_DAEMON); openlog ("tinyproxy", LOG_PID, LOG_DAEMON);
else else
openlog ("tinyproxy", LOG_PID, LOG_USER); openlog ("tinyproxy", LOG_PID, LOG_USER);
@ -299,7 +314,7 @@ int main (int argc, char **argv)
anonymous_insert ("Content-Type"); anonymous_insert ("Content-Type");
} }
if (godaemon == TRUE) if (config.godaemon == TRUE)
makedaemon (); makedaemon ();
if (config.pidpath) { if (config.pidpath) {

View File

@ -50,6 +50,7 @@ struct config_s {
unsigned int syslog; /* boolean */ unsigned int syslog; /* boolean */
int port; int port;
char *stathost; char *stathost;
unsigned int godaemon; /* boolean */
unsigned int quit; /* boolean */ unsigned int quit; /* boolean */
char *user; char *user;
char *group; char *group;