Группа :: Система/Серверы
Пакет: pdnsd
Главная Изменения Спек Патчи Загрузить Bugs and FR
Патч: pdnsd-1.1.7a-alt-getopt.patch
--- pdnsd-1.1.7a-/src/main.c 2003-08-23 17:42:59 +0400
+++ pdnsd-1.1.7a/src/main.c 2003-08-23 17:58:32 +0400
@@ -25,6 +25,8 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <syslog.h>
@@ -186,6 +188,29 @@
#ifndef O_NOFOLLOW
struct stat so, sn;
#endif
+ static struct option options[] = {
+ { "help", 0, 0, 'h' },
+ { "version", 0, 0, 'V' },
+ { "config-file", 1, 0, 'c' },
+ { "status", 0, 0, 's' },
+ { "daemon", 0, 0, 'd' },
+ { "debug", 0, 0, 'g' },
+ { "tcp", 0, 0, 't' },
+ { "verbose", 0, 0, 'v' },
+ { "pidfile", 0, 0, 'p' },
+ { "pid-file", 0, 0, 'p' },
+ { "method", 0, 0, 'm' },
+ { "pdnsd-user", 0, 0, 'u' },
+ { "ipv4", 0, 0, '4' },
+ { "ipv6", 0, 0, '6' },
+ /* "noxxx" uppercase */
+ { "nostatus", 0, 0, 'S' },
+ { "nodaemon", 0, 0, 'D' },
+ { "notcp", 0, 0, 'T' },
+ { "nodebug", 0, 0, 'G' },
+ { 0, 0, 0, 0 },
+ };
+ static const char optstr[] = "hVc:sdgtv:p:m:u46SDTG";
main_thread=pthread_self();
@@ -193,82 +218,84 @@
yydebug=1;
#endif
- /* We parse the command line two times, because the command-line options shall override the ones
- * given in the config file */
- for (i=1;i<argc;i++) {
- if (strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0) {
+ /* We parse the command line two times, because the command-line
+ * options shall override the ones given in the config file */
+ while (1) {
+ int c = getopt_long(argc, argv, optstr, options, 0);
+ if (c < 0)
+ break;
+ switch (c) {
+ case 'h':
print_info();
print_help();
exit(1);
- } else if (strcmp(argv[i],"-V")==0 || strcmp(argv[i],"--version")==0) {
+ case 'V':
print_info();
exit(1);
- } else if (strcmp(argv[i],"-c")==0 || strcmp(argv[i],"--config-file")==0) {
- if (i<argc-1) {
- i++;
- conf_file=argv[i];
- } else {
- fprintf(stderr,"Error: file name expected after -c option.\n");
- exit(1);
- }
+ case 'c':
+ conf_file = optarg;
+ break;
+ case '?':
+ exit(1);
+ default:
+ break;
}
}
init_cache();
read_config_file(conf_file);
- for (i=1;i<argc;i++) {
- if (strcmp(argv[i],"-s")==0 || strcmp(argv[i],"--status")==0) {
- stat_pipe=1;
- } else if (strcmp(argv[i],"--nostatus")==0) {
- stat_pipe=0;
- } else if (strcmp(argv[i],"-d")==0 || strcmp(argv[i],"--daemon")==0) {
- daemon_p=1;
- } else if (strcmp(argv[i],"--nodaemon")==0) {
- daemon_p=0;
- } else if (strcmp(argv[i],"-t")==0 || strcmp(argv[i],"--tcp")==0) {
- notcp=0;
- } else if (strcmp(argv[i],"--notcp")==0) {
- notcp=1;
- } else if (strcmp(argv[i],"-p")==0) {
- if (i<argc-1) {
- i++;
- if (strlen(argv[i]) >= sizeof(pidfile)) {
- fprintf(stderr,"Error: pidfile name too long.\n");
- exit(1);
- }
- strncpy(pidfile,argv[i],sizeof(pidfile));
- pidfile[sizeof(pidfile)-1]='\0';
- } else {
- fprintf(stderr,"Error: file name expected after -p option.\n");
- exit(1);
- }
- } else if (strncmp(argv[i],"-v",2)==0) {
- if (strlen(argv[i])!=3 || !isdigit(argv[i][2])) {
- fprintf(stderr,"Error: one digit expected after -v option (like -v2).\n");
+ /* 1003.2 says this must be 1 before any call. */
+ optind = 1;
+
+ while (1) {
+ int c = getopt_long(argc, argv, optstr, options, 0);
+ if (c < 0)
+ break;
+ switch (c) {
+ case 's':
+ stat_pipe = 1; break;
+ case 'S':
+ stat_pipe = 0; break;
+ case 'd':
+ daemon_p = 1; break;
+ case 'D':
+ daemon_p = 0; break;
+ case 't':
+ notcp = 0; break;
+ case 'T':
+ notcp = 1; break;
+ case 'p':
+ if (strlen(optarg) >= sizeof(pidfile)) {
+ fprintf(stderr,"Error: pidfile name too long.\n");
exit(1);
}
- verbosity=argv[i][2]-'0';
- } else if (strncmp(argv[i],"-m",2)==0) {
- if (strlen(argv[i])!=4) {
+ strncpy(pidfile, optarg, sizeof(pidfile));
+ pidfile[sizeof(pidfile)-1] = '\0';
+ break;
+ case 'v':
+ verbosity = optarg[2] - '0';
+ break;
+ case 'm':
+ if (strlen(optarg)!=4) {
fprintf(stderr,"Error: uo, to or tu expected after the -m option (like -muo).\n");
exit(1);
}
- if (strcmp(&argv[i][2],"uo")==0) {
+ if (strcmp(optarg,"uo")==0) {
#ifdef NO_UDP_QUERIES
fprintf(stderr,"Error: pdnsd was compiled without UDP support.\n");
exit(1);
#else
query_method=UDP_ONLY;
#endif
- } else if (strcmp(&argv[i][2],"to")==0) {
+ } else if (strcmp(optarg,"to")==0) {
#ifdef NO_TCP_QUERIES
fprintf(stderr,"Error: pdnsd was compiled without TCP support.\n");
exit(1);
#else
query_method=TCP_ONLY;
#endif
- } else if (strcmp(&argv[i][2],"tu")==0) {
+ } else if (strcmp(optarg,"tu")==0) {
#if defined(NO_UDP_QUERIES) || defined(NO_TCP_QUERIES)
fprintf(stderr,"Error: pdnsd was not compiled with UDP and TCP support.\n");
exit(1);
@@ -279,7 +306,8 @@
fprintf(stderr,"Error: uo, to or tu expected after the -m option (like -muo).\n");
exit(1);
}
- } else if (strcmp(argv[i],"-4")==0) {
+ break;
+ case '4':
#ifdef ENABLE_IPV4
run_ipv4=1;
# ifdef ENABLE_IPV6
@@ -289,7 +317,8 @@
fprintf(stderr,"Error: -4: pdnsd was compiled without IPv4 support.\n");
exit(1);
#endif
- } else if (strcmp(argv[i],"-6")==0) {
+ break;
+ case '6':
#ifdef ENABLE_IPV6
run_ipv6=1;
# ifdef ENABLE_IPV4
@@ -299,15 +328,12 @@
fprintf(stderr,"Error: -6: pdnsd was compiled without IPv6 support.\n");
exit(1);
#endif
- } else if (strcmp(argv[i],"-g")==0 || strcmp(argv[i],"--debug")==0) {
-#if DEBUG>0
- debug_p=1;
-#else
- fprintf(stderr,"pdnsd was compiled without debugging support. -g has no effect.\n");
-#endif
- } else if (strcmp(argv[i],"--nodebug")==0) {
- debug_p=0;
- } else if (strcmp(argv[i],"--pdnsd-user")==0) {
+ break;
+ case 'g':
+ debug_p = 1; break;
+ case 'G':
+ debug_p = 0; break;
+ case 'u':
if (global.run_as[0]) {
printf("%s\n",global.run_as);
} else {
@@ -318,14 +344,14 @@
}
}
exit(0);
- } else if (strcmp(argv[i],"-c")==0 || strcmp(argv[i],"--config-file")==0) {
+ case 'c':
/* at this point, it is already checked that a file name arg follows. */
- i++;
- } else {
- fprintf(stderr,"Error: unknown option: %s\n",argv[i]);
+ break;
+ case '?':
+ default:
exit(1);
}
- }
+ } /* while (1) */
if (!(global.run_as[0] && global.strict_suid)) {
struct passwd *pws=getpwuid(getuid());