minissdpd.c: use getopt() to parse argc/argv

fixes #576
This commit is contained in:
Thomas Bernard 2021-11-05 00:29:59 +01:00
parent f91824c270
commit c914193a05
No known key found for this signature in database
GPG Key ID: DB511043A31ACAAF
1 changed files with 45 additions and 33 deletions

View File

@ -1,4 +1,4 @@
/* $Id: minissdpd.c,v 1.58 2020/06/06 20:20:47 nanard Exp $ */ /* $Id: minissdpd.c,v 1.61 2021/11/04 23:27:28 nanard Exp $ */
/* vim: tabstop=4 shiftwidth=4 noexpandtab /* vim: tabstop=4 shiftwidth=4 noexpandtab
* MiniUPnP project * MiniUPnP project
* (c) 2007-2021 Thomas Bernard * (c) 2007-2021 Thomas Bernard
@ -1263,52 +1263,64 @@ int main(int argc, char * * argv)
#endif /* ENABLE_IPV6 */ #endif /* ENABLE_IPV6 */
unsigned char ttl = 2; /* UDA says it should default to 2 */ unsigned char ttl = 2; /* UDA says it should default to 2 */
const char * searched_device = NULL; /* if not NULL, search/filter a specific device type */ const char * searched_device = NULL; /* if not NULL, search/filter a specific device type */
int opt;
LIST_INIT(&reqlisthead); LIST_INIT(&reqlisthead);
LIST_INIT(&servicelisthead); LIST_INIT(&servicelisthead);
LIST_INIT(&lan_addrs); LIST_INIT(&lan_addrs);
/* process command line */ /* process command line */
for(i=1; i<argc; i++) #define OPTSTRING "d6i:s:p:t:f:"
{ while ((opt = getopt(argc, argv, "di:s:t:f:"
if(0==strcmp(argv[i], "-d"))
debug_flag = 1;
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
else if(0==strcmp(argv[i], "-6")) "6"
#endif
#ifndef NO_BACKGROUND_NO_PIDFILE
"p:"
#endif
)) != -1)
{
switch(opt)
{
case 'd':
debug_flag = 1;
break;
#ifdef ENABLE_IPV6
case '6':
ipv6 = 1; ipv6 = 1;
break;
#endif /* ENABLE_IPV6 */ #endif /* ENABLE_IPV6 */
else { case 'i':
if((i + 1) >= argc) { lan_addr = malloc(sizeof(struct lan_addr_s));
fprintf(stderr, "option %s needs an argument.\n", argv[i]); if(lan_addr == NULL) {
fprintf(stderr, "malloc(%d) FAILED\n", (int)sizeof(struct lan_addr_s));
break; break;
} }
if(0==strcmp(argv[i], "-i")) { if(parselanaddr(lan_addr, optarg) != 0) {
lan_addr = malloc(sizeof(struct lan_addr_s)); fprintf(stderr, "can't parse \"%s\" as a valid "
if(lan_addr == NULL) {
fprintf(stderr, "malloc(%d) FAILED\n", (int)sizeof(struct lan_addr_s));
break;
}
if(parselanaddr(lan_addr, argv[++i]) != 0) {
fprintf(stderr, "can't parse \"%s\" as a valid "
#ifndef ENABLE_IPV6 #ifndef ENABLE_IPV6
"address or " "address or "
#endif #endif
"interface name\n", argv[i]); "interface name\n", optarg);
free(lan_addr); free(lan_addr);
} else { } else {
LIST_INSERT_HEAD(&lan_addrs, lan_addr, list); LIST_INSERT_HEAD(&lan_addrs, lan_addr, list);
} }
} else if(0==strcmp(argv[i], "-s")) break;
sockpath = argv[++i]; case 's':
sockpath = optarg;
break;
#ifndef NO_BACKGROUND_NO_PIDFILE #ifndef NO_BACKGROUND_NO_PIDFILE
else if(0==strcmp(argv[i], "-p")) case 'p':
pidfilename = argv[++i]; pidfilename = optarg;
break;
#endif #endif
else if(0==strcmp(argv[i], "-t")) case 't':
ttl = (unsigned char)atoi(argv[++i]); ttl = (unsigned char)atoi(optarg);
else if(0==strcmp(argv[i], "-f")) break;
searched_device = argv[++i]; case 'f':
else searched_device = optarg;
fprintf(stderr, "unknown commandline option %s.\n", argv[i]); break;
} }
} }
if(lan_addrs.lh_first == NULL) if(lan_addrs.lh_first == NULL)