From ecf8b712186de5a5b05364e94ca2ef17c99dbaf6 Mon Sep 17 00:00:00 2001 From: Exynox Date: Wed, 23 Nov 2022 21:44:45 +0200 Subject: [PATCH] Fixed getopt routine --- game/src/main.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/game/src/main.cpp b/game/src/main.cpp index e93ff7a..3531fdf 100644 --- a/game/src/main.cpp +++ b/game/src/main.cpp @@ -509,29 +509,28 @@ int start(int argc, char **argv) std::string st_localeServiceName; bool bVerbose = false; - char ch; //_malloc_options = "A"; #if defined(__FreeBSD__) && defined(DEBUG_ALLOC) _malloc_message = WriteMallocMessage; #endif - while ((ch = getopt(argc, argv, "npverltI")) != -1) + int ch; + while ((ch = getopt(argc, argv, "n:p:verl:tI:")) != -1) { char* ep = NULL; switch (ch) { case 'I': // IP - strncpy(g_szPublicIP, argv[optind], sizeof(g_szPublicIP)); + strncpy(g_szPublicIP, optarg, sizeof(g_szPublicIP)); printf("IP %s\n", g_szPublicIP); - optind = 0; break; case 'p': // port - mother_port = strtol(argv[optind], &ep, 10); + mother_port = strtol(optarg, &ep, 10); if (mother_port <= 1024) { @@ -539,31 +538,21 @@ int start(int argc, char **argv) return 0; } - printf("port %d\n", mother_port); + printf("port %hu\n", mother_port); - optind = 0; break; case 'l': { - int l = strtol(argv[optind], &ep, 10); + int l = strtol(optarg, &ep, 10); log_set_level(l); - - optind = 0; - } + } break; // LOCALE_SERVICE - case 'n': - { - if (optind < argc) - { - st_localeServiceName = argv[optind]; - - optind = 0; - } - } + case 'n': + st_localeServiceName = optarg; break; // END_OF_LOCALE_SERVICE @@ -580,6 +569,18 @@ int start(int argc, char **argv) g_bTrafficProfileOn = true; break; // END_OF_TRAFFIC_PROFILER + + case '?': + if (strchr("Ipln", optopt)) + fprintf(stderr, "Option -%c requires an argument.\n", optopt); + else if (isprint (optopt)) + fprintf(stderr, "Unknown option `-%c'.\n", optopt); + else + fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); + default: + usage(); + return 1; + break; } }