From 8df2a73594fbcc812f4ea22a72ab7a79bfb63dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Mon, 26 Jul 2021 17:22:36 +0100 Subject: [PATCH] Use the musl in-tree getopt_long() everywhere Besides the obvious limitation of being unavailable on Windows, the standard is vague about getopt() and getopt_long() has many non-portable pitfalls and buggy implementations, that increase the maintainance cost a lot. Also the GNU libc code currently in the tree is not suited for embedding and is unmaintainable. Own maintainership for getopt_long() and use the musl implementation everywhere. This way we don't need to worry if optreset is available, or if the $OPERATING_SYSTEM version behaves in subtly different ways. The API is under the Wireshark namespace to avoid conflicts with system headers. Side-note, the Mingw-w64 9.0 getopt_long() implementation is buggy with opterr and known to crash. In my experience it's a headache to use the embedded getopt implementation if the system provides one. --- ConfigureChecks.cmake | 23 - capinfos.c | 19 +- capture_opts.h | 2 +- captype.c | 15 +- cmakeconfig.h.in | 6 - debian/libwsutil0.symbols | 9 + dumpcap.c | 41 +- editcap.c | 101 ++- epan/ex-opt.c | 4 +- epan/ex-opt.h | 2 +- epan/stats_tree_priv.h | 4 +- extcap/androiddump.c | 48 +- extcap/ciscodump.c | 44 +- extcap/dpauxmon.c | 20 +- extcap/etl.c | 22 +- extcap/etwdump.c | 12 +- extcap/extcap-base.c | 12 - extcap/extcap-base.h | 13 +- extcap/randpktdump.c | 22 +- extcap/sdjournal.c | 18 +- extcap/sshdump.c | 42 +- extcap/udpdump.c | 22 +- mergecap.c | 43 +- randpkt.c | 25 +- rawshark.c | 65 +- reordercap.c | 21 +- sharkd_daemon.c | 31 +- text2pcap.c | 107 ++- tfshark.c | 95 +-- tools/pre-commit-ignore.conf | 1 - tshark.c | 155 ++-- ui/cli/tap-exportobject.h | 2 +- ui/commandline.c | 99 +-- ui/qt/main.cpp | 13 - wsutil/CMakeLists.txt | 7 +- wsutil/getopt_long.c | 1310 ---------------------------------- wsutil/getopt_long.h | 122 ---- wsutil/ws_getopt.c | 108 +-- wsutil/ws_getopt.h | 45 +- wsutil/ws_getopt_long.c | 104 ++- wsutil/wsgetopt.h | 173 ----- 41 files changed, 631 insertions(+), 2396 deletions(-) delete mode 100644 wsutil/getopt_long.c delete mode 100644 wsutil/getopt_long.h delete mode 100644 wsutil/wsgetopt.h diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index a118c77859..58f3e5f6dd 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -93,29 +93,6 @@ if (NOT WIN32) check_function_exists("clock_gettime" HAVE_CLOCK_GETTIME) endif (NOT WIN32) -check_function_exists("getopt_long" HAVE_GETOPT_LONG) -if(HAVE_GETOPT_LONG) - check_include_file("getopt.h" HAVE_GETOPT_H) - # - # The OS has getopt_long(), so it might have optreset. - # Do we have it? - # - if(HAVE_GETOPT_H) - check_symbol_exists("optreset" "getopt.h" HAVE_OPTRESET) - else() - check_symbol_exists("optreset" HAVE_OPTRESET) - endif() -else() - # - # The OS doesn't have getopt_long(), so we're using the GNU libc - # version that we have in wsutil. It doesn't have optreset, so we - # don't need to check for it. - # - # However, it uses alloca(), so we may need to include alloca.h; - # check for it. - # - check_include_file("alloca.h" HAVE_ALLOCA_H) -endif() check_function_exists("getifaddrs" HAVE_GETIFADDRS) check_function_exists("issetugid" HAVE_ISSETUGID) check_function_exists("setresgid" HAVE_SETRESGID) diff --git a/capinfos.c b/capinfos.c index c43dddd83a..43cbcf6480 100644 --- a/capinfos.c +++ b/capinfos.c @@ -51,18 +51,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -1620,7 +1609,7 @@ main(int argc, char *argv[]) wtap_init(TRUE); /* Process the options */ - while ((opt = getopt_long(argc, argv, "abcdehiklmnoqrstuvxyzABCDEFHIKLMNQRST", long_options, NULL)) !=-1) { + while ((opt = ws_getopt_long(argc, argv, "abcdehiklmnoqrstuvxyzABCDEFHIKLMNQRST", long_options, NULL)) !=-1) { switch (opt) { @@ -1803,7 +1792,7 @@ main(int argc, char *argv[]) } } - if ((argc - optind) < 1) { + if ((argc - ws_optind) < 1) { print_usage(stderr); overall_error_status = INVALID_OPTION; goto exit; @@ -1825,7 +1814,7 @@ main(int argc, char *argv[]) overall_error_status = 0; - for (opt = optind; opt < argc; opt++) { + for (opt = ws_optind; opt < argc; opt++) { (void) g_strlcpy(file_sha256, "", HASH_STR_SIZE); (void) g_strlcpy(file_rmd160, "", HASH_STR_SIZE); diff --git a/capture_opts.h b/capture_opts.h index 6573b34aaf..84379403f3 100644 --- a/capture_opts.h +++ b/capture_opts.h @@ -332,7 +332,7 @@ capture_opts_cleanup(capture_options *capture_opts); /* set a command line option value */ extern int -capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg); +capture_opts_add_opt(capture_options *capture_opts, int opt, const char *ws_optarg); /* log content of capture_opts */ extern void diff --git a/captype.c b/captype.c index c8448d4b91..150ee87a01 100644 --- a/captype.c +++ b/captype.c @@ -20,18 +20,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -157,7 +146,7 @@ main(int argc, char *argv[]) wtap_init(TRUE); /* Process the options */ - while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) { + while ((opt = ws_getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) { switch (opt) { diff --git a/cmakeconfig.h.in b/cmakeconfig.h.in index ea47646a08..6c6ca1360c 100644 --- a/cmakeconfig.h.in +++ b/cmakeconfig.h.in @@ -67,9 +67,6 @@ /* Define if you have the 'getexecname' function. */ #cmakedefine HAVE_GETEXECNAME 1 -/* Define to 1 if you have the getopt_long function. */ -#cmakedefine HAVE_GETOPT_LONG 1 - /* Define to 1 if you have the header file. */ #cmakedefine HAVE_GRP_H 1 @@ -217,9 +214,6 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_PWD_H 1 -/* Define to 1 if you have the optreset variable */ -#cmakedefine HAVE_OPTRESET 1 - /* Define to 1 if you want to playing SBC by standalone BlueZ SBC library */ #cmakedefine HAVE_SBC 1 diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols index 45a00abe58..c23f17235e 100644 --- a/debian/libwsutil0.symbols +++ b/debian/libwsutil0.symbols @@ -351,6 +351,9 @@ libwsutil.so.0 libwsutil0 #MINVER# ws_buffer_remove_start@Base 1.99.0 ws_cleanup_sockets@Base 3.1.0 ws_cmac_buffer@Base 3.1.0 + ws_getopt@Base 3.5.1 + ws_getopt_long@Base 3.5.1 + ws_getopt_long_only@Base 3.5.1 ws_buffer_cleanup@Base 2.3.0 ws_hexstrtou16@Base 2.3.0 ws_hexstrtou32@Base 2.3.0 @@ -387,6 +390,12 @@ libwsutil.so.0 libwsutil0 #MINVER# ws_logv_full@Base 3.5.0 ws_mempbrk_compile@Base 1.99.4 ws_mempbrk_exec@Base 1.99.4 + ws_optarg@Base 3.5.1 + ws_opterr@Base 3.5.1 + ws_optind@Base 3.5.1 + ws_optopt@Base 3.5.1 + ws_optpos@Base 3.5.1 + ws_optreset@Base 3.5.1 ws_pipe_close@Base 2.6.5 ws_pipe_data_available@Base 2.5.0 ws_pipe_init@Base 2.5.1 diff --git a/dumpcap.c b/dumpcap.c index 682156232f..d1093f9f70 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -22,18 +22,7 @@ #include #endif -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #if defined(__APPLE__) && defined(__LP64__) #include @@ -5126,7 +5115,7 @@ main(int argc, char *argv[]) global_capture_opts.capture_child = capture_child; /* Now get our args */ - while ((opt = getopt_long(argc, argv, OPTSTRING, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, OPTSTRING, long_options, NULL)) != -1) { switch (opt) { case 'h': /* Print help and exit */ show_help_header("Capture network packets and dump them into a pcapng or pcap file."); @@ -5166,7 +5155,7 @@ main(int argc, char *argv[]) case 'I': /* Monitor mode */ #endif case LONGOPT_COMPRESS_TYPE: /* compress type */ - status = capture_opts_add_opt(&global_capture_opts, opt, optarg); + status = capture_opts_add_opt(&global_capture_opts, opt, ws_optarg); if (status != 0) { exit_main(status); } @@ -5177,7 +5166,7 @@ main(int argc, char *argv[]) interface_options *interface_opts; interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, global_capture_opts.ifaces->len - 1); - interface_opts->ifname = g_strdup(optarg); + interface_opts->ifname = g_strdup(ws_optarg); } else { cmdarg_err("--ifname must be specified after a -i option"); exit_main(1); @@ -5188,7 +5177,7 @@ main(int argc, char *argv[]) interface_options *interface_opts; interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, global_capture_opts.ifaces->len - 1); - interface_opts->descr = g_strdup(optarg); + interface_opts->descr = g_strdup(ws_optarg); } else { cmdarg_err("--ifdescr must be specified after a -i option"); exit_main(1); @@ -5198,7 +5187,7 @@ main(int argc, char *argv[]) if (capture_comments == NULL) { capture_comments = g_ptr_array_new_with_free_func(g_free); } - g_ptr_array_add(capture_comments, g_strdup(optarg)); + g_ptr_array_add(capture_comments, g_strdup(ws_optarg)); break; case 'Z': capture_child = TRUE; @@ -5206,11 +5195,11 @@ main(int argc, char *argv[]) /* set output pipe to binary mode, to avoid ugly text conversions */ _setmode(2, O_BINARY); /* - * optarg = the control ID, aka the PPID, currently used for the + * ws_optarg = the control ID, aka the PPID, currently used for the * signal pipe name. */ - if (strcmp(optarg, SIGNAL_PIPE_CTRL_ID_NONE) != 0) { - sig_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, optarg); + if (strcmp(ws_optarg, SIGNAL_PIPE_CTRL_ID_NONE) != 0) { + sig_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, ws_optarg); sig_pipe_handle = CreateFile(utf_8to16(sig_pipe_name), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); @@ -5260,7 +5249,7 @@ main(int argc, char *argv[]) case 'k': /* Set wireless channel */ if (!set_chan) { set_chan = TRUE; - set_chan_arg = optarg; + set_chan_arg = ws_optarg; run_once_args++; } else { cmdarg_err("Only one -k flag may be specified"); @@ -5271,13 +5260,13 @@ main(int argc, char *argv[]) machine_readable = TRUE; break; case 'C': - pcap_queue_byte_limit = get_positive_int(optarg, "byte_limit"); + pcap_queue_byte_limit = get_positive_int(ws_optarg, "byte_limit"); break; case 'N': - pcap_queue_packet_limit = get_positive_int(optarg, "packet_limit"); + pcap_queue_packet_limit = get_positive_int(ws_optarg, "packet_limit"); break; default: - cmdarg_err("Invalid Option: %s", argv[optind-1]); + cmdarg_err("Invalid Option: %s", argv[ws_optind-1]); /* FALLTHROUGH */ case '?': /* Bad flag - print usage message */ arg_error = TRUE; @@ -5285,8 +5274,8 @@ main(int argc, char *argv[]) } } if (!arg_error) { - argc -= optind; - argv += optind; + argc -= ws_optind; + argv += ws_optind; if (argc >= 1) { /* user specified file name as regular command-line argument */ /* XXX - use it as the capture file name (or something else)? */ diff --git a/editcap.c b/editcap.c index f775359194..ac57d795b6 100644 --- a/editcap.c +++ b/editcap.c @@ -40,18 +40,7 @@ #include #endif -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include #include @@ -1240,7 +1229,7 @@ main(int argc, char *argv[]) wtap_init(TRUE); /* Process the options */ - while ((opt = getopt_long(argc, argv, ":a:A:B:c:C:dD:E:F:hi:I:Lo:rs:S:t:T:vVw:", long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, ":a:A:B:c:C:dD:E:F:hi:I:Lo:rs:S:t:T:vVw:", long_options, NULL)) != -1) { switch (opt) { case LONGOPT_NO_VLAN: { @@ -1256,9 +1245,9 @@ main(int argc, char *argv[]) case LONGOPT_SEED: { - if (sscanf(optarg, "%u", &seed) != 1) { + if (sscanf(ws_optarg, "%u", &seed) != 1) { fprintf(stderr, "editcap: \"%s\" isn't a valid seed\n\n", - optarg); + ws_optarg); ret = INVALID_OPTION; goto clean_exit; } @@ -1270,11 +1259,11 @@ main(int argc, char *argv[]) { guint32 secrets_type_id = 0; const char *secrets_filename = NULL; - if (strcmp("help", optarg) == 0) { + if (strcmp("help", ws_optarg) == 0) { list_secrets_types(stdout); goto clean_exit; } - gchar **splitted = g_strsplit(optarg, ",", 2); + gchar **splitted = g_strsplit(ws_optarg, ",", 2); if (splitted[0] && splitted[0][0] != '\0') { secrets_type_id = lookup_secrets_type(splitted[0]); if (secrets_type_id == 0) { @@ -1313,7 +1302,7 @@ main(int argc, char *argv[]) if (!capture_comments) { capture_comments = g_ptr_array_new_with_free_func(g_free); } - g_ptr_array_add(capture_comments, g_strdup(optarg)); + g_ptr_array_add(capture_comments, g_strdup(ws_optarg)); break; } @@ -1328,9 +1317,9 @@ main(int argc, char *argv[]) guint frame_number; gint string_start_index = 0; - if ((sscanf(optarg, "%u:%n", &frame_number, &string_start_index) < 1) || (string_start_index == 0)) { + if ((sscanf(ws_optarg, "%u:%n", &frame_number, &string_start_index) < 1) || (string_start_index == 0)) { fprintf(stderr, "editcap: \"%s\" isn't a valid :\n\n", - optarg); + ws_optarg); ret = INVALID_OPTION; goto clean_exit; } @@ -1341,7 +1330,7 @@ main(int argc, char *argv[]) } /* Insert this entry (framenum -> comment) */ - g_tree_replace(frames_user_comments, GUINT_TO_POINTER(frame_number), g_strdup(optarg+string_start_index)); + g_tree_replace(frames_user_comments, GUINT_TO_POINTER(frame_number), g_strdup(ws_optarg+string_start_index)); break; } @@ -1351,7 +1340,7 @@ main(int argc, char *argv[]) nstime_t in_time; check_startstop = TRUE; - if ((0 < iso8601_to_nstime(&in_time, optarg)) || (0 < unix_epoch_to_nstime(&in_time, optarg))) { + if ((0 < iso8601_to_nstime(&in_time, ws_optarg)) || (0 < unix_epoch_to_nstime(&in_time, ws_optarg))) { if (opt == 'A') { nstime_copy(&starttime, &in_time); have_starttime = TRUE; @@ -1363,21 +1352,21 @@ main(int argc, char *argv[]) } else { fprintf(stderr, "editcap: \"%s\" isn't a valid date and time\n\n", - optarg); + ws_optarg); ret = INVALID_OPTION; goto clean_exit; } } case 'c': - split_packet_count = get_nonzero_guint32(optarg, "packet count"); + split_packet_count = get_nonzero_guint32(ws_optarg, "packet count"); break; case 'C': { int choplen = 0, chopoff = 0; - switch (sscanf(optarg, "%d:%d", &chopoff, &choplen)) { + switch (sscanf(ws_optarg, "%d:%d", &chopoff, &choplen)) { case 1: /* only the chop length was specififed */ choplen = chopoff; chopoff = 0; @@ -1388,7 +1377,7 @@ main(int argc, char *argv[]) default: fprintf(stderr, "editcap: \"%s\" isn't a valid chop length or offset:length\n", - optarg); + ws_optarg); ret = INVALID_OPTION; goto clean_exit; break; @@ -1419,7 +1408,7 @@ main(int argc, char *argv[]) case 'D': dup_detect = TRUE; dup_detect_by_time = FALSE; - dup_window = get_guint32(optarg, "duplicate window"); + dup_window = get_guint32(ws_optarg, "duplicate window"); if (dup_window > MAX_DUP_DEPTH) { fprintf(stderr, "editcap: \"%d\" duplicate window value must be between 0 and %d inclusive.\n", dup_window, MAX_DUP_DEPTH); @@ -1429,20 +1418,20 @@ main(int argc, char *argv[]) break; case 'E': - err_prob = g_ascii_strtod(optarg, &p); - if (p == optarg || err_prob < 0.0 || err_prob > 1.0) { + err_prob = g_ascii_strtod(ws_optarg, &p); + if (p == ws_optarg || err_prob < 0.0 || err_prob > 1.0) { fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n", - optarg); + ws_optarg); ret = INVALID_OPTION; goto clean_exit; } break; case 'F': - out_file_type_subtype = wtap_name_to_file_type_subtype(optarg); + out_file_type_subtype = wtap_name_to_file_type_subtype(ws_optarg); if (out_file_type_subtype < 0) { fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n", - optarg); + ws_optarg); list_capture_types(stderr); ret = INVALID_OPTION; goto clean_exit; @@ -1457,7 +1446,7 @@ main(int argc, char *argv[]) case 'i': /* break capture file based on time interval */ { - double spb = get_positive_double(optarg, "time interval"); + double spb = get_positive_double(ws_optarg, "time interval"); if (spb == 0.0) { cmdarg_err("The specified interval is zero"); ret = INVALID_OPTION; @@ -1472,7 +1461,7 @@ main(int argc, char *argv[]) break; case 'I': /* ignored_bytes at the beginning of the frame for duplications removal */ - ignored_bytes = get_guint32(optarg, "number of bytes to ignore"); + ignored_bytes = get_guint32(ws_optarg, "number of bytes to ignore"); break; case 'L': @@ -1480,7 +1469,7 @@ main(int argc, char *argv[]) break; case 'o': - change_offset = get_guint32(optarg, "change offset"); + change_offset = get_guint32(ws_optarg, "change offset"); break; case 'r': @@ -1493,11 +1482,11 @@ main(int argc, char *argv[]) break; case 's': - snaplen = get_nonzero_guint32(optarg, "snapshot length"); + snaplen = get_nonzero_guint32(ws_optarg, "snapshot length"); break; case 'S': - if (!set_strict_time_adj(optarg)) { + if (!set_strict_time_adj(ws_optarg)) { ret = INVALID_OPTION; goto clean_exit; } @@ -1505,17 +1494,17 @@ main(int argc, char *argv[]) break; case 't': - if (!set_time_adjustment(optarg)) { + if (!set_time_adjustment(ws_optarg)) { ret = INVALID_OPTION; goto clean_exit; } break; case 'T': - out_frame_type = wtap_name_to_encap(optarg); + out_frame_type = wtap_name_to_encap(ws_optarg); if (out_frame_type < 0) { fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n", - optarg); + ws_optarg); list_encap_types(stderr); ret = INVALID_OPTION; goto clean_exit; @@ -1540,7 +1529,7 @@ main(int argc, char *argv[]) dup_detect = FALSE; dup_detect_by_time = TRUE; dup_window = MAX_DUP_DEPTH; - if (!set_rel_time(optarg)) { + if (!set_rel_time(ws_optarg)) { ret = INVALID_OPTION; goto clean_exit; } @@ -1548,7 +1537,7 @@ main(int argc, char *argv[]) case '?': /* Bad options if GNU getopt */ case ':': /* missing option argument */ - switch(optopt) { + switch(ws_optopt) { case'F': list_capture_types(stdout); break; @@ -1557,9 +1546,9 @@ main(int argc, char *argv[]) break; default: if (opt == '?') { - fprintf(stderr, "editcap: invalid option -- '%c'\n", optopt); + fprintf(stderr, "editcap: invalid option -- '%c'\n", ws_optopt); } else { - fprintf(stderr, "editcap: option requires an argument -- '%c'\n", optopt); + fprintf(stderr, "editcap: option requires an argument -- '%c'\n", ws_optopt); } print_usage(stderr); ret = INVALID_OPTION; @@ -1571,10 +1560,10 @@ main(int argc, char *argv[]) } /* processing commmand-line options */ #ifdef DEBUG - fprintf(stderr, "Optind = %i, argc = %i\n", optind, argc); + fprintf(stderr, "Optind = %i, argc = %i\n", ws_optind, argc); #endif - if ((argc - optind) < 2) { + if ((argc - ws_optind) < 2) { print_usage(stderr); ret = INVALID_OPTION; goto clean_exit; @@ -1609,16 +1598,16 @@ main(int argc, char *argv[]) goto clean_exit; } - wth = wtap_open_offline(argv[optind], WTAP_TYPE_AUTO, &read_err, &read_err_info, FALSE); + wth = wtap_open_offline(argv[ws_optind], WTAP_TYPE_AUTO, &read_err, &read_err_info, FALSE); if (!wth) { - cfile_open_failure_message(argv[optind], read_err, read_err_info); + cfile_open_failure_message(argv[ws_optind], read_err, read_err_info); ret = INVALID_FILE; goto clean_exit; } if (verbose) { - fprintf(stderr, "File %s is a %s capture file.\n", argv[optind], + fprintf(stderr, "File %s is a %s capture file.\n", argv[ws_optind], wtap_file_type_subtype_description(wtap_file_type_subtype(wth))); } @@ -1740,7 +1729,7 @@ main(int argc, char *argv[]) * Now process the arguments following the input and output file * names, if any; they specify packets to include/exclude. */ - for (i = optind + 2; i < argc; i++) + for (i = ws_optind + 2; i < argc; i++) if (add_selection(argv[i], &max_packet_number) == FALSE) break; @@ -1778,14 +1767,14 @@ main(int argc, char *argv[]) /* Extra actions for the first packet */ if (read_count == 1) { if (split_packet_count != 0 || !nstime_is_unset(&secs_per_block)) { - if (!fileset_extract_prefix_suffix(argv[optind+1], &fprefix, &fsuffix)) { + if (!fileset_extract_prefix_suffix(argv[ws_optind+1], &fprefix, &fsuffix)) { ret = CANT_EXTRACT_PREFIX; goto clean_exit; } filename = fileset_get_filename_by_pattern(block_cnt++, rec, fprefix, fsuffix); } else { - filename = g_strdup(argv[optind+1]); + filename = g_strdup(argv[ws_optind+1]); } ws_assert(filename); @@ -1810,7 +1799,7 @@ main(int argc, char *argv[]) * Process whatever IDBs we haven't seen yet. */ if (!process_new_idbs(wth, pdh, idbs_seen, &write_err, &write_err_info)) { - cfile_write_failure_message(argv[optind], filename, + cfile_write_failure_message(argv[ws_optind], filename, write_err, write_err_info, read_count, out_file_type_subtype); @@ -2255,7 +2244,7 @@ main(int argc, char *argv[]) /* Attempt to dump out current frame to the output file */ if (!wtap_dump(pdh, rec, buf, &write_err, &write_err_info)) { - cfile_write_failure_message(argv[optind], filename, + cfile_write_failure_message(argv[ws_optind], filename, write_err, write_err_info, read_count, out_file_type_subtype); @@ -2276,14 +2265,14 @@ main(int argc, char *argv[]) if (read_err != 0) { /* Print a message noting that the read failed somewhere along the * line. */ - cfile_read_failure_message(argv[optind], read_err, read_err_info); + cfile_read_failure_message(argv[ws_optind], read_err, read_err_info); } if (!pdh) { /* No valid packages found, open the outfile so we can write an * empty header */ g_free (filename); - filename = g_strdup(argv[optind+1]); + filename = g_strdup(argv[ws_optind+1]); pdh = editcap_dump_open(filename, ¶ms, idbs_seen, &write_err, &write_err_info); diff --git a/epan/ex-opt.c b/epan/ex-opt.c index 112309115f..a6a99ea3c4 100644 --- a/epan/ex-opt.c +++ b/epan/ex-opt.c @@ -19,13 +19,13 @@ static GHashTable* ex_opts = NULL; -gboolean ex_opt_add(const gchar* optarg) { +gboolean ex_opt_add(const gchar* ws_optarg) { gchar** splitted; if (!ex_opts) ex_opts = g_hash_table_new(g_str_hash,g_str_equal); - splitted = g_strsplit(optarg,":",2); + splitted = g_strsplit(ws_optarg,":",2); if (splitted[0] && splitted[1]) { GPtrArray* this_opts = (GPtrArray *)g_hash_table_lookup(ex_opts,splitted[0]); diff --git a/epan/ex-opt.h b/epan/ex-opt.h index ac8c934851..b4d7e06654 100644 --- a/epan/ex-opt.h +++ b/epan/ex-opt.h @@ -22,7 +22,7 @@ extern "C" { #endif /* __cplusplus */ /* will be called by main each time a -X option is found */ -WS_DLL_PUBLIC gboolean ex_opt_add(const gchar* optarg); +WS_DLL_PUBLIC gboolean ex_opt_add(const gchar* ws_optarg); /* yields the number of arguments of a given key obviously returns 0 if there aren't */ WS_DLL_PUBLIC gint ex_opt_count(const gchar* key); diff --git a/epan/stats_tree_priv.h b/epan/stats_tree_priv.h index 4fe77293b4..6de4099c87 100644 --- a/epan/stats_tree_priv.h +++ b/epan/stats_tree_priv.h @@ -189,9 +189,9 @@ WS_DLL_PUBLIC void stats_tree_reinit(void *p_st); /* callback for destoy */ WS_DLL_PUBLIC void stats_tree_free(stats_tree *st); -/** given an optarg splits the abbr part +/** given an ws_optarg splits the abbr part and returns a newly allocated buffer containing it */ -WS_DLL_PUBLIC gchar *stats_tree_get_abbr(const gchar *optarg); +WS_DLL_PUBLIC gchar *stats_tree_get_abbr(const gchar *ws_optarg); /** obtains a stats tree from the registry given its abbr */ WS_DLL_PUBLIC stats_tree_cfg *stats_tree_get_cfg_by_abbr(const char *abbr); diff --git a/extcap/androiddump.c b/extcap/androiddump.c index 88a6f89192..4720223273 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -2598,8 +2598,8 @@ int main(int argc, char *argv[]) { extcap_help_add_option(extcap_conf, "--bt-local-ip ", "the bluetooth local IP"); extcap_help_add_option(extcap_conf, "--bt-local-tcp-port ", "the bluetooth local TCP port"); - opterr = 0; - optind = 0; + ws_opterr = 0; + ws_optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); @@ -2607,7 +2607,7 @@ int main(int argc, char *argv[]) { goto end; } - while ((result = getopt_long(argc, argv, "", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, "", longopts, &option_idx)) != -1) { switch (result) { case OPT_VERSION: @@ -2619,77 +2619,77 @@ int main(int argc, char *argv[]) { ret = EXIT_CODE_SUCCESS; goto end; case OPT_CONFIG_ADB_SERVER_IP: - adb_server_ip = optarg; + adb_server_ip = ws_optarg; break; case OPT_CONFIG_ADB_SERVER_TCP_PORT: adb_server_tcp_port = &local_adb_server_tcp_port; - if (!optarg){ + if (!ws_optarg){ ws_warning("Impossible exception. Parameter required argument, but there is no it right now."); goto end; } - if (!ws_strtou16(optarg, NULL, adb_server_tcp_port)) { - ws_warning("Invalid adb server TCP port: %s", optarg); + if (!ws_strtou16(ws_optarg, NULL, adb_server_tcp_port)) { + ws_warning("Invalid adb server TCP port: %s", ws_optarg); goto end; } break; case OPT_CONFIG_LOGCAT_TEXT: - if (optarg && !*optarg) + if (ws_optarg && !*ws_optarg) logcat_text = TRUE; else - logcat_text = (g_ascii_strncasecmp(optarg, "TRUE", 4) == 0); + logcat_text = (g_ascii_strncasecmp(ws_optarg, "TRUE", 4) == 0); break; case OPT_CONFIG_LOGCAT_IGNORE_LOG_BUFFER: - if (optarg == NULL || (optarg && !*optarg)) + if (ws_optarg == NULL || (ws_optarg && !*ws_optarg)) logcat_ignore_log_buffer = TRUE; else - logcat_ignore_log_buffer = (g_ascii_strncasecmp(optarg, "TRUE", 4) == 0); + logcat_ignore_log_buffer = (g_ascii_strncasecmp(ws_optarg, "TRUE", 4) == 0); break; case OPT_CONFIG_LOGCAT_CUSTOM_OPTIONS: - if (optarg == NULL || (optarg && *optarg == '\0')) { + if (ws_optarg == NULL || (ws_optarg && *ws_optarg == '\0')) { logcat_custom_parameter = NULL; break; } - if (g_regex_match_simple("(^|\\s)-[bBcDfgLnpPrv]", optarg, G_REGEX_RAW, (GRegexMatchFlags)0)) { + if (g_regex_match_simple("(^|\\s)-[bBcDfgLnpPrv]", ws_optarg, G_REGEX_RAW, (GRegexMatchFlags)0)) { ws_error("Found prohibited option in logcat-custom-options"); return EXIT_CODE_GENERIC; } - logcat_custom_parameter = optarg; + logcat_custom_parameter = ws_optarg; break; case OPT_CONFIG_BT_SERVER_TCP_PORT: bt_server_tcp_port = &local_bt_server_tcp_port; - if (!optarg){ + if (!ws_optarg){ ws_warning("Impossible exception. Parameter required argument, but there is no it right now."); goto end; } - if (!ws_strtou16(optarg, NULL, bt_server_tcp_port)) { - ws_warning("Invalid bluetooth server TCP port: %s", optarg); + if (!ws_strtou16(ws_optarg, NULL, bt_server_tcp_port)) { + ws_warning("Invalid bluetooth server TCP port: %s", ws_optarg); goto end; } break; case OPT_CONFIG_BT_FORWARD_SOCKET: - bt_forward_socket = (g_ascii_strncasecmp(optarg, "TRUE", 4) == 0); + bt_forward_socket = (g_ascii_strncasecmp(ws_optarg, "TRUE", 4) == 0); break; case OPT_CONFIG_BT_LOCAL_IP: - bt_local_ip = optarg; + bt_local_ip = ws_optarg; break; case OPT_CONFIG_BT_LOCAL_TCP_PORT: bt_local_tcp_port = &local_bt_local_tcp_port; - if (!optarg){ + if (!ws_optarg){ ws_warning("Impossible exception. Parameter required argument, but there is no it right now."); goto end; } - if (!ws_strtou16(optarg, NULL, bt_local_tcp_port)) { - ws_warning("Invalid bluetooth local tcp port: %s", optarg); + if (!ws_strtou16(ws_optarg, NULL, bt_local_tcp_port)) { + ws_warning("Invalid bluetooth local tcp port: %s", ws_optarg); goto end; } break; default: - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { - ws_warning("Invalid argument <%s>. Try --help.\n", argv[optind - 1]); + ws_warning("Invalid argument <%s>. Try --help.\n", argv[ws_optind - 1]); goto end; } } diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index 499563332d..d9cdb46218 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -588,15 +588,15 @@ int main(int argc, char *argv[]) extcap_help_add_option(extcap_conf, "--remote-filter ", "a filter for remote capture " "(default: don't capture data for lal interfaces IPs)"); - opterr = 0; - optind = 0; + ws_opterr = 0; + ws_optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { @@ -611,68 +611,68 @@ int main(int argc, char *argv[]) case OPT_REMOTE_HOST: g_free(ssh_params->host); - ssh_params->host = g_strdup(optarg); + ssh_params->host = g_strdup(ws_optarg); break; case OPT_REMOTE_PORT: - if (!ws_strtou16(optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { - ws_warning("Invalid port: %s", optarg); + if (!ws_strtou16(ws_optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { + ws_warning("Invalid port: %s", ws_optarg); goto end; } break; case OPT_REMOTE_USERNAME: g_free(ssh_params->username); - ssh_params->username = g_strdup(optarg); + ssh_params->username = g_strdup(ws_optarg); break; case OPT_REMOTE_PASSWORD: g_free(ssh_params->password); - ssh_params->password = g_strdup(optarg); - memset(optarg, 'X', strlen(optarg)); + ssh_params->password = g_strdup(ws_optarg); + memset(ws_optarg, 'X', strlen(ws_optarg)); break; case OPT_SSHKEY: g_free(ssh_params->sshkey_path); - ssh_params->sshkey_path = g_strdup(optarg); + ssh_params->sshkey_path = g_strdup(ws_optarg); break; case OPT_SSHKEY_PASSPHRASE: g_free(ssh_params->sshkey_passphrase); - ssh_params->sshkey_passphrase = g_strdup(optarg); - memset(optarg, 'X', strlen(optarg)); + ssh_params->sshkey_passphrase = g_strdup(ws_optarg); + memset(ws_optarg, 'X', strlen(ws_optarg)); break; case OPT_PROXYCOMMAND: g_free(ssh_params->proxycommand); - ssh_params->proxycommand = g_strdup(optarg); + ssh_params->proxycommand = g_strdup(ws_optarg); break; case OPT_REMOTE_INTERFACE: g_free(remote_interface); - remote_interface = g_strdup(optarg); + remote_interface = g_strdup(ws_optarg); break; case OPT_REMOTE_FILTER: g_free(remote_filter); - remote_filter = g_strdup(optarg); + remote_filter = g_strdup(ws_optarg); break; case OPT_REMOTE_COUNT: - if (!ws_strtou32(optarg, NULL, &count)) { - ws_warning("Invalid packet count: %s", optarg); + if (!ws_strtou32(ws_optarg, NULL, &count)) { + ws_warning("Invalid packet count: %s", ws_optarg); goto end; } break; case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } @@ -680,8 +680,8 @@ int main(int argc, char *argv[]) extcap_cmdline_debug(argv, argc); - if (optind != argc) { - ws_warning("Unexpected extra option: %s", argv[optind]); + if (ws_optind != argc) { + ws_warning("Unexpected extra option: %s", argv[ws_optind]); goto end; } diff --git a/extcap/dpauxmon.c b/extcap/dpauxmon.c index 3f31d7020d..d668f20633 100644 --- a/extcap/dpauxmon.c +++ b/extcap/dpauxmon.c @@ -530,15 +530,15 @@ int main(int argc, char *argv[]) extcap_help_add_option(extcap_conf, "--version", "print the version"); extcap_help_add_option(extcap_conf, "--port ", "the dpauxmon interface index"); - opterr = 0; - optind = 0; + ws_opterr = 0; + ws_optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: @@ -551,20 +551,20 @@ int main(int argc, char *argv[]) goto end; case OPT_INTERFACE_ID: - if (!ws_strtou32(optarg, NULL, &interface_id)) { - ws_warning("Invalid interface id: %s", optarg); + if (!ws_strtou32(ws_optarg, NULL, &interface_id)) { + ws_warning("Invalid interface id: %s", ws_optarg); goto end; } break; case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } @@ -572,8 +572,8 @@ int main(int argc, char *argv[]) extcap_cmdline_debug(argv, argc); - if (optind != argc) { - ws_warning("Unexpected extra option: %s", argv[optind]); + if (ws_optind != argc) { + ws_warning("Unexpected extra option: %s", argv[ws_optind]); goto end; } diff --git a/extcap/etl.c b/extcap/etl.c index 4bdb83c754..a4ff72e152 100644 --- a/extcap/etl.c +++ b/extcap/etl.c @@ -17,7 +17,7 @@ #include "config.h" #include "etl.h" -#include "wsutil/wsgetopt.h" +#include "wsutil/ws_getopt.h" #include "wsutil/strtoi.h" #include "etw_message.h" @@ -179,11 +179,11 @@ wtap_open_return_val etw_dump(const char* etl_filename, const char* pcapng_filen params_array_num++; } - optind = 0; - while ((opt_result = getopt_long(params_array_num, params_array, ":", longopts, &option_idx)) != -1) { + ws_optind = 0; + while ((opt_result = ws_getopt_long(params_array_num, params_array, ":", longopts, &option_idx)) != -1) { switch (opt_result) { case OPT_PROVIDER: - mbstowcs(provider_id, optarg, FILENAME_MAX); + mbstowcs(provider_id, ws_optarg, FILENAME_MAX); if (UuidFromString(provider_id, &g_provider_filters[provider_idx].ProviderId) == RPC_S_INVALID_STRING_UUID) { PEVT_VARIANT value = NULL; @@ -209,7 +209,7 @@ wtap_open_return_val etw_dump(const char* etl_filename, const char* pcapng_filen } else { - *err_info = g_strdup_printf("Cannot convert provider %s to a GUID, err is 0x%x", optarg, *err); + *err_info = g_strdup_printf("Cannot convert provider %s to a GUID, err is 0x%x", ws_optarg, *err); return WTAP_OPEN_ERROR; } @@ -219,7 +219,7 @@ wtap_open_return_val etw_dump(const char* etl_filename, const char* pcapng_filen if (IsEqualGUID(&g_provider_filters[0].ProviderId, &ZeroGuid)) { *err = ERROR_INVALID_PARAMETER; - *err_info = g_strdup_printf("Provider %s is zero, err is 0x%x", optarg, *err); + *err_info = g_strdup_printf("Provider %s is zero, err is 0x%x", ws_optarg, *err); return WTAP_OPEN_ERROR; } provider_idx++; @@ -232,11 +232,11 @@ wtap_open_return_val etw_dump(const char* etl_filename, const char* pcapng_filen return WTAP_OPEN_ERROR; } - g_provider_filters[provider_idx - 1].Keyword = _strtoui64(optarg, NULL, 0); + g_provider_filters[provider_idx - 1].Keyword = _strtoui64(ws_optarg, NULL, 0); if (!g_provider_filters[provider_idx - 1].Keyword) { *err = ERROR_INVALID_PARAMETER; - *err_info = g_strdup_printf("Keyword %s cannot be converted, err is 0x%x", optarg, *err); + *err_info = g_strdup_printf("Keyword %s cannot be converted, err is 0x%x", ws_optarg, *err); return WTAP_OPEN_ERROR; } break; @@ -248,17 +248,17 @@ wtap_open_return_val etw_dump(const char* etl_filename, const char* pcapng_filen return WTAP_OPEN_ERROR; } - convert_level = strtoul(optarg, NULL, 0); + convert_level = strtoul(ws_optarg, NULL, 0); if (convert_level > UCHAR_MAX) { *err = ERROR_INVALID_PARAMETER; - *err_info = g_strdup_printf("Level %s is bigger than 0xff, err is 0x%x", optarg, *err); + *err_info = g_strdup_printf("Level %s is bigger than 0xff, err is 0x%x", ws_optarg, *err); return WTAP_OPEN_ERROR; } if (!convert_level) { *err = ERROR_INVALID_PARAMETER; - *err_info = g_strdup_printf("Level %s cannot be converted, err is 0x%x", optarg, *err); + *err_info = g_strdup_printf("Level %s cannot be converted, err is 0x%x", ws_optarg, *err); return WTAP_OPEN_ERROR; } diff --git a/extcap/etwdump.c b/extcap/etwdump.c index a89cd4b677..903ca1e667 100644 --- a/extcap/etwdump.c +++ b/extcap/etwdump.c @@ -170,7 +170,7 @@ int main(int argc, char* argv[]) goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_VERSION: extcap_version_print(extcap_conf); @@ -183,12 +183,12 @@ int main(int argc, char* argv[]) goto end; case OPT_ETLFILE: - etlfile = g_strdup(optarg); + etlfile = g_strdup(ws_optarg); break; case OPT_PARAMS: /* Add params as the prefix since getopt_long will ignore the first argument always */ - params = g_strdup_printf("params %s", optarg); + params = g_strdup_printf("params %s", ws_optarg); break; case OPT_INCLUDE_UNDECIDABLE_EVENT: @@ -197,14 +197,14 @@ int main(int argc, char* argv[]) case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: /* Handle extcap specific options */ - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } diff --git a/extcap/extcap-base.c b/extcap/extcap-base.c index 7266a24814..2ec44fe82a 100644 --- a/extcap/extcap-base.c +++ b/extcap/extcap-base.c @@ -21,18 +21,6 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif #include "ws_attributes.h" diff --git a/extcap/extcap-base.h b/extcap/extcap-base.h index 20d4be31c3..5d3548fe06 100644 --- a/extcap/extcap-base.h +++ b/extcap/extcap-base.h @@ -19,18 +19,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG - #include -#else - #include -#endif +#include #ifdef _WIN32 #include diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index 2e5f90146c..ad74474e93 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -205,7 +205,7 @@ int main(int argc, char *argv[]) goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_VERSION: extcap_version_print(extcap_conf); @@ -218,23 +218,23 @@ int main(int argc, char *argv[]) goto end; case OPT_MAXBYTES: - if (!ws_strtou16(optarg, NULL, &maxbytes)) { + if (!ws_strtou16(ws_optarg, NULL, &maxbytes)) { ws_warning("Invalid parameter maxbytes: %s (max value is %u)", - optarg, G_MAXUINT16); + ws_optarg, G_MAXUINT16); goto end; } break; case OPT_COUNT: - if (!ws_strtou64(optarg, NULL, &count)) { - ws_warning("Invalid packet count: %s", optarg); + if (!ws_strtou64(ws_optarg, NULL, &count)) { + ws_warning("Invalid packet count: %s", ws_optarg); goto end; } break; case OPT_DELAY: - if (!ws_strtou64(optarg, NULL, &packet_delay_ms)) { - ws_warning("Invalid packet delay: %s", optarg); + if (!ws_strtou64(ws_optarg, NULL, &packet_delay_ms)) { + ws_warning("Invalid packet delay: %s", ws_optarg); goto end; } break; @@ -249,19 +249,19 @@ int main(int argc, char *argv[]) case OPT_TYPE: g_free(type); - type = g_strdup(optarg); + type = g_strdup(ws_optarg); break; case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: /* Handle extcap specific options */ - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } diff --git a/extcap/sdjournal.c b/extcap/sdjournal.c index d1d9791933..40f9efe94b 100644 --- a/extcap/sdjournal.c +++ b/extcap/sdjournal.c @@ -385,15 +385,15 @@ int main(int argc, char **argv) extcap_help_add_option(extcap_conf, "--version", "print the version"); extcap_help_add_option(extcap_conf, "--start-from ", "starting position"); - opterr = 0; - optind = 0; + ws_opterr = 0; + ws_optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { @@ -408,12 +408,12 @@ int main(int argc, char **argv) goto end; case OPT_START_FROM: - start_from_entries = (int) strtol(optarg, NULL, 10); + start_from_entries = (int) strtol(ws_optarg, NULL, 10); if (errno == EINVAL) { - ws_warning("Invalid entry count: %s", optarg); + ws_warning("Invalid entry count: %s", ws_optarg); goto end; } - if (strlen(optarg) > 0 && optarg[0] == '+') { + if (strlen(ws_optarg) > 0 && ws_optarg[0] == '+') { start_from_end = FALSE; } if (start_from_entries < 0) { @@ -425,12 +425,12 @@ int main(int argc, char **argv) case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } diff --git a/extcap/sshdump.c b/extcap/sshdump.c index afb7693193..2c3bb37c7a 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -426,15 +426,15 @@ int main(int argc, char *argv[]) "listen on local interfaces IPs)"); extcap_help_add_option(extcap_conf, "--remote-count ", "the number of packets to capture"); - opterr = 0; - optind = 0; + ws_opterr = 0; + ws_optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { @@ -450,51 +450,51 @@ int main(int argc, char *argv[]) case OPT_REMOTE_HOST: g_free(ssh_params->host); - ssh_params->host = g_strdup(optarg); + ssh_params->host = g_strdup(ws_optarg); break; case OPT_REMOTE_PORT: - if (!ws_strtou16(optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { - ws_warning("Invalid port: %s", optarg); + if (!ws_strtou16(ws_optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { + ws_warning("Invalid port: %s", ws_optarg); goto end; } break; case OPT_REMOTE_USERNAME: g_free(ssh_params->username); - ssh_params->username = g_strdup(optarg); + ssh_params->username = g_strdup(ws_optarg); break; case OPT_REMOTE_PASSWORD: g_free(ssh_params->password); - ssh_params->password = g_strdup(optarg); - memset(optarg, 'X', strlen(optarg)); + ssh_params->password = g_strdup(ws_optarg); + memset(ws_optarg, 'X', strlen(ws_optarg)); break; case OPT_SSHKEY: g_free(ssh_params->sshkey_path); - ssh_params->sshkey_path = g_strdup(optarg); + ssh_params->sshkey_path = g_strdup(ws_optarg); break; case OPT_SSHKEY_PASSPHRASE: g_free(ssh_params->sshkey_passphrase); - ssh_params->sshkey_passphrase = g_strdup(optarg); - memset(optarg, 'X', strlen(optarg)); + ssh_params->sshkey_passphrase = g_strdup(ws_optarg); + memset(ws_optarg, 'X', strlen(ws_optarg)); break; case OPT_PROXYCOMMAND: g_free(ssh_params->proxycommand); - ssh_params->proxycommand = g_strdup(optarg); + ssh_params->proxycommand = g_strdup(ws_optarg); break; case OPT_REMOTE_INTERFACE: g_free(remote_interface); - remote_interface = g_strdup(optarg); + remote_interface = g_strdup(ws_optarg); break; case OPT_REMOTE_CAPTURE_COMMAND: g_free(remote_capture_command); - remote_capture_command = g_strdup(optarg); + remote_capture_command = g_strdup(ws_optarg); break; case OPT_REMOTE_SUDO: @@ -503,12 +503,12 @@ int main(int argc, char *argv[]) case OPT_REMOTE_FILTER: g_free(remote_filter); - remote_filter = g_strdup(optarg); + remote_filter = g_strdup(ws_optarg); break; case OPT_REMOTE_COUNT: - if (!ws_strtou32(optarg, NULL, &count)) { - ws_warning("Invalid value for count: %s", optarg); + if (!ws_strtou32(ws_optarg, NULL, &count)) { + ws_warning("Invalid value for count: %s", ws_optarg); goto end; } break; @@ -519,12 +519,12 @@ int main(int argc, char *argv[]) case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } diff --git a/extcap/udpdump.c b/extcap/udpdump.c index cedc2ed932..63b29253e8 100644 --- a/extcap/udpdump.c +++ b/extcap/udpdump.c @@ -399,15 +399,15 @@ int main(int argc, char *argv[]) extcap_help_add_option(extcap_conf, "--port ", port_msg); g_free(port_msg); - opterr = 0; - optind = 0; + ws_opterr = 0; + ws_optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } - while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { + while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: @@ -420,25 +420,25 @@ int main(int argc, char *argv[]) goto end; case OPT_PORT: - if (!ws_strtou16(optarg, NULL, &port)) { - ws_warning("Invalid port: %s", optarg); + if (!ws_strtou16(ws_optarg, NULL, &port)) { + ws_warning("Invalid port: %s", ws_optarg); goto end; } break; case OPT_PAYLOAD: g_free(payload); - payload = g_strdup(optarg); + payload = g_strdup(ws_optarg); break; case ':': /* missing option argument */ - ws_warning("Option '%s' requires an argument", argv[optind - 1]); + ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]); break; default: - if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { - ws_warning("Invalid option: %s", argv[optind - 1]); + if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) { + ws_warning("Invalid option: %s", argv[ws_optind - 1]); goto end; } } @@ -446,8 +446,8 @@ int main(int argc, char *argv[]) extcap_cmdline_debug(argv, argc); - if (optind != argc) { - ws_warning("Unexpected extra option: %s", argv[optind]); + if (ws_optind != argc) { + ws_warning("Unexpected extra option: %s", argv[ws_optind]); goto end; } diff --git a/mergecap.c b/mergecap.c index 66c1d5d792..0c5b3d768d 100644 --- a/mergecap.c +++ b/mergecap.c @@ -18,18 +18,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -265,7 +254,7 @@ main(int argc, char *argv[]) wtap_init(TRUE); /* Process the options first */ - while ((opt = getopt_long(argc, argv, "aF:hI:s:vVw:", long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, "aF:hI:s:vVw:", long_options, NULL)) != -1) { switch (opt) { case 'a': @@ -273,10 +262,10 @@ main(int argc, char *argv[]) break; case 'F': - file_type = wtap_name_to_file_type_subtype(optarg); + file_type = wtap_name_to_file_type_subtype(ws_optarg); if (file_type < 0) { fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n", - optarg); + ws_optarg); list_capture_types(); status = MERGE_ERR_INVALID_OPTION; goto clean_exit; @@ -290,10 +279,10 @@ main(int argc, char *argv[]) break; case 'I': - mode = merge_string_to_idb_merge_mode(optarg); + mode = merge_string_to_idb_merge_mode(ws_optarg); if (mode == IDB_MERGE_MODE_MAX) { fprintf(stderr, "mergecap: \"%s\" isn't a valid IDB merge mode\n", - optarg); + ws_optarg); list_idb_merge_modes(); status = MERGE_ERR_INVALID_OPTION; goto clean_exit; @@ -301,7 +290,7 @@ main(int argc, char *argv[]) break; case 's': - snaplen = get_nonzero_guint32(optarg, "snapshot length"); + snaplen = get_nonzero_guint32(ws_optarg, "snapshot length"); break; case 'v': @@ -314,11 +303,11 @@ main(int argc, char *argv[]) break; case 'w': - out_filename = optarg; + out_filename = ws_optarg; break; case '?': /* Bad options if GNU getopt */ - switch(optopt) { + switch(ws_optopt) { case'F': list_capture_types(); break; @@ -344,7 +333,7 @@ main(int argc, char *argv[]) /* check for proper args; at a minimum, must have an output * filename and one input file */ - in_file_count = argc - optind; + in_file_count = argc - ws_optind; if (!out_filename) { fprintf(stderr, "mergecap: an output filename must be set with -w\n"); fprintf(stderr, " run with -h for help\n"); @@ -376,7 +365,7 @@ main(int argc, char *argv[]) if (strcmp(out_filename, "-") == 0) { /* merge the files to the standard output */ status = merge_files_to_stdout(file_type, - (const char *const *) &argv[optind], + (const char *const *) &argv[ws_optind], in_file_count, do_append, mode, snaplen, get_appname_and_version(), verbose ? &cb : NULL, @@ -384,7 +373,7 @@ main(int argc, char *argv[]) } else { /* merge the files to the outfile */ status = merge_files(out_filename, file_type, - (const char *const *) &argv[optind], in_file_count, + (const char *const *) &argv[ws_optind], in_file_count, do_append, mode, snaplen, get_appname_and_version(), verbose ? &cb : NULL, &err, &err_info, &err_fileno, &err_framenum); @@ -400,7 +389,7 @@ main(int argc, char *argv[]) break; case MERGE_ERR_CANT_OPEN_INFILE: - cfile_open_failure_message(argv[optind + err_fileno], err, err_info); + cfile_open_failure_message(argv[ws_optind + err_fileno], err, err_info); break; case MERGE_ERR_CANT_OPEN_OUTFILE: @@ -408,16 +397,16 @@ main(int argc, char *argv[]) break; case MERGE_ERR_CANT_READ_INFILE: - cfile_read_failure_message(argv[optind + err_fileno], err, err_info); + cfile_read_failure_message(argv[ws_optind + err_fileno], err, err_info); break; case MERGE_ERR_BAD_PHDR_INTERFACE_ID: cmdarg_err("Record %u of \"%s\" has an interface ID that does not match any IDB in its file.", - err_framenum, argv[optind + err_fileno]); + err_framenum, argv[ws_optind + err_fileno]); break; case MERGE_ERR_CANT_WRITE_OUTFILE: - cfile_write_failure_message(argv[optind + err_fileno], out_filename, + cfile_write_failure_message(argv[ws_optind + err_fileno], out_filename, err, err_info, err_framenum, file_type); break; diff --git a/randpkt.c b/randpkt.c index 1c81db4f9c..dcfa1f0f73 100644 --- a/randpkt.c +++ b/randpkt.c @@ -31,18 +31,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include "randpkt_core/randpkt_core.h" @@ -171,10 +160,10 @@ main(int argc, char *argv[]) create_app_running_mutex(); #endif /* _WIN32 */ - while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) { switch (opt) { case 'b': /* max bytes */ - produce_max_bytes = get_positive_int(optarg, "max bytes"); + produce_max_bytes = get_positive_int(ws_optarg, "max bytes"); if (produce_max_bytes > 65536) { cmdarg_err("max bytes is > 65536"); ret = INVALID_OPTION; @@ -183,11 +172,11 @@ main(int argc, char *argv[]) break; case 'c': /* count */ - produce_count = get_positive_int(optarg, "count"); + produce_count = get_positive_int(ws_optarg, "count"); break; case 't': /* type of packet to produce */ - type = g_strdup(optarg); + type = g_strdup(ws_optarg); break; case 'h': @@ -208,8 +197,8 @@ main(int argc, char *argv[]) } /* any more command line parameters? */ - if (argc > optind) { - produce_filename = argv[optind]; + if (argc > ws_optind) { + produce_filename = argv[ws_optind]; } else { usage(TRUE); ret = INVALID_OPTION; diff --git a/rawshark.c b/rawshark.c index 1dafd06fa3..869f688ddb 100644 --- a/rawshark.c +++ b/rawshark.c @@ -35,18 +35,7 @@ #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include #include @@ -540,17 +529,17 @@ main(int argc, char *argv[]) /* Now get our args */ /* XXX - We should probably have an option to dump libpcap link types */ - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case 'd': /* Payload type */ - if (!set_link_type(optarg)) { - cmdarg_err("Invalid link type or protocol \"%s\"", optarg); + if (!set_link_type(ws_optarg)) { + cmdarg_err("Invalid link type or protocol \"%s\"", ws_optarg); ret = INVALID_OPTION; goto clean_exit; } break; case 'F': /* Read field to display */ - g_ptr_array_add(disp_fields, g_strdup(optarg)); + g_ptr_array_add(disp_fields, g_strdup(ws_optarg)); break; case 'h': /* Print help and exit */ show_help_header("Dump and analyze network traffic."); @@ -574,8 +563,8 @@ main(int argc, char *argv[]) break; #ifndef _WIN32 case 'm': - limit.rlim_cur = get_positive_int(optarg, "memory limit"); - limit.rlim_max = get_positive_int(optarg, "memory limit"); + limit.rlim_cur = get_positive_int(ws_optarg, "memory limit"); + limit.rlim_max = get_positive_int(ws_optarg, "memory limit"); if(setrlimit(RLIMIT_AS, &limit) != 0) { cmdarg_err("setrlimit() returned error"); @@ -588,7 +577,7 @@ main(int argc, char *argv[]) disable_name_resolution(); break; case 'N': /* Select what types of addresses/port #s to resolve */ - badopt = string_to_name_resolve(optarg, &gbl_resolv_flags); + badopt = string_to_name_resolve(ws_optarg, &gbl_resolv_flags); if (badopt != '\0') { cmdarg_err("-N specifies unknown resolving option '%c'; valid options are 'd', m', 'n', 'N', and 't'", badopt); @@ -600,13 +589,13 @@ main(int argc, char *argv[]) { char *errmsg = NULL; - switch (prefs_set_pref(optarg, &errmsg)) { + switch (prefs_set_pref(ws_optarg, &errmsg)) { case PREFS_SET_OK: break; case PREFS_SET_SYNTAX_ERR: - cmdarg_err("Invalid -o flag \"%s\"%s%s", optarg, + cmdarg_err("Invalid -o flag \"%s\"%s%s", ws_optarg, errmsg ? ": " : "", errmsg ? errmsg : ""); g_free(errmsg); ret = INVALID_OPTION; @@ -615,7 +604,7 @@ main(int argc, char *argv[]) case PREFS_SET_NO_SUCH_PREF: case PREFS_SET_OBSOLETE: - cmdarg_err("-o flag \"%s\" specifies unknown preference", optarg); + cmdarg_err("-o flag \"%s\" specifies unknown preference", ws_optarg); ret = INVALID_OPTION; goto clean_exit; break; @@ -626,11 +615,11 @@ main(int argc, char *argv[]) want_pcap_pkthdr = TRUE; break; case 'r': /* Read capture file xxx */ - pipe_name = g_strdup(optarg); + pipe_name = g_strdup(ws_optarg); break; case 'R': /* Read file filter */ if(n_rfilters < (int) sizeof(rfilters) / (int) sizeof(rfilters[0])) { - rfilters[n_rfilters++] = optarg; + rfilters[n_rfilters++] = ws_optarg; } else { cmdarg_err("Too many display filters"); @@ -642,36 +631,36 @@ main(int argc, char *argv[]) skip_pcap_header = TRUE; break; case 'S': /* Print string representations */ - if (!parse_field_string_format(optarg)) { + if (!parse_field_string_format(ws_optarg)) { cmdarg_err("Invalid field string format"); ret = INVALID_OPTION; goto clean_exit; } break; case 't': /* Time stamp type */ - if (strcmp(optarg, "r") == 0) + if (strcmp(ws_optarg, "r") == 0) timestamp_set_type(TS_RELATIVE); - else if (strcmp(optarg, "a") == 0) + else if (strcmp(ws_optarg, "a") == 0) timestamp_set_type(TS_ABSOLUTE); - else if (strcmp(optarg, "ad") == 0) + else if (strcmp(ws_optarg, "ad") == 0) timestamp_set_type(TS_ABSOLUTE_WITH_YMD); - else if (strcmp(optarg, "adoy") == 0) + else if (strcmp(ws_optarg, "adoy") == 0) timestamp_set_type(TS_ABSOLUTE_WITH_YDOY); - else if (strcmp(optarg, "d") == 0) + else if (strcmp(ws_optarg, "d") == 0) timestamp_set_type(TS_DELTA); - else if (strcmp(optarg, "dd") == 0) + else if (strcmp(ws_optarg, "dd") == 0) timestamp_set_type(TS_DELTA_DIS); - else if (strcmp(optarg, "e") == 0) + else if (strcmp(ws_optarg, "e") == 0) timestamp_set_type(TS_EPOCH); - else if (strcmp(optarg, "u") == 0) + else if (strcmp(ws_optarg, "u") == 0) timestamp_set_type(TS_UTC); - else if (strcmp(optarg, "ud") == 0) + else if (strcmp(ws_optarg, "ud") == 0) timestamp_set_type(TS_UTC_WITH_YMD); - else if (strcmp(optarg, "udoy") == 0) + else if (strcmp(ws_optarg, "udoy") == 0) timestamp_set_type(TS_UTC_WITH_YDOY); else { cmdarg_err("Invalid time stamp type \"%s\"", - optarg); + ws_optarg); cmdarg_err_cont( "It must be \"a\" for absolute, \"ad\" for absolute with YYYY-MM-DD date,"); cmdarg_err_cont( @@ -718,7 +707,7 @@ main(int argc, char *argv[]) still command-line arguments, treat them as the tokens of a capture filter (if no "-r" flag was specified) or a read filter (if a "-r" flag was specified. */ - if (optind < argc) { + if (ws_optind < argc) { if (pipe_name != NULL) { if (n_rfilters != 0) { cmdarg_err("Read filters were specified both with \"-R\" " @@ -726,7 +715,7 @@ main(int argc, char *argv[]) ret = INVALID_OPTION; goto clean_exit; } - rfilters[n_rfilters] = get_args_as_string(argc, argv, optind); + rfilters[n_rfilters] = get_args_as_string(argc, argv, ws_optind); } } diff --git a/reordercap.c b/reordercap.c index c8aa70297b..d52f49daff 100644 --- a/reordercap.c +++ b/reordercap.c @@ -15,18 +15,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -242,7 +231,7 @@ main(int argc, char *argv[]) wtap_init(TRUE); /* Process the options first */ - while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) { switch (opt) { case 'n': write_output_regardless = FALSE; @@ -262,10 +251,10 @@ main(int argc, char *argv[]) } /* Remaining args are file names */ - file_count = argc - optind; + file_count = argc - ws_optind; if (file_count == 2) { - infile = argv[optind]; - outfile = argv[optind+1]; + infile = argv[ws_optind]; + outfile = argv[ws_optind+1]; } else { print_usage(stderr); diff --git a/sharkd_daemon.c b/sharkd_daemon.c index 0c9363301f..d5bda8da61 100644 --- a/sharkd_daemon.c +++ b/sharkd_daemon.c @@ -30,18 +30,7 @@ #include #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #ifndef _WIN32 #include @@ -295,29 +284,29 @@ sharkd_init(int argc, char **argv) */ do { - if (optind > (argc - 1)) + if (ws_optind > (argc - 1)) break; - opt = getopt_long(argc, argv, optstring, long_options, NULL); + opt = ws_getopt_long(argc, argv, optstring, long_options, NULL); switch (opt) { case 'C': /* Configuration Profile */ - if (profile_exists(optarg, FALSE)) { - set_profile_name(optarg); // In Daemon Mode, we may need to do this again in the child process + if (profile_exists(ws_optarg, FALSE)) { + set_profile_name(ws_optarg); // In Daemon Mode, we may need to do this again in the child process } else { - fprintf(stderr, "Configuration Profile \"%s\" does not exist\n", optarg); + fprintf(stderr, "Configuration Profile \"%s\" does not exist\n", ws_optarg); return -1; } break; case 'a': - fd = socket_init(optarg); + fd = socket_init(ws_optarg); if (fd == INVALID_SOCKET) return -1; _server_fd = fd; - fprintf(stderr, "Sharkd listening on: %s\n", optarg); + fprintf(stderr, "Sharkd listening on: %s\n", ws_optarg); mode = SHARKD_MODE_GOLD_DAEMON; break; @@ -338,8 +327,8 @@ sharkd_init(int argc, char **argv) break; default: - if (!optopt) - fprintf(stderr, "This option isn't supported: %s\n", argv[optind]); + if (!ws_optopt) + fprintf(stderr, "This option isn't supported: %s\n", argv[ws_optind]); fprintf(stderr, "Use sharkd -h for details of supported options\n"); exit(0); break; diff --git a/text2pcap.c b/text2pcap.c index 2b2471f079..37bce82b15 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -113,18 +113,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include #include @@ -1467,7 +1456,7 @@ parse_options (int argc, char *argv[]) ws_init_version_info("Text2pcap (Wireshark)", NULL, NULL, NULL); /* Scan CLI parameters */ - while ((c = getopt_long(argc, argv, "aDdhqe:i:l:m:nN:o:u:s:S:t:T:v4:6:", long_options, NULL)) != -1) { + while ((c = ws_getopt_long(argc, argv, "aDdhqe:i:l:m:nN:o:u:s:S:t:T:v4:6:", long_options, NULL)) != -1) { switch (c) { case 'h': show_help_header("Generate a capture file from an ASCII hexdump of packets."); @@ -1477,17 +1466,17 @@ parse_options (int argc, char *argv[]) case 'd': if (!quiet) debug++; break; case 'D': has_direction = TRUE; break; case 'q': quiet = TRUE; debug = 0; break; - case 'l': pcap_link_type = (guint32)strtol(optarg, NULL, 0); break; - case 'm': max_offset = (guint32)strtol(optarg, NULL, 0); break; + case 'l': pcap_link_type = (guint32)strtol(ws_optarg, NULL, 0); break; + case 'm': max_offset = (guint32)strtol(ws_optarg, NULL, 0); break; case 'n': use_pcapng = TRUE; break; - case 'N': interface_name = optarg; break; + case 'N': interface_name = ws_optarg; break; case 'o': - if (optarg[0] != 'h' && optarg[0] != 'o' && optarg[0] != 'd') { - fprintf(stderr, "Bad argument for '-o': %s\n", optarg); + if (ws_optarg[0] != 'h' && ws_optarg[0] != 'o' && ws_optarg[0] != 'd') { + fprintf(stderr, "Bad argument for '-o': %s\n", ws_optarg); print_usage(stderr); return EXIT_FAILURE; } - switch (optarg[0]) { + switch (ws_optarg[0]) { case 'o': offset_base = 8; break; case 'h': offset_base = 16; break; case 'd': offset_base = 10; break; @@ -1495,18 +1484,18 @@ parse_options (int argc, char *argv[]) break; case 'e': hdr_ethernet = TRUE; - if (sscanf(optarg, "%x", &hdr_ethernet_proto) < 1) { - fprintf(stderr, "Bad argument for '-e': %s\n", optarg); + if (sscanf(ws_optarg, "%x", &hdr_ethernet_proto) < 1) { + fprintf(stderr, "Bad argument for '-e': %s\n", ws_optarg); print_usage(stderr); return EXIT_FAILURE; } break; case 'i': - hdr_ip_proto = strtol(optarg, &p, 10); - if (p == optarg || *p != '\0' || hdr_ip_proto < 0 || + hdr_ip_proto = strtol(ws_optarg, &p, 10); + if (p == ws_optarg || *p != '\0' || hdr_ip_proto < 0 || hdr_ip_proto > 255) { - fprintf(stderr, "Bad argument for '-i': %s\n", optarg); + fprintf(stderr, "Bad argument for '-i': %s\n", ws_optarg); print_usage(stderr); return EXIT_FAILURE; } @@ -1518,8 +1507,8 @@ parse_options (int argc, char *argv[]) hdr_data_chunk = FALSE; hdr_tcp = FALSE; hdr_udp = FALSE; - hdr_sctp_src = (guint32)strtol(optarg, &p, 10); - if (p == optarg || (*p != ',' && *p != '\0')) { + hdr_sctp_src = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || (*p != ',' && *p != '\0')) { fprintf(stderr, "Bad src port for '-%c'\n", c); print_usage(stderr); return EXIT_FAILURE; @@ -1530,9 +1519,9 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } p++; - optarg = p; - hdr_sctp_dest = (guint32)strtol(optarg, &p, 10); - if (p == optarg || (*p != ',' && *p != '\0')) { + ws_optarg = p; + hdr_sctp_dest = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || (*p != ',' && *p != '\0')) { fprintf(stderr, "Bad dest port for '-s'\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1543,9 +1532,9 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } p++; - optarg = p; - hdr_sctp_tag = (guint32)strtol(optarg, &p, 10); - if (p == optarg || *p != '\0') { + ws_optarg = p; + hdr_sctp_tag = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || *p != '\0') { fprintf(stderr, "Bad tag for '-%c'\n", c); print_usage(stderr); return EXIT_FAILURE; @@ -1559,8 +1548,8 @@ parse_options (int argc, char *argv[]) hdr_data_chunk = TRUE; hdr_tcp = FALSE; hdr_udp = FALSE; - hdr_sctp_src = (guint32)strtol(optarg, &p, 10); - if (p == optarg || (*p != ',' && *p != '\0')) { + hdr_sctp_src = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || (*p != ',' && *p != '\0')) { fprintf(stderr, "Bad src port for '-%c'\n", c); print_usage(stderr); return EXIT_FAILURE; @@ -1571,9 +1560,9 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } p++; - optarg = p; - hdr_sctp_dest = (guint32)strtol(optarg, &p, 10); - if (p == optarg || (*p != ',' && *p != '\0')) { + ws_optarg = p; + hdr_sctp_dest = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || (*p != ',' && *p != '\0')) { fprintf(stderr, "Bad dest port for '-s'\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1584,9 +1573,9 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } p++; - optarg = p; - hdr_data_chunk_ppid = (guint32)strtoul(optarg, &p, 10); - if (p == optarg || *p != '\0') { + ws_optarg = p; + hdr_data_chunk_ppid = (guint32)strtoul(ws_optarg, &p, 10); + if (p == ws_optarg || *p != '\0') { fprintf(stderr, "Bad ppi for '-%c'\n", c); print_usage(stderr); return EXIT_FAILURE; @@ -1597,7 +1586,7 @@ parse_options (int argc, char *argv[]) break; case 't': - ts_fmt = optarg; + ts_fmt = ws_optarg; break; case 'u': @@ -1605,8 +1594,8 @@ parse_options (int argc, char *argv[]) hdr_tcp = FALSE; hdr_sctp = FALSE; hdr_data_chunk = FALSE; - hdr_src_port = (guint32)strtol(optarg, &p, 10); - if (p == optarg || (*p != ',' && *p != '\0')) { + hdr_src_port = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || (*p != ',' && *p != '\0')) { fprintf(stderr, "Bad src port for '-u'\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1617,9 +1606,9 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } p++; - optarg = p; - hdr_dest_port = (guint32)strtol(optarg, &p, 10); - if (p == optarg || *p != '\0') { + ws_optarg = p; + hdr_dest_port = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || *p != '\0') { fprintf(stderr, "Bad dest port for '-u'\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1633,8 +1622,8 @@ parse_options (int argc, char *argv[]) hdr_udp = FALSE; hdr_sctp = FALSE; hdr_data_chunk = FALSE; - hdr_src_port = (guint32)strtol(optarg, &p, 10); - if (p == optarg || (*p != ',' && *p != '\0')) { + hdr_src_port = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || (*p != ',' && *p != '\0')) { fprintf(stderr, "Bad src port for '-T'\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1645,9 +1634,9 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } p++; - optarg = p; - hdr_dest_port = (guint32)strtol(optarg, &p, 10); - if (p == optarg || *p != '\0') { + ws_optarg = p; + hdr_dest_port = (guint32)strtol(ws_optarg, &p, 10); + if (p == ws_optarg || *p != '\0') { fprintf(stderr, "Bad dest port for '-T'\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1667,7 +1656,7 @@ parse_options (int argc, char *argv[]) case '4': case '6': - p = strchr(optarg, ','); + p = strchr(ws_optarg, ','); if (!p) { fprintf(stderr, "Bad source param addr for '-%c'\n", c); @@ -1689,13 +1678,13 @@ parse_options (int argc, char *argv[]) hdr_ethernet = TRUE; if (hdr_ipv6 == TRUE) { - if (!ws_inet_pton6(optarg, &hdr_ipv6_src_addr)) { + if (!ws_inet_pton6(ws_optarg, &hdr_ipv6_src_addr)) { fprintf(stderr, "Bad src addr -%c '%s'\n", c, p); print_usage(stderr); return EXIT_FAILURE; } } else { - if (!ws_inet_pton4(optarg, &hdr_ip_src_addr)) { + if (!ws_inet_pton4(ws_optarg, &hdr_ip_src_addr)) { fprintf(stderr, "Bad src addr -%c '%s'\n", c, p); print_usage(stderr); return EXIT_FAILURE; @@ -1732,7 +1721,7 @@ parse_options (int argc, char *argv[]) } } - if (optind >= argc || argc-optind < 2) { + if (ws_optind >= argc || argc-ws_optind < 2) { fprintf(stderr, "Must specify input and output filename\n"); print_usage(stderr); return EXIT_FAILURE; @@ -1744,8 +1733,8 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } - if (strcmp(argv[optind], "-") != 0) { - input_filename = argv[optind]; + if (strcmp(argv[ws_optind], "-") != 0) { + input_filename = argv[ws_optind]; input_file = ws_fopen(input_filename, "rb"); if (!input_file) { fprintf(stderr, "Cannot open file [%s] for reading: %s\n", @@ -1757,9 +1746,9 @@ parse_options (int argc, char *argv[]) input_file = stdin; } - if (strcmp(argv[optind+1], "-") != 0) { + if (strcmp(argv[ws_optind+1], "-") != 0) { /* Write to a file. Open the file, in binary mode. */ - output_filename = argv[optind+1]; + output_filename = argv[ws_optind+1]; output_file = ws_fopen(output_filename, "wb"); if (!output_file) { fprintf(stderr, "Cannot open file [%s] for writing: %s\n", diff --git a/tfshark.c b/tfshark.c index a87149387f..d5f717f793 100644 --- a/tfshark.c +++ b/tfshark.c @@ -19,18 +19,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -402,20 +391,20 @@ main(int argc, char *argv[]) * arguments we can't handle until after initializing libwireshark, * and then process them after initializing libwireshark? */ - opterr = 0; + ws_opterr = 0; - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case 'C': /* Configuration Profile */ - if (profile_exists (optarg, FALSE)) { - set_profile_name (optarg); + if (profile_exists (ws_optarg, FALSE)) { + set_profile_name (ws_optarg); } else { - cmdarg_err("Configuration Profile \"%s\" does not exist", optarg); + cmdarg_err("Configuration Profile \"%s\" does not exist", ws_optarg); return 1; } break; case 'O': /* Only output these protocols */ - output_only = g_strdup(optarg); + output_only = g_strdup(ws_optarg); /* FALLTHROUGH */ case 'V': /* Verbose */ print_details = TRUE; @@ -429,7 +418,7 @@ main(int argc, char *argv[]) print_packet_info = TRUE; break; case 'X': - ex_opt_add(optarg); + ex_opt_add(ws_optarg); break; default: break; @@ -555,29 +544,17 @@ main(int argc, char *argv[]) output_fields = output_fields_new(); /* - * To reset the options parser, set optreset to 1 on platforms that - * have optreset (documented in *BSD and macOS, apparently present but - * not documented in Solaris - the Illumos repository seems to - * suggest that the first Solaris getopt_long(), at least as of 2004, - * was based on the NetBSD one, it had optreset) and set optind to 1, - * and set optind to 0 otherwise (documented as working in the GNU - * getopt_long(). Setting optind to 0 didn't originally work in the - * NetBSD one, but that was added later - we don't want to depend on - * it if we have optreset). + * To reset the options parser, set ws_optreset to 1 and set ws_optind to 1. * - * Also reset opterr to 1, so that error messages are printed by + * Also reset ws_opterr to 1, so that error messages are printed by * getopt_long(). */ -#ifdef HAVE_OPTRESET - optreset = 1; - optind = 1; -#else - optind = 0; -#endif - opterr = 1; + ws_optreset = 1; + ws_optind = 1; + ws_opterr = 1; /* Now get our args */ - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case '2': /* Perform two pass analysis */ perform_two_pass_analysis = TRUE; @@ -587,12 +564,12 @@ main(int argc, char *argv[]) break; case 'e': /* Field entry */ - output_fields_add(output_fields, optarg); + output_fields_add(output_fields, ws_optarg); break; case 'E': /* Field option */ - if (!output_fields_set_option(output_fields, optarg)) { - cmdarg_err("\"%s\" is not a valid field output option=value pair.", optarg); + if (!output_fields_set_option(output_fields, ws_optarg)) { + cmdarg_err("\"%s\" is not a valid field output option=value pair.", ws_optarg); output_fields_list_options(stderr); exit_status = INVALID_OPTION; goto clean_exit; @@ -634,13 +611,13 @@ main(int argc, char *argv[]) { char *errmsg = NULL; - switch (prefs_set_pref(optarg, &errmsg)) { + switch (prefs_set_pref(ws_optarg, &errmsg)) { case PREFS_SET_OK: break; case PREFS_SET_SYNTAX_ERR: - cmdarg_err("Invalid -o flag \"%s\"%s%s", optarg, + cmdarg_err("Invalid -o flag \"%s\"%s%s", ws_optarg, errmsg ? ": " : "", errmsg ? errmsg : ""); g_free(errmsg); return 1; @@ -648,7 +625,7 @@ main(int argc, char *argv[]) case PREFS_SET_NO_SUCH_PREF: case PREFS_SET_OBSOLETE: - cmdarg_err("-o flag \"%s\" specifies unknown preference", optarg); + cmdarg_err("-o flag \"%s\" specifies unknown preference", ws_optarg); exit_status = INVALID_OPTION; goto clean_exit; break; @@ -663,35 +640,35 @@ main(int argc, char *argv[]) really_quiet = TRUE; break; case 'r': /* Read capture file x */ - cf_name = g_strdup(optarg); + cf_name = g_strdup(ws_optarg); break; case 'R': /* Read file filter */ - rfilter = optarg; + rfilter = ws_optarg; break; case 'S': /* Set the line Separator to be printed between packets */ - separator = g_strdup(optarg); + separator = g_strdup(ws_optarg); break; case 'T': /* printing Type */ - if (strcmp(optarg, "text") == 0) { + if (strcmp(ws_optarg, "text") == 0) { output_action = WRITE_TEXT; print_format = PR_FMT_TEXT; - } else if (strcmp(optarg, "ps") == 0) { + } else if (strcmp(ws_optarg, "ps") == 0) { output_action = WRITE_TEXT; print_format = PR_FMT_PS; - } else if (strcmp(optarg, "pdml") == 0) { + } else if (strcmp(ws_optarg, "pdml") == 0) { output_action = WRITE_XML; print_details = TRUE; /* Need details */ print_summary = FALSE; /* Don't allow summary */ - } else if (strcmp(optarg, "psml") == 0) { + } else if (strcmp(ws_optarg, "psml") == 0) { output_action = WRITE_XML; print_details = FALSE; /* Don't allow details */ print_summary = TRUE; /* Need summary */ - } else if (strcmp(optarg, "fields") == 0) { + } else if (strcmp(ws_optarg, "fields") == 0) { output_action = WRITE_FIELDS; print_details = TRUE; /* Need full tree info */ print_summary = FALSE; /* Don't allow summary */ } else { - cmdarg_err("Invalid -T parameter \"%s\"; it must be one of:", optarg); /* x */ + cmdarg_err("Invalid -T parameter \"%s\"; it must be one of:", ws_optarg); /* x */ cmdarg_err_cont("\t\"fields\" The values of fields specified with the -e option, in a form\n" "\t specified by the -E option.\n" "\t\"pdml\" Packet Details Markup Language, an XML-based format for the\n" @@ -728,7 +705,7 @@ main(int argc, char *argv[]) /* already processed; just ignore it now */ break; case 'Y': - dfilter = optarg; + dfilter = ws_optarg; break; case 'z': /* We won't call the init function for the stat this soon @@ -736,13 +713,13 @@ main(int argc, char *argv[]) by the preferences set callback) from being used as part of a tap filter. Instead, we just add the argument to a list of stat arguments. */ - if (strcmp("help", optarg) == 0) { + if (strcmp("help", ws_optarg) == 0) { fprintf(stderr, "tfshark: The available statistics for the \"-z\" option are:\n"); list_stat_cmd_args(); goto clean_exit; } - if (!process_stat_cmd_arg(optarg)) { - cmdarg_err("Invalid -z argument \"%s\"; it must be one of:", optarg); + if (!process_stat_cmd_arg(ws_optarg)) { + cmdarg_err("Invalid -z argument \"%s\"; it must be one of:", ws_optarg); list_stat_cmd_args(); exit_status = INVALID_OPTION; goto clean_exit; @@ -756,7 +733,7 @@ main(int argc, char *argv[]) case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */ case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */ case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disabled by default) */ - if (!dissect_opts_handle_opt(opt, optarg)) { + if (!dissect_opts_handle_opt(opt, ws_optarg)) { exit_status = INVALID_OPTION; goto clean_exit; } @@ -792,14 +769,14 @@ main(int argc, char *argv[]) /* If no display filter has been specified, and there are still command- line arguments, treat them as the tokens of a display filter. */ - if (optind < argc) { + if (ws_optind < argc) { if (dfilter != NULL) { cmdarg_err("Display filters were specified both with \"-Y\" " "and with additional command-line arguments."); exit_status = INVALID_OPTION; goto clean_exit; } - dfilter = get_args_as_string(argc, argv, optind); + dfilter = get_args_as_string(argc, argv, ws_optind); } /* if "-q" wasn't specified, we should print packet information */ diff --git a/tools/pre-commit-ignore.conf b/tools/pre-commit-ignore.conf index 66789c4c7e..db95fb0a0c 100644 --- a/tools/pre-commit-ignore.conf +++ b/tools/pre-commit-ignore.conf @@ -23,5 +23,4 @@ mmdbresolve.c tools/lemon/* wsutil/file_util.h wsutil/strptime.c -wsutil/getopt_long.c epan/dissectors/packet-http.c diff --git a/tshark.c b/tshark.c index d4d8df5a4b..5442ca831e 100644 --- a/tshark.c +++ b/tshark.c @@ -20,18 +20,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -852,21 +841,21 @@ main(int argc, char *argv[]) * arguments we can't handle until after initializing libwireshark, * and then process them after initializing libwireshark? */ - opterr = 0; + ws_opterr = 0; - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case 'C': /* Configuration Profile */ - if (profile_exists (optarg, FALSE)) { - set_profile_name (optarg); + if (profile_exists (ws_optarg, FALSE)) { + set_profile_name (ws_optarg); } else { - cmdarg_err("Configuration Profile \"%s\" does not exist", optarg); + cmdarg_err("Configuration Profile \"%s\" does not exist", ws_optarg); exit_status = INVALID_OPTION; goto clean_exit; } break; case 'G': - if (g_str_has_suffix(optarg, "prefs") || strcmp(optarg, "folders") == 0) { + if (g_str_has_suffix(ws_optarg, "prefs") || strcmp(ws_optarg, "folders") == 0) { has_extcap_options = TRUE; } break; @@ -874,7 +863,7 @@ main(int argc, char *argv[]) has_extcap_options = TRUE; break; case 'o': - if (g_str_has_prefix(optarg, "extcap.")) { + if (g_str_has_prefix(ws_optarg, "extcap.")) { has_extcap_options = TRUE; } break; @@ -883,10 +872,10 @@ main(int argc, char *argv[]) print_summary = TRUE; break; case 'r': /* Read capture file x */ - cf_name = g_strdup(optarg); + cf_name = g_strdup(ws_optarg); break; case 'O': /* Only output these protocols */ - output_only = g_strdup(optarg); + output_only = g_strdup(ws_optarg); /* FALLTHROUGH */ case 'V': /* Verbose */ print_details = TRUE; @@ -900,10 +889,10 @@ main(int argc, char *argv[]) print_packet_info = TRUE; break; case 'X': - ex_opt_add(optarg); + ex_opt_add(ws_optarg); break; case LONGOPT_ELASTIC_MAPPING_FILTER: - elastic_mapping_filter = optarg; + elastic_mapping_filter = ws_optarg; break; default: break; @@ -1058,29 +1047,17 @@ main(int argc, char *argv[]) output_fields = output_fields_new(); /* - * To reset the options parser, set optreset to 1 on platforms that - * have optreset (documented in *BSD and macOS, apparently present but - * not documented in Solaris - the Illumos repository seems to - * suggest that the first Solaris getopt_long(), at least as of 2004, - * was based on the NetBSD one, it had optreset) and set optind to 1, - * and set optind to 0 otherwise (documented as working in the GNU - * getopt_long(). Setting optind to 0 didn't originally work in the - * NetBSD one, but that was added later - we don't want to depend on - * it if we have optreset). + * To reset the options parser, set ws_optreset to 1 and set ws_optind to 1. * - * Also reset opterr to 1, so that error messages are printed by + * Also reset ws_opterr to 1, so that error messages are printed by * getopt_long(). */ -#ifdef HAVE_OPTRESET - optreset = 1; - optind = 1; -#else - optind = 0; -#endif - opterr = 1; + ws_optreset = 1; + ws_optind = 1; + ws_opterr = 1; /* Now get our args */ - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case '2': /* Perform two pass analysis */ if(epan_auto_reset){ @@ -1094,7 +1071,7 @@ main(int argc, char *argv[]) cmdarg_err("-M does not support two pass analysis."); arg_error=TRUE; } - epan_auto_reset_count = get_positive_int(optarg, "epan reset count"); + epan_auto_reset_count = get_positive_int(ws_optarg, "epan reset count"); epan_auto_reset = TRUE; break; case 'a': /* autostop criteria */ @@ -1118,7 +1095,7 @@ main(int argc, char *argv[]) case LONGOPT_COMPRESS_TYPE: /* compress type */ /* These are options only for packet capture. */ #ifdef HAVE_LIBPCAP - exit_status = capture_opts_add_opt(&global_capture_opts, opt, optarg); + exit_status = capture_opts_add_opt(&global_capture_opts, opt, ws_optarg); if (exit_status != 0) { goto clean_exit; } @@ -1129,18 +1106,18 @@ main(int argc, char *argv[]) break; case 'c': /* Stop after x packets */ #ifdef HAVE_LIBPCAP - exit_status = capture_opts_add_opt(&global_capture_opts, opt, optarg); + exit_status = capture_opts_add_opt(&global_capture_opts, opt, ws_optarg); if (exit_status != 0) { goto clean_exit; } #else - max_packet_count = get_positive_int(optarg, "packet count"); + max_packet_count = get_positive_int(ws_optarg, "packet count"); #endif break; case 'w': /* Write to file x */ - output_file_name = g_strdup(optarg); + output_file_name = g_strdup(ws_optarg); #ifdef HAVE_LIBPCAP - exit_status = capture_opts_add_opt(&global_capture_opts, opt, optarg); + exit_status = capture_opts_add_opt(&global_capture_opts, opt, ws_optarg); if (exit_status != 0) { goto clean_exit; } @@ -1173,21 +1150,21 @@ main(int argc, char *argv[]) break; case 'e': /* Field entry */ - output_fields_add(output_fields, optarg); + output_fields_add(output_fields, ws_optarg); break; case 'E': /* Field option */ - if (!output_fields_set_option(output_fields, optarg)) { - cmdarg_err("\"%s\" is not a valid field output option=value pair.", optarg); + if (!output_fields_set_option(output_fields, ws_optarg)) { + cmdarg_err("\"%s\" is not a valid field output option=value pair.", ws_optarg); output_fields_list_options(stderr); exit_status = INVALID_OPTION; goto clean_exit; } break; case 'F': - out_file_type = wtap_name_to_file_type_subtype(optarg); + out_file_type = wtap_name_to_file_type_subtype(ws_optarg); if (out_file_type < 0) { - cmdarg_err("\"%s\" isn't a valid capture file type", optarg); + cmdarg_err("\"%s\" isn't a valid capture file type", ws_optarg); list_capture_types(); exit_status = INVALID_OPTION; goto clean_exit; @@ -1197,30 +1174,30 @@ main(int argc, char *argv[]) if (protocolfilter) { cmdarg_err("-j or -J was already specified! Overwriting previous protocol filter"); } - protocolfilter = wmem_strsplit(wmem_epan_scope(), optarg, " ", -1); + protocolfilter = wmem_strsplit(wmem_epan_scope(), ws_optarg, " ", -1); break; case 'J': if (protocolfilter) { cmdarg_err("-j or -J was already specified! Overwriting previous protocol filter"); } protocolfilter_flags = PF_INCLUDE_CHILDREN; - protocolfilter = wmem_strsplit(wmem_epan_scope(), optarg, " ", -1); + protocolfilter = wmem_strsplit(wmem_epan_scope(), ws_optarg, " ", -1); break; case 'W': /* Select extra information to save in our capture file */ /* This is patterned after the -N flag which may not be the best idea. */ - if (strchr(optarg, 'n')) { + if (strchr(ws_optarg, 'n')) { out_file_name_res = TRUE; } else { - cmdarg_err("Invalid -W argument \"%s\"; it must be one of:", optarg); + cmdarg_err("Invalid -W argument \"%s\"; it must be one of:", ws_optarg); cmdarg_err_cont("\t'n' write network address resolution information (pcapng only)"); exit_status = INVALID_OPTION; goto clean_exit; } break; case 'H': /* Read address to name mappings from a hosts file */ - if (! add_hosts_file(optarg)) + if (! add_hosts_file(ws_optarg)) { - cmdarg_err("Can't read host entries from \"%s\"", optarg); + cmdarg_err("Can't read host entries from \"%s\"", ws_optarg); exit_status = INVALID_OPTION; goto clean_exit; } @@ -1279,13 +1256,13 @@ main(int argc, char *argv[]) { char *errmsg = NULL; - switch (prefs_set_pref(optarg, &errmsg)) { + switch (prefs_set_pref(ws_optarg, &errmsg)) { case PREFS_SET_OK: break; case PREFS_SET_SYNTAX_ERR: - cmdarg_err("Invalid -o flag \"%s\"%s%s", optarg, + cmdarg_err("Invalid -o flag \"%s\"%s%s", ws_optarg, errmsg ? ": " : "", errmsg ? errmsg : ""); g_free(errmsg); exit_status = INVALID_OPTION; @@ -1294,7 +1271,7 @@ main(int argc, char *argv[]) case PREFS_SET_NO_SUCH_PREF: case PREFS_SET_OBSOLETE: - cmdarg_err("-o flag \"%s\" specifies unknown preference", optarg); + cmdarg_err("-o flag \"%s\" specifies unknown preference", ws_optarg); exit_status = INVALID_OPTION; goto clean_exit; break; @@ -1312,13 +1289,13 @@ main(int argc, char *argv[]) /* already processed; just ignore it now */ break; case 'R': /* Read file filter */ - rfilter = optarg; + rfilter = ws_optarg; break; case 'P': /* already processed; just ignore it now */ break; case 'S': /* Set the line Separator to be printed between packets */ - separator = optarg; + separator = ws_optarg; break; case 'T': /* printing Type */ /* output_action has been already set. It means multiple -T. */ @@ -1328,43 +1305,43 @@ main(int argc, char *argv[]) goto clean_exit; } print_packet_info = TRUE; - if (strcmp(optarg, "text") == 0) { + if (strcmp(ws_optarg, "text") == 0) { output_action = WRITE_TEXT; print_format = PR_FMT_TEXT; - } else if (strcmp(optarg, "tabs") == 0) { + } else if (strcmp(ws_optarg, "tabs") == 0) { output_action = WRITE_TEXT; print_format = PR_FMT_TEXT; delimiter_char = "\t"; - } else if (strcmp(optarg, "ps") == 0) { + } else if (strcmp(ws_optarg, "ps") == 0) { output_action = WRITE_TEXT; print_format = PR_FMT_PS; - } else if (strcmp(optarg, "pdml") == 0) { + } else if (strcmp(ws_optarg, "pdml") == 0) { output_action = WRITE_XML; print_details = TRUE; /* Need details */ print_summary = FALSE; /* Don't allow summary */ - } else if (strcmp(optarg, "psml") == 0) { + } else if (strcmp(ws_optarg, "psml") == 0) { output_action = WRITE_XML; print_details = FALSE; /* Don't allow details */ print_summary = TRUE; /* Need summary */ - } else if (strcmp(optarg, "fields") == 0) { + } else if (strcmp(ws_optarg, "fields") == 0) { output_action = WRITE_FIELDS; print_details = TRUE; /* Need full tree info */ print_summary = FALSE; /* Don't allow summary */ - } else if (strcmp(optarg, "json") == 0) { + } else if (strcmp(ws_optarg, "json") == 0) { output_action = WRITE_JSON; print_details = TRUE; /* Need details */ print_summary = FALSE; /* Don't allow summary */ - } else if (strcmp(optarg, "ek") == 0) { + } else if (strcmp(ws_optarg, "ek") == 0) { output_action = WRITE_EK; if (!print_summary) print_details = TRUE; - } else if (strcmp(optarg, "jsonraw") == 0) { + } else if (strcmp(ws_optarg, "jsonraw") == 0) { output_action = WRITE_JSON_RAW; print_details = TRUE; /* Need details */ print_summary = FALSE; /* Don't allow summary */ } else { - cmdarg_err("Invalid -T parameter \"%s\"; it must be one of:", optarg); /* x */ + cmdarg_err("Invalid -T parameter \"%s\"; it must be one of:", ws_optarg); /* x */ cmdarg_err_cont("\t\"fields\" The values of fields specified with the -e option, in a form\n" "\t specified by the -E option.\n" "\t\"pdml\" Packet Details Markup Language, an XML-based format for the\n" @@ -1398,12 +1375,12 @@ main(int argc, char *argv[]) } break; case 'U': /* Export PDUs to file */ - if (strcmp(optarg, "") == 0 || strcmp(optarg, "?") == 0) { + if (strcmp(ws_optarg, "") == 0 || strcmp(ws_optarg, "?") == 0) { list_export_pdu_taps(); exit_status = INVALID_OPTION; goto clean_exit; } - pdu_export_arg = g_strdup(optarg); + pdu_export_arg = g_strdup(ws_optarg); break; case 'v': /* Show version and exit */ show_version(); @@ -1429,7 +1406,7 @@ main(int argc, char *argv[]) /* already processed; just ignore it now */ break; case 'Y': - dfilter = g_strdup(optarg); + dfilter = g_strdup(ws_optarg); break; case 'z': /* We won't call the init function for the stat this soon @@ -1437,14 +1414,14 @@ main(int argc, char *argv[]) by the preferences set callback) from being used as part of a tap filter. Instead, we just add the argument to a list of stat arguments. */ - if (strcmp("help", optarg) == 0) { + if (strcmp("help", ws_optarg) == 0) { fprintf(stderr, "tshark: The available statistics for the \"-z\" option are:\n"); list_stat_cmd_args(); exit_status = EXIT_SUCCESS; goto clean_exit; } - if (!process_stat_cmd_arg(optarg)) { - cmdarg_err("Invalid -z argument \"%s\"; it must be one of:", optarg); + if (!process_stat_cmd_arg(ws_optarg)) { + cmdarg_err("Invalid -z argument \"%s\"; it must be one of:", ws_optarg); list_stat_cmd_args(); exit_status = INVALID_OPTION; goto clean_exit; @@ -1460,25 +1437,25 @@ main(int argc, char *argv[]) case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */ case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */ case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disabled by default) */ - if (!dissect_opts_handle_opt(opt, optarg)) { + if (!dissect_opts_handle_opt(opt, ws_optarg)) { exit_status = INVALID_OPTION; goto clean_exit; } break; case LONGOPT_EXPORT_OBJECTS: /* --export-objects */ - if (strcmp("help", optarg) == 0) { + if (strcmp("help", ws_optarg) == 0) { fprintf(stderr, "tshark: The available export object types for the \"--export-objects\" option are:\n"); eo_list_object_types(); exit_status = EXIT_SUCCESS; goto clean_exit; } - if (!eo_tap_opt_add(optarg)) { + if (!eo_tap_opt_add(ws_optarg)) { exit_status = INVALID_OPTION; goto clean_exit; } break; case LONGOPT_EXPORT_TLS_SESSION_KEYS: /* --export-tls-session-keys */ - tls_session_keys_file = optarg; + tls_session_keys_file = ws_optarg; break; case LONGOPT_COLOR: /* print in color where appropriate */ dissect_color = TRUE; @@ -1491,11 +1468,11 @@ main(int argc, char *argv[]) if (capture_comments == NULL) { capture_comments = g_ptr_array_new_with_free_func(g_free); } - g_ptr_array_add(capture_comments, g_strdup(optarg)); + g_ptr_array_add(capture_comments, g_strdup(ws_optarg)); break; default: case '?': /* Bad flag - print usage message */ - switch(optopt) { + switch(ws_optopt) { case 'F': list_capture_types(); break; @@ -1556,7 +1533,7 @@ main(int argc, char *argv[]) still command-line arguments, treat them as the tokens of a capture filter (if no "-r" flag was specified) or a display filter (if a "-r" flag was specified. */ - if (optind < argc) { + if (ws_optind < argc) { if (cf_name != NULL) { if (dfilter != NULL) { cmdarg_err("Display filters were specified both with \"-Y\" " @@ -1564,7 +1541,7 @@ main(int argc, char *argv[]) exit_status = INVALID_OPTION; goto clean_exit; } - dfilter = get_args_as_string(argc, argv, optind); + dfilter = get_args_as_string(argc, argv, ws_optind); } else { #ifdef HAVE_LIBPCAP guint i; @@ -1579,7 +1556,7 @@ main(int argc, char *argv[]) interface_options *interface_opts; interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, i); if (interface_opts->cfilter == NULL) { - interface_opts->cfilter = get_args_as_string(argc, argv, optind); + interface_opts->cfilter = get_args_as_string(argc, argv, ws_optind); } else { cmdarg_err("A capture filter was specified both with \"-f\"" " and with additional command-line arguments."); @@ -1587,7 +1564,7 @@ main(int argc, char *argv[]) goto clean_exit; } } - global_capture_opts.default_options.cfilter = get_args_as_string(argc, argv, optind); + global_capture_opts.default_options.cfilter = get_args_as_string(argc, argv, ws_optind); #else capture_option_specified = TRUE; #endif diff --git a/ui/cli/tap-exportobject.h b/ui/cli/tap-exportobject.h index 180eca9f84..10bde6f860 100644 --- a/ui/cli/tap-exportobject.h +++ b/ui/cli/tap-exportobject.h @@ -17,7 +17,7 @@ extern "C" { void eo_list_object_types(void); /* will be called by main each time a --export-objects option is found */ -gboolean eo_tap_opt_add(const char *optarg); +gboolean eo_tap_opt_add(const char *ws_optarg); void start_exportobjects(void); diff --git a/ui/commandline.c b/ui/commandline.c index 9113f2ec49..e953cbb031 100644 --- a/ui/commandline.c +++ b/ui/commandline.c @@ -16,18 +16,7 @@ #include #include -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif +#include #include @@ -247,7 +236,7 @@ void commandline_early_options(int argc, char *argv[]) * arguments have not been removed from the argument list; those arguments * begin with "--", and will be treated as an error by getopt_long(). * - * We thus ignore errors - *and* set "opterr" to 0 to suppress the + * We thus ignore errors - *and* set "ws_opterr" to 0 to suppress the * error messages. * * In order to handle, for example, -o options, we also need to call it @@ -263,18 +252,18 @@ void commandline_early_options(int argc, char *argv[]) * GUI is up, as that can take a while, and we want a window of some * sort up to show progress while that's happening. */ - opterr = 0; + ws_opterr = 0; #ifndef HAVE_LIBPCAP capture_option_specified = FALSE; #endif - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case 'C': /* Configuration Profile */ - if (profile_exists (optarg, FALSE)) { - set_profile_name (optarg); + if (profile_exists (ws_optarg, FALSE)) { + set_profile_name (ws_optarg); } else { - cmdarg_err("Configuration Profile \"%s\" does not exist", optarg); + cmdarg_err("Configuration Profile \"%s\" does not exist", ws_optarg); exit(1); } break; @@ -309,13 +298,13 @@ void commandline_early_options(int argc, char *argv[]) break; #ifdef _WIN32 case 'i': - if (strcmp(optarg, "-") == 0) + if (strcmp(ws_optarg, "-") == 0) set_stdin_capture(TRUE); break; #endif case 'P': /* Personal file directory path settings - change these before the Preferences and alike are processed */ - if (!persfilepath_opt(opt, optarg)) { - cmdarg_err("-P flag \"%s\" failed (hint: is it quoted and existing?)", optarg); + if (!persfilepath_opt(opt, ws_optarg)) { + cmdarg_err("-P flag \"%s\" failed (hint: is it quoted and existing?)", ws_optarg); exit(EXIT_SUCCESS); } break; @@ -335,7 +324,7 @@ void commandline_early_options(int argc, char *argv[]) * we call epan_init() as they are supposed to be used by dissectors * or taps very early in the registration process. */ - ex_opt_add(optarg); + ex_opt_add(ws_optarg); break; case '?': /* Ignore errors - the "real" scan will catch them. */ break; @@ -370,23 +359,15 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) #endif /* - * To reset the options parser, set optreset to 1 on platforms that - * have optreset (documented in *BSD and macOS, apparently present but - * not documented in Solaris - the Illumos repository seems to - * suggest that the first Solaris getopt_long(), at least as of 2004, - * was based on the NetBSD one, it had optreset) and set optind to 1, - * and set optind to 0 otherwise (documented as working in the GNU - * getopt_long(). Setting optind to 0 didn't originally work in the - * NetBSD one, but that was added later - we don't want to depend on - * it if we have optreset). + * To reset the options parser, set ws_optreset to 1 and set ws_optind to 1. * - * Also reset opterr to 1, so that error messages are printed by + * Also reset ws_opterr to 1, so that error messages are printed by * getopt_long(). * * XXX - if we want to control all the command-line option errors, so * that we can display them where we choose (e.g., in a window), we'd - * want to leave opterr as 0, and produce our own messages using optopt. - * We'd have to check the value of optopt to see if it's a valid option + * want to leave ws_opterr as 0, and produce our own messages using ws_optopt. + * We'd have to check the value of ws_optopt to see if it's a valid option * letter, in which case *presumably* the error is "this option requires * an argument but none was specified", or not a valid option letter, * in which case *presumably* the error is "this option isn't valid". @@ -396,13 +377,9 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) * not all do. But we're now using getopt_long() - what does it do? */ if (opt_reset) { -#ifdef HAVE_OPTRESET - optreset = 1; - optind = 1; -#else - optind = 0; -#endif - opterr = 1; + ws_optreset = 1; + ws_optind = 1; + ws_opterr = 1; } /* Initialize with default values */ @@ -423,7 +400,7 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) global_commandline_info.full_screen = FALSE; global_commandline_info.user_opts = NULL; - while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { + while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { /*** capture option specific ***/ case 'a': /* autostop criteria */ @@ -448,7 +425,7 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) case 'B': /* Buffer size */ #endif #ifdef HAVE_LIBPCAP - status = capture_opts_add_opt(&global_capture_opts, opt, optarg); + status = capture_opts_add_opt(&global_capture_opts, opt, ws_optarg); if(status != 0) { exit_application(status); } @@ -466,10 +443,10 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) global_commandline_info.jump_backwards = SD_BACKWARD; break; case 'g': /* Go to packet with the given packet number */ - global_commandline_info.go_to_packet = get_nonzero_guint32(optarg, "go to packet"); + global_commandline_info.go_to_packet = get_nonzero_guint32(ws_optarg, "go to packet"); break; case 'J': /* Jump to the first packet which matches the filter criteria */ - global_commandline_info.jfilter = optarg; + global_commandline_info.jfilter = ws_optarg; break; case 'k': /* Start capture immediately */ #ifdef HAVE_LIBPCAP @@ -509,32 +486,32 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) { char *errmsg = NULL; - switch (prefs_set_pref(optarg, &errmsg)) { + switch (prefs_set_pref(ws_optarg, &errmsg)) { case PREFS_SET_OK: global_commandline_info.user_opts = g_slist_prepend(global_commandline_info.user_opts, - g_strdup(optarg)); + g_strdup(ws_optarg)); break; case PREFS_SET_SYNTAX_ERR: - cmdarg_err("Invalid -o flag \"%s\"%s%s", optarg, + cmdarg_err("Invalid -o flag \"%s\"%s%s", ws_optarg, errmsg ? ": " : "", errmsg ? errmsg : ""); g_free(errmsg); exit_application(1); break; case PREFS_SET_NO_SUCH_PREF: /* not a preference, might be a recent setting */ - switch (recent_set_arg(optarg)) { + switch (recent_set_arg(ws_optarg)) { case PREFS_SET_OK: break; case PREFS_SET_SYNTAX_ERR: /* shouldn't happen, checked already above */ - cmdarg_err("Invalid -o flag \"%s\"", optarg); + cmdarg_err("Invalid -o flag \"%s\"", ws_optarg); exit_application(1); break; case PREFS_SET_NO_SUCH_PREF: case PREFS_SET_OBSOLETE: cmdarg_err("-o flag \"%s\" specifies unknown preference/recent value", - optarg); + ws_optarg); exit_application(1); break; default: @@ -543,7 +520,7 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) break; case PREFS_SET_OBSOLETE: cmdarg_err("-o flag \"%s\" specifies obsolete preference", - optarg); + ws_optarg); exit_application(1); break; default: @@ -558,16 +535,16 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) /* We may set "last_open_dir" to "cf_name", and if we change "last_open_dir" later, we free the old value, so we have to set "cf_name" to something that's been allocated. */ - global_commandline_info.cf_name = g_strdup(optarg); + global_commandline_info.cf_name = g_strdup(ws_optarg); break; case 'R': /* Read file filter */ - global_commandline_info.rfilter = optarg; + global_commandline_info.rfilter = ws_optarg; break; case 'X': /* ext ops were already processed just ignore them this time*/ break; case 'Y': - global_commandline_info.dfilter = optarg; + global_commandline_info.dfilter = ws_optarg; break; case 'z': /* We won't call the init function for the stat this soon @@ -575,12 +552,12 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) by the preferences set callback) from being used as part of a tap filter. Instead, we just add the argument to a list of stat arguments. */ - if (strcmp("help", optarg) == 0) { + if (strcmp("help", ws_optarg) == 0) { fprintf(stderr, "wireshark: The available statistics for the \"-z\" option are:\n"); list_stat_cmd_args(); exit_application(0); } - if (!process_stat_cmd_arg(optarg)) { + if (!process_stat_cmd_arg(ws_optarg)) { cmdarg_err("Invalid -z argument."); cmdarg_err_cont(" -z argument must be one of :"); list_stat_cmd_args(); @@ -597,7 +574,7 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */ case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */ case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disabled by default) */ - if (!dissect_opts_handle_opt(opt, optarg)) + if (!dissect_opts_handle_opt(opt, ws_optarg)) exit_application(1); break; case LONGOPT_FULL_SCREEN: @@ -608,7 +585,7 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) if (global_commandline_info.capture_comments == NULL) { global_commandline_info.capture_comments = g_ptr_array_new_with_free_func(g_free); } - g_ptr_array_add(global_commandline_info.capture_comments, g_strdup(optarg)); + g_ptr_array_add(global_commandline_info.capture_comments, g_strdup(ws_optarg)); #else capture_option_specified = TRUE; arg_error = TRUE; @@ -627,8 +604,8 @@ void commandline_other_options(int argc, char *argv[], gboolean opt_reset) global_commandline_info.user_opts = g_slist_reverse(global_commandline_info.user_opts); if (!arg_error) { - argc -= optind; - argv += optind; + argc -= ws_optind; + argv += ws_optind; if (argc >= 1) { if (global_commandline_info.cf_name != NULL) { /* diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp index d5ec5191e9..63160327ac 100644 --- a/ui/qt/main.cpp +++ b/ui/qt/main.cpp @@ -21,19 +21,6 @@ #include "ui/win32/console_win32.h" #endif -/* - * If we have getopt_long() in the system library, include . - * Otherwise, we're using our own getopt_long() (either because the - * system has getopt() but not getopt_long(), as with some UN*Xes, - * or because it doesn't even have getopt(), as with Windows), so - * include our getopt_long()'s header. - */ -#ifdef HAVE_GETOPT_LONG -#include -#else -#include -#endif - #include #include #include diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index c26ff8c4c8..29c5d78049 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -72,6 +72,7 @@ set(WSUTIL_PUBLIC_HEADERS ws_assert.h ws_cpuid.h glib-compat.h + ws_getopt.h ws_mempbrk.h ws_mempbrk_int.h ws_pipe.h @@ -128,6 +129,8 @@ set(WSUTIL_COMMON_FILES unicode-utils.c glib-compat.c ws_assert.c + ws_getopt.c + ws_getopt_long.c ws_mempbrk.c ws_pipe.c wsgcrypt.c @@ -220,10 +223,6 @@ if(HAVE_SSE4_2) list(APPEND WSUTIL_FILES ws_mempbrk_sse42.c) endif() -if(NOT HAVE_GETOPT_LONG) - list(APPEND WSUTIL_FILES getopt_long.c) -endif() - if(NOT HAVE_STRPTIME) list(APPEND WSUTIL_FILES strptime.c) endif() diff --git a/wsutil/getopt_long.c b/wsutil/getopt_long.c deleted file mode 100644 index 6e4d0070a8..0000000000 --- a/wsutil/getopt_long.c +++ /dev/null @@ -1,1310 +0,0 @@ -/* - Copied from glibc-2.15 modified to compile on Windows. - - Getopt for GNU. - NOTE: getopt is part of the C library, so if you don't know what - "Keep this file name-space clean" means, talk to drepper@gnu.org - before changing it! - Copyright (C) 1987-1996,1998-2004,2008,2009,2010,2011 - Free Software Foundation, Inc. - This file is part of the GNU C Library. - - SPDX-License-Identifier: LGPL-2.0-or-later - */ - -/* This tells Alpha OSF/1 not to define a getopt prototype in . - Ditto for AIX 3.2 and . */ -#ifndef _NO_PROTO -# define _NO_PROTO -#endif - -#include "config.h" -#include "ws_attributes.h" - -#include - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#define GETOPT_INTERFACE_VERSION 2 -#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 -# include -# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION -# define ELIDE_CODE -# endif -#endif - -#ifndef ELIDE_CODE - - -/* This needs to come after some library #include - to get __GNU_LIBRARY__ defined. */ -#ifdef __GNU_LIBRARY__ -/* Don't include stdlib.h for non-GNU C libraries because some of them - contain conflicting prototypes for getopt. */ -# include -# include -#endif /* GNU C library. */ - -#include - -#ifdef VMS -# include -#endif - -#ifdef _LIBC -# include -#else -/*# include "gettext.h" */ -/*# define _(msgid) gettext (msgid) */ -# define _(msgid) (msgid) -#endif - -/* - * If we have alloca.h, we assume we need it. - */ -# -#ifdef HAVE_ALLOCA_H -# include -#endif - -#ifdef _WIN32 -# define alloca _alloca -#endif - -#if defined _LIBC -# include -#endif - -#ifndef attribute_hidden -# define attribute_hidden -#endif - -/* This version of `getopt' appears to the caller like standard Unix `getopt' - but it behaves differently for the user, since it allows the user - to intersperse the options with the other arguments. - - As `getopt' works, it permutes the elements of ARGV so that, - when it is done, all the options precede everything else. Thus - all application programs are extended to handle flexible argument order. - - Setting the environment variable POSIXLY_CORRECT disables permutation. - Then the behavior is completely standard. - - GNU application programs can use a third alternative mode in which - they can distinguish the relative order of options and other arguments. */ - -#include "wsgetopt.h" -#include "getopt_long.h" - -/* For communication from `getopt' to the caller. - When `getopt' finds an option that takes an argument, - the argument value is returned here. - Also, when `ordering' is RETURN_IN_ORDER, - each non-option ARGV-element is returned here. */ - -char *optarg; - -/* Index in ARGV of the next element to be scanned. - This is used for communication to and from the caller - and for communication between successive calls to `getopt'. - - On entry to `getopt', zero means this is the first call; initialize. - - When `getopt' returns -1, this is the index of the first of the - non-option elements that the caller should itself scan. - - Otherwise, `optind' communicates from one call to the next - how much of ARGV has been scanned so far. */ - -/* 1003.2 says this must be 1 before any call. */ -int optind = 1; - -/* Callers store zero here to inhibit the error message - for unrecognized options. */ - -int opterr = 1; - -/* Set to an option character which was unrecognized. - This must be initialized on some systems to avoid linking in the - system's own getopt implementation. */ - -int optopt = '?'; - -/* Keep a global copy of all internal members of getopt_data. */ - -static struct _getopt_data getopt_data; - -#ifndef __GNU_LIBRARY__ - -/* Avoid depending on library functions or files - whose names are inconsistent. */ - -#ifndef getenv -extern char *getenv (); -#endif - -#endif /* not __GNU_LIBRARY__ */ - -#ifdef _LIBC -/* Stored original parameters. - XXX This is no good solution. We should rather copy the args so - that we can compare them later. But we must not use malloc(3). */ -extern int __libc_argc; -extern char **__libc_argv; - -/* Bash 2.0 gives us an environment variable containing flags - indicating ARGV elements that should not be considered arguments. */ - -# ifdef USE_NONOPTION_FLAGS -/* Defined in getopt_init.c */ -extern char *__getopt_nonoption_flags; -# endif - -# ifdef USE_NONOPTION_FLAGS -# define SWAP_FLAGS(ch1, ch2) \ - if (d->__nonoption_flags_len > 0) \ - { \ - char __tmp = __getopt_nonoption_flags[ch1]; \ - __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ - __getopt_nonoption_flags[ch2] = __tmp; \ - } -# else -# define SWAP_FLAGS(ch1, ch2) -# endif -#else /* !_LIBC */ -# define SWAP_FLAGS(ch1, ch2) -#endif /* _LIBC */ - -/* Exchange two adjacent subsequences of ARGV. - One subsequence is elements [first_nonopt,last_nonopt) - which contains all the non-options that have been skipped so far. - The other is elements [last_nonopt,optind), which contains all - the options processed since those non-options were skipped. - - `first_nonopt' and `last_nonopt' are relocated so that they describe - the new indices of the non-options in ARGV after they are moved. */ - -static void -exchange (char **argv, struct _getopt_data *d) -{ - int bottom = d->__first_nonopt; - int middle = d->__last_nonopt; - int top = d->optind; - char *tem; - - /* Exchange the shorter segment with the far end of the longer segment. - That puts the shorter segment into the right place. - It leaves the longer segment in the right place overall, - but it consists of two parts that need to be swapped next. */ - -#if defined _LIBC && defined USE_NONOPTION_FLAGS - /* First make sure the handling of the `__getopt_nonoption_flags' - string can work normally. Our top argument must be in the range - of the string. */ - if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len) - { - /* We must extend the array. The user plays games with us and - presents new arguments. */ - char *new_str = malloc (top + 1); - if (new_str == NULL) - d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0; - else - { - memset (__mempcpy (new_str, __getopt_nonoption_flags, - d->__nonoption_flags_max_len), - '\0', top + 1 - d->__nonoption_flags_max_len); - d->__nonoption_flags_max_len = top + 1; - __getopt_nonoption_flags = new_str; - } - } -#endif - - while (top > middle && middle > bottom) - { - if (top - middle > middle - bottom) - { - /* Bottom segment is the short one. */ - int len = middle - bottom; - register int i; - - /* Swap it with the top part of the top segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[top - (middle - bottom) + i]; - argv[top - (middle - bottom) + i] = tem; - SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); - } - /* Exclude the moved bottom segment from further swapping. */ - top -= len; - } - else - { - /* Top segment is the short one. */ - int len = top - middle; - register int i; - - /* Swap it with the bottom part of the bottom segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[middle + i]; - argv[middle + i] = tem; - SWAP_FLAGS (bottom + i, middle + i); - } - /* Exclude the moved top segment from further swapping. */ - bottom += len; - } - } - - /* Update records for the slots the non-options now occupy. */ - - d->__first_nonopt += (d->optind - d->__last_nonopt); - d->__last_nonopt = d->optind; -} - -/* Initialize the internal data when the first call is made. */ - -static const char * -_getopt_initialize ( -#if defined _LIBC && defined USE_NONOPTION_FLAGS - int argc, char *const *argv, -#else - int argc _U_, char *const *argv _U_, -#endif - const char *optstring, - struct _getopt_data *d, int posixly_correct) -{ - /* Start processing options with ARGV-element 1 (since ARGV-element 0 - is the program name); the sequence of previously skipped - non-option ARGV-elements is empty. */ - - d->__first_nonopt = d->__last_nonopt = d->optind; - - d->__nextchar = NULL; - - d->__posixly_correct = posixly_correct | !!getenv ("POSIXLY_CORRECT"); - - /* Determine how to handle the ordering of options and nonoptions. */ - - if (optstring[0] == '-') - { - d->__ordering = RETURN_IN_ORDER; - ++optstring; - } - else if (optstring[0] == '+') - { - d->__ordering = REQUIRE_ORDER; - ++optstring; - } - else if (d->__posixly_correct) - d->__ordering = REQUIRE_ORDER; - else - d->__ordering = PERMUTE; - -#if defined _LIBC && defined USE_NONOPTION_FLAGS - if (!d->__posixly_correct - && argc == __libc_argc && argv == __libc_argv) - { - if (d->__nonoption_flags_max_len == 0) - { - if (__getopt_nonoption_flags == NULL - || __getopt_nonoption_flags[0] == '\0') - d->__nonoption_flags_max_len = -1; - else - { - const char *orig_str = __getopt_nonoption_flags; - int len = d->__nonoption_flags_max_len = strlen (orig_str); - if (d->__nonoption_flags_max_len < argc) - d->__nonoption_flags_max_len = argc; - __getopt_nonoption_flags = - (char *) malloc (d->__nonoption_flags_max_len); - if (__getopt_nonoption_flags == NULL) - d->__nonoption_flags_max_len = -1; - else - memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), - '\0', d->__nonoption_flags_max_len - len); - } - } - d->__nonoption_flags_len = d->__nonoption_flags_max_len; - } - else - d->__nonoption_flags_len = 0; -#endif - - return optstring; -} - -/* Scan elements of ARGV (whose length is ARGC) for option characters - given in OPTSTRING. - - If an element of ARGV starts with '-', and is not exactly "-" or "--", - then it is an option element. The characters of this element - (aside from the initial '-') are option characters. If `getopt' - is called repeatedly, it returns successively each of the option characters - from each of the option elements. - - If `getopt' finds another option character, it returns that character, - updating `optind' and `nextchar' so that the next call to `getopt' can - resume the scan with the following option character or ARGV-element. - - If there are no more option characters, `getopt' returns -1. - Then `optind' is the index in ARGV of the first ARGV-element - that is not an option. (The ARGV-elements have been permuted - so that those that are not options now come last.) - - OPTSTRING is a string containing the legitimate option characters. - If an option character is seen that is not listed in OPTSTRING, - return '?' after printing an error message. If you set `opterr' to - zero, the error message is suppressed but we still return '?'. - - If a char in OPTSTRING is followed by a colon, that means it wants an arg, - so the following text in the same ARGV-element, or the text of the following - ARGV-element, is returned in `optarg'. Two colons mean an option that - wants an optional arg; if there is text in the current ARGV-element, - it is returned in `optarg', otherwise `optarg' is set to zero. - - If OPTSTRING starts with `-' or `+', it requests different methods of - handling the non-option ARGV-elements. - See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. - - Long-named options begin with `--' instead of `-'. - Their names may be abbreviated as long as the abbreviation is unique - or is an exact match for some defined option. If they have an - argument, it follows the option name in the same ARGV-element, separated - from the option name by a `=', or else the in next ARGV-element. - When `getopt' finds a long-named option, it returns 0 if that option's - `flag' field is nonzero, the value of the option's `val' field - if the `flag' field is zero. - - The elements of ARGV aren't really const, because we permute them. - But we pretend they're const in the prototype to be compatible - with other systems. - - LONGOPTS is a vector of `struct option' terminated by an - element containing a name which is zero. - - LONGIND returns the index in LONGOPT of the long-named option found. - It is only valid when a long-named option has been found by the most - recent call. - - If LONG_ONLY is nonzero, '-' as well as '--' can introduce - long-named options. */ - -int -_getopt_internal_r (int argc, char *const *argv, const char *optstring, - const struct option *longopts, int *longind, - int long_only, struct _getopt_data *d, int posixly_correct) -{ - int print_errors = d->opterr; - - if (argc < 1) - return -1; - - d->optarg = NULL; - - if (d->optind == 0 || !d->__initialized) - { - if (d->optind == 0) - d->optind = 1; /* Don't scan ARGV[0], the program name. */ - optstring = _getopt_initialize (argc, argv, optstring, d, - posixly_correct); - d->__initialized = 1; - } - else if (optstring[0] == '-' || optstring[0] == '+') - optstring++; - if (optstring[0] == ':') - print_errors = 0; - - /* Test whether ARGV[optind] points to a non-option argument. - Either it does not have option syntax, or there is an environment flag - from the shell indicating it is not an option. The later information - is only used when the used in the GNU libc. */ -#if defined _LIBC && defined USE_NONOPTION_FLAGS -# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \ - || (d->optind < d->__nonoption_flags_len \ - && __getopt_nonoption_flags[d->optind] == '1')) -#else -# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') -#endif - - if (d->__nextchar == NULL || *d->__nextchar == '\0') - { - /* Advance to the next ARGV-element. */ - - /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been - moved back by the user (who may also have changed the arguments). */ - if (d->__last_nonopt > d->optind) - d->__last_nonopt = d->optind; - if (d->__first_nonopt > d->optind) - d->__first_nonopt = d->optind; - - if (d->__ordering == PERMUTE) - { - /* If we have just processed some options following some non-options, - exchange them so that the options come first. */ - - if (d->__first_nonopt != d->__last_nonopt - && d->__last_nonopt != d->optind) - exchange ((char **) argv, d); - else if (d->__last_nonopt != d->optind) - d->__first_nonopt = d->optind; - - /* Skip any additional non-options - and extend the range of non-options previously skipped. */ - - while (d->optind < argc && NONOPTION_P) - d->optind++; - d->__last_nonopt = d->optind; - } - - /* The special ARGV-element `--' means premature end of options. - Skip it like a null option, - then exchange with previous non-options as if it were an option, - then skip everything else like a non-option. */ - - if (d->optind != argc && !strcmp (argv[d->optind], "--")) - { - d->optind++; - - if (d->__first_nonopt != d->__last_nonopt - && d->__last_nonopt != d->optind) - exchange ((char **) argv, d); - else if (d->__first_nonopt == d->__last_nonopt) - d->__first_nonopt = d->optind; - d->__last_nonopt = argc; - - d->optind = argc; - } - - /* If we have done all the ARGV-elements, stop the scan - and back over any non-options that we skipped and permuted. */ - - if (d->optind == argc) - { - /* Set the next-arg-index to point at the non-options - that we previously skipped, so the caller will digest them. */ - if (d->__first_nonopt != d->__last_nonopt) - d->optind = d->__first_nonopt; - return -1; - } - - /* If we have come to a non-option and did not permute it, - either stop the scan or describe it to the caller and pass it by. */ - - if (NONOPTION_P) - { - if (d->__ordering == REQUIRE_ORDER) - return -1; - d->optarg = argv[d->optind++]; - return 1; - } - - /* We have found another option-ARGV-element. - Skip the initial punctuation. */ - - d->__nextchar = (argv[d->optind] + 1 - + (longopts != NULL && argv[d->optind][1] == '-')); - } - - /* Decode the current option-ARGV-element. */ - - /* Check whether the ARGV-element is a long option. - - If long_only and the ARGV-element has the form "-f", where f is - a valid short option, don't consider it an abbreviated form of - a long option that starts with f. Otherwise there would be no - way to give the -f short option. - - On the other hand, if there's a long option "fubar" and - the ARGV-element is "-fu", do consider that an abbreviation of - the long option, just like "--fu", and not "-f" with arg "u". - - This distinction seems to be the most useful approach. */ - - if (longopts != NULL - && (argv[d->optind][1] == '-' - || (long_only && (argv[d->optind][2] - || !strchr (optstring, argv[d->optind][1]))))) - { - char *nameend; - size_t namelen; - const struct option *p; - const struct option *pfound = NULL; - struct option_list - { - const struct option *p; - struct option_list *next; - } *ambig_list = NULL; - int exact = 0; - int indfound = -1; - int option_index; - - for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++) - /* Do nothing. */ ; - namelen = nameend - d->__nextchar; - - /* Test all long options for either exact match - or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, d->__nextchar, namelen)) - { - if (namelen == (unsigned int) strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else if (long_only - || pfound->has_arg != p->has_arg - || pfound->flag != p->flag - || pfound->val != p->val) - { - /* Second or later nonexact match found. */ - struct option_list *newp = alloca (sizeof (*newp)); - newp->p = p; - newp->next = ambig_list; - ambig_list = newp; - } - } - - if (ambig_list != NULL && !exact) - { - if (print_errors) - { - struct option_list first; - first.p = pfound; - first.next = ambig_list; - ambig_list = &first; - -#if defined _LIBC - char *buf = NULL; - size_t buflen = 0; - - FILE *fp = open_memstream (&buf, &buflen); - if (fp != NULL) - { - fprintf (fp, - _("%s: option '%s' is ambiguous; possibilities:"), - argv[0], argv[d->optind]); - - do - { - fprintf (fp, " '--%s'", ambig_list->p->name); - ambig_list = ambig_list->next; - } - while (ambig_list != NULL); - - fputc_unlocked ('\n', fp); - - if (__builtin_expect (fclose (fp) != EOF, 1)) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } - } -#else - fprintf (stderr, - _("%s: option '%s' is ambiguous; possibilities:"), - argv[0], argv[d->optind]); - do - { - fprintf (stderr, " '--%s'", ambig_list->p->name); - ambig_list = ambig_list->next; - } - while (ambig_list != NULL); - - fputc ('\n', stderr); -#endif - } - d->__nextchar += strlen (d->__nextchar); - d->optind++; - d->optopt = 0; - return '?'; - } - - if (pfound != NULL) - { - option_index = indfound; - d->optind++; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - allow it to be used on enums. */ - if (pfound->has_arg) - d->optarg = nameend + 1; - else - { - if (print_errors) - { -#if defined _LIBC - char *buf; - int n; -#endif - - if (argv[d->optind - 1][1] == '-') - { - /* --option */ -#if defined _LIBC - n = __asprintf (&buf, _("\ -%s: option '--%s' doesn't allow an argument\n"), - argv[0], pfound->name); -#else - fprintf (stderr, _("\ -%s: option '--%s' doesn't allow an argument\n"), - argv[0], pfound->name); -#endif - } - else - { - /* +option or -option */ -#if defined _LIBC - n = __asprintf (&buf, _("\ -%s: option '%c%s' doesn't allow an argument\n"), - argv[0], argv[d->optind - 1][0], - pfound->name); -#else - fprintf (stderr, _("\ -%s: option '%c%s' doesn't allow an argument\n"), - argv[0], argv[d->optind - 1][0], - pfound->name); -#endif - } - -#if defined _LIBC - if (n >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 - |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#endif - } - - d->__nextchar += strlen (d->__nextchar); - - d->optopt = pfound->val; - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (d->optind < argc) - d->optarg = argv[d->optind++]; - else - { - if (print_errors) - { -#if defined _LIBC - char *buf; - - if (__asprintf (&buf, _("\ -%s: option '--%s' requires an argument\n"), - argv[0], pfound->name) >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 - |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#else - fprintf (stderr, - _("%s: option '--%s' requires an argument\n"), - argv[0], pfound->name); -#endif - } - d->__nextchar += strlen (d->__nextchar); - d->optopt = pfound->val; - return optstring[0] == ':' ? ':' : '?'; - } - } - d->__nextchar += strlen (d->__nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } - - /* Can't find it as a long option. If this is not getopt_long_only, - or the option starts with '--' or is not a valid short - option, then it's an error. - Otherwise interpret it as a short option. */ - if (!long_only || argv[d->optind][1] == '-' - || strchr (optstring, *d->__nextchar) == NULL) - { - if (print_errors) - { -#if defined _LIBC - char *buf; - int n; -#endif - - if (argv[d->optind][1] == '-') - { - /* --option */ -#if defined _LIBC - n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"), - argv[0], d->__nextchar); -#else - fprintf (stderr, _("%s: unrecognized option '--%s'\n"), - argv[0], d->__nextchar); -#endif - } - else - { - /* +option or -option */ -#if defined _LIBC - n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"), - argv[0], argv[d->optind][0], d->__nextchar); -#else - fprintf (stderr, _("%s: unrecognized option '%c%s'\n"), - argv[0], argv[d->optind][0], d->__nextchar); -#endif - } - -#if defined _LIBC - if (n >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#endif - } - d->__nextchar = ""; - d->optind++; - d->optopt = 0; - return '?'; - } - } - - /* Look at and handle the next short option-character. */ - - { - char c = *d->__nextchar++; - char *temp = strchr (optstring, c); - char *nameend; - const struct option *p; - const struct option *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound = 0; - int option_index; - - /* Increment `optind' when we start to process its last character. */ - if (*d->__nextchar == '\0') - ++d->optind; - - if (temp == NULL || c == ':' || c == ';') - { - if (print_errors) - { -#if defined _LIBC - char *buf; - int n; -#endif - -#if defined _LIBC - n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"), - argv[0], c); -#else - fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c); -#endif - -#if defined _LIBC - if (n >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#endif - } - d->optopt = c; - return '?'; - } - /* Convenience. Treat POSIX -W foo same as long option --foo */ - if (temp[0] == 'W' && temp[1] == ';') - { - if (longopts == NULL) - goto no_longs; - - - /* This is an option that requires an argument. */ - if (*d->__nextchar != '\0') - { - d->optarg = d->__nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - we must advance to the next element now. */ - d->optind++; - } - else if (d->optind == argc) - { - if (print_errors) - { -#if defined _LIBC - char *buf; - - if (__asprintf (&buf, - _("%s: option requires an argument -- '%c'\n"), - argv[0], c) >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#else - fprintf (stderr, - _("%s: option requires an argument -- '%c'\n"), - argv[0], c); -#endif - } - d->optopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - return c; - } - else - /* We already incremented `d->optind' once; - increment it again when taking next ARGV-elt as argument. */ - d->optarg = argv[d->optind++]; - - /* optarg is now the argument, see if it's in the - table of longopts. */ - - for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; - nameend++) - /* Do nothing. */ ; - - /* Test all long options for either exact match - or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) - { - if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else if (long_only - || pfound->has_arg != p->has_arg - || pfound->flag != p->flag - || pfound->val != p->val) - /* Second or later nonexact match found. */ - ambig = 1; - } - if (ambig && !exact) - { - if (print_errors) - { -#if defined _LIBC - char *buf; - - if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"), - argv[0], d->optarg) >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#else - fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"), - argv[0], d->optarg); -#endif - } - d->__nextchar += strlen (d->__nextchar); - d->optind++; - return '?'; - } - if (pfound != NULL) - { - option_index = indfound; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - allow it to be used on enums. */ - if (pfound->has_arg) - d->optarg = nameend + 1; - else - { - if (print_errors) - { -#if defined _LIBC - char *buf; - - if (__asprintf (&buf, _("\ -%s: option '-W %s' doesn't allow an argument\n"), - argv[0], pfound->name) >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 - |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#else - fprintf (stderr, _("\ -%s: option '-W %s' doesn't allow an argument\n"), - argv[0], pfound->name); -#endif - } - - d->__nextchar += strlen (d->__nextchar); - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (d->optind < argc) - d->optarg = argv[d->optind++]; - else - { - if (print_errors) - { -#if defined _LIBC - char *buf; - - if (__asprintf (&buf, _("\ -%s: option '-W %s' requires an argument\n"), - argv[0], pfound->name) >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 - |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#else - fprintf (stderr, _("\ -%s: option '-W %s' requires an argument\n"), - argv[0], pfound->name); -#endif - } - d->__nextchar += strlen (d->__nextchar); - return optstring[0] == ':' ? ':' : '?'; - } - } - else - d->optarg = NULL; - d->__nextchar += strlen (d->__nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } - - no_longs: - d->__nextchar = NULL; - return 'W'; /* Let the application handle it. */ - } - if (temp[1] == ':') - { - if (temp[2] == ':') - { - /* This is an option that accepts an argument optionally. */ - if (*d->__nextchar != '\0') - { - d->optarg = d->__nextchar; - d->optind++; - } - else - d->optarg = NULL; - d->__nextchar = NULL; - } - else - { - /* This is an option that requires an argument. */ - if (*d->__nextchar != '\0') - { - d->optarg = d->__nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - we must advance to the next element now. */ - d->optind++; - } - else if (d->optind == argc) - { - if (print_errors) - { -#if defined _LIBC - char *buf; - - if (__asprintf (&buf, _("\ -%s: option requires an argument -- '%c'\n"), - argv[0], c) >= 0) - { - _IO_flockfile (stderr); - - int old_flags2 = ((_IO_FILE *) stderr)->_flags2; - ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; - - __fxprintf (NULL, "%s", buf); - - ((_IO_FILE *) stderr)->_flags2 = old_flags2; - _IO_funlockfile (stderr); - - free (buf); - } -#else - fprintf (stderr, - _("%s: option requires an argument -- '%c'\n"), - argv[0], c); -#endif - } - d->optopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - } - else - /* We already incremented `optind' once; - increment it again when taking next ARGV-elt as argument. */ - d->optarg = argv[d->optind++]; - d->__nextchar = NULL; - } - } - return c; - } -} - -int -_getopt_internal (int argc, char *const *argv, const char *optstring, - const struct option *longopts, int *longind, int long_only, - int posixly_correct) -{ - int result; - - getopt_data.optind = optind; - getopt_data.opterr = opterr; - - result = _getopt_internal_r (argc, argv, optstring, longopts, - longind, long_only, &getopt_data, - posixly_correct); - - optind = getopt_data.optind; - optarg = getopt_data.optarg; - optopt = getopt_data.optopt; - - return result; -} - -int -getopt (int argc, char *const *argv, const char *optstring) -{ - return _getopt_internal (argc, argv, optstring, - (const struct option *) 0, - (int *) 0, - 0, 0); -} - -/* getopt_long() was copied from posix/getopt1.c - the rest of this file is a nearly identical copy of posix/getopt.c */ -int -getopt_long (int argc, char *const *argv, const char *options, - const struct option *long_options, int *opt_index) -{ - return _getopt_internal (argc, argv, options, long_options, - opt_index, 0, 0); -} - - -#ifdef _LIBC -int -__posix_getopt (int argc, char *const *argv, const char *optstring) -{ - return _getopt_internal (argc, argv, optstring, - (const struct option *) 0, - (int *) 0, - 0, 1); -} -#endif - -#endif /* Not ELIDE_CODE. */ - -#ifdef TEST - -/* Compile with -DTEST to make an executable for use in testing - the above definition of `getopt'. */ - -int -main (int argc, char **argv) -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - - c = getopt (argc, argv, "abc:d:0123456789"); - if (c == -1) - break; - - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value '%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ - -/* - * Editor modelines - https://www.wireshark.org/tools/modelines.html - * - * Local Variables: - * c-basic-offset: 2 - * tab-width: 8 - * indent-tabs-mode: nil - * End: - * - * ex: set shiftwidth=2 tabstop=8 expandtab: - * :indentSize=2:tabSize=8:noTabs=true: - */ diff --git a/wsutil/getopt_long.h b/wsutil/getopt_long.h deleted file mode 100644 index 03aec05b42..0000000000 --- a/wsutil/getopt_long.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - Copied from glibc-2.15 - - Internal declarations for getopt. - Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2009 - Free Software Foundation, Inc. - This file is part of the GNU C Library. - - SPDX-License-Identifier: LGPL-2.0-or-later -*/ - -#ifndef _GETOPT_INT_H -#define _GETOPT_INT_H 1 - -extern int _getopt_internal (int ___argc, char *const *___argv, - const char *__shortopts, - const struct option *__longopts, int *__longind, - int __long_only, int posixly_correct); - - -/* Reentrant versions which can handle parsing multiple argument - vectors at the same time. */ - -/* Data type for reentrant functions. */ -struct _getopt_data -{ - /* These have exactly the same meaning as the corresponding global - variables, except that they are used for the reentrant - versions of getopt. */ - int optind; - int opterr; - int optopt; - char *optarg; - - /* Internal members. */ - - /* True if the internal members have been initialized. */ - int __initialized; - - /* The next char to be scanned in the option-element - in which the last option character we returned was found. - This allows us to pick up the scan where we left off. - - If this is zero, or a null string, it means resume the scan - by advancing to the next ARGV-element. */ - char *__nextchar; - - /* Describe how to deal with options that follow non-option ARGV-elements. - - If the caller did not specify anything, - the default is REQUIRE_ORDER if the environment variable - POSIXLY_CORRECT is defined, PERMUTE otherwise. - - REQUIRE_ORDER means don't recognize them as options; - stop option processing when the first non-option is seen. - This is what Unix does. - This mode of operation is selected by either setting the environment - variable POSIXLY_CORRECT, or using `+' as the first character - of the list of option characters. - - PERMUTE is the default. We permute the contents of ARGV as we - scan, so that eventually all the non-options are at the end. - This allows options to be given in any order, even with programs - that were not written to expect this. - - RETURN_IN_ORDER is an option available to programs that were - written to expect options and other ARGV-elements in any order - and that care about the ordering of the two. We describe each - non-option ARGV-element as if it were the argument of an option - with character code 1. Using `-' as the first character of the - list of option characters selects this mode of operation. - - The special argument `--' forces an end of option-scanning regardless - of the value of `ordering'. In the case of RETURN_IN_ORDER, only - `--' can cause `getopt' to return -1 with `optind' != ARGC. */ - - enum - { - REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER - } __ordering; - - /* If the POSIXLY_CORRECT environment variable is set. */ - int __posixly_correct; - - - /* Handle permutation of arguments. */ - - /* Describe the part of ARGV that contains non-options that have - been skipped. `first_nonopt' is the index in ARGV of the first - of them; `last_nonopt' is the index after the last of them. */ - - int __first_nonopt; - int __last_nonopt; - -#if defined _LIBC && defined USE_NONOPTION_FLAGS - int __nonoption_flags_max_len; - int __nonoption_flags_len; -# endif -}; - -/* The initializer is necessary to set OPTIND and OPTERR to their - default values and to clear the initialization flag. */ -#define _GETOPT_DATA_INITIALIZER { 1, 1 } - -extern int _getopt_internal_r (int ___argc, char *const *___argv, - const char *__shortopts, - const struct option *__longopts, int *__longind, - int __long_only, struct _getopt_data *__data, - int posixly_correct); - -extern int _getopt_long_r (int ___argc, char *const *___argv, - const char *__shortopts, - const struct option *__longopts, int *__longind, - struct _getopt_data *__data); - -extern int _getopt_long_only_r (int ___argc, char *const *___argv, - const char *__shortopts, - const struct option *__longopts, - int *__longind, - struct _getopt_data *__data); - -#endif /* getopt_int.h */ diff --git a/wsutil/ws_getopt.c b/wsutil/ws_getopt.c index c3f6699559..0b37bd8a48 100644 --- a/wsutil/ws_getopt.c +++ b/wsutil/ws_getopt.c @@ -1,71 +1,93 @@ -#define _BSD_SOURCE -#include +/* + * musl as a whole is licensed under the following standard MIT license: + * + * ---------------------------------------------------------------------- + * Copyright © 2005-2020 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * ---------------------------------------------------------------------- + */ + +//#define _BSD_SOURCE +//#include #include #include #include #include -#include "locale_impl.h" -#include "stdio_impl.h" +#include -char *optarg; -int optind=1, opterr=1, optopt, __optpos, __optreset=0; +#include -#define optpos __optpos -weak_alias(__optreset, optreset); +char *ws_optarg; +int ws_optind=1, ws_opterr=1, ws_optopt, ws_optpos, ws_optreset=0; void __getopt_msg(const char *a, const char *b, const char *c, size_t l) { FILE *f = stderr; - b = __lctrans_cur(b); - FLOCK(f); - fputs(a, f)>=0 - && fwrite(b, strlen(b), 1, f) - && fwrite(c, 1, l, f)==l - && putc('\n', f); - FUNLOCK(f); + fputs(a, f); + fwrite(b, strlen(b), 1, f); + fwrite(c, 1, l, f); + putc('\n', f); } -int getopt(int argc, char * const argv[], const char *optstring) +int ws_getopt(int argc, char * const argv[], const char *optstring) { int i; wchar_t c, d; int k, l; char *optchar; - if (!optind || __optreset) { - __optreset = 0; - __optpos = 0; - optind = 1; + if (!ws_optind || ws_optreset) { + ws_optreset = 0; + ws_optpos = 0; + ws_optind = 1; } - if (optind >= argc || !argv[optind]) + if (ws_optind >= argc || !argv[ws_optind]) return -1; - if (argv[optind][0] != '-') { + if (argv[ws_optind][0] != '-') { if (optstring[0] == '-') { - optarg = argv[optind++]; + ws_optarg = argv[ws_optind++]; return 1; } return -1; } - if (!argv[optind][1]) + if (!argv[ws_optind][1]) return -1; - if (argv[optind][1] == '-' && !argv[optind][2]) - return optind++, -1; + if (argv[ws_optind][1] == '-' && !argv[ws_optind][2]) + return ws_optind++, -1; - if (!optpos) optpos++; - if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) { + if (!ws_optpos) ws_optpos++; + if ((k = mbtowc(&c, argv[ws_optind]+ws_optpos, MB_LEN_MAX)) < 0) { k = 1; c = 0xfffd; /* replacement char */ } - optchar = argv[optind]+optpos; - optpos += k; + optchar = argv[ws_optind]+ws_optpos; + ws_optpos += k; - if (!argv[optind][optpos]) { - optind++; - optpos = 0; + if (!argv[ws_optind][ws_optpos]) { + ws_optind++; + ws_optpos = 0; } if (optstring[0] == '-' || optstring[0] == '+') @@ -79,21 +101,21 @@ int getopt(int argc, char * const argv[], const char *optstring) } while (l && d != c); if (d != c || c == ':') { - optopt = c; - if (optstring[0] != ':' && opterr) + ws_optopt = c; + if (optstring[0] != ':' && ws_opterr) __getopt_msg(argv[0], ": unrecognized option: ", optchar, k); return '?'; } if (optstring[i] == ':') { - optarg = 0; - if (optstring[i+1] != ':' || optpos) { - optarg = argv[optind++] + optpos; - optpos = 0; + ws_optarg = 0; + if (optstring[i+1] != ':' || ws_optpos) { + ws_optarg = argv[ws_optind++] + ws_optpos; + ws_optpos = 0; } - if (optind > argc) { - optopt = c; + if (ws_optind > argc) { + ws_optopt = c; if (optstring[0] == ':') return ':'; - if (opterr) __getopt_msg(argv[0], + if (ws_opterr) __getopt_msg(argv[0], ": option requires an argument: ", optchar, k); return '?'; @@ -101,5 +123,3 @@ int getopt(int argc, char * const argv[], const char *optstring) } return c; } - -weak_alias(getopt, __posix_getopt); diff --git a/wsutil/ws_getopt.h b/wsutil/ws_getopt.h index 35cbd358b5..f0a5b89625 100644 --- a/wsutil/ws_getopt.h +++ b/wsutil/ws_getopt.h @@ -1,13 +1,42 @@ -#ifndef _GETOPT_H -#define _GETOPT_H +/* + * musl as a whole is licensed under the following standard MIT license: + * + * ---------------------------------------------------------------------- + * Copyright © 2005-2020 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * ---------------------------------------------------------------------- + */ + +#ifndef _WS_GETOPT_H_ +#define _WS_GETOPT_H_ + +#include #ifdef __cplusplus extern "C" { #endif -int getopt(int, char * const [], const char *); -extern char *optarg; -extern int optind, opterr, optopt, optreset; +WS_DLL_PUBLIC int ws_getopt(int, char * const [], const char *); +WS_DLL_PUBLIC char *ws_optarg; +WS_DLL_PUBLIC int ws_optind, ws_opterr, ws_optopt, ws_optpos, ws_optreset; struct option { const char *name; @@ -16,8 +45,10 @@ struct option { int val; }; -int getopt_long(int, char *const *, const char *, const struct option *, int *); -int getopt_long_only(int, char *const *, const char *, const struct option *, int *); +WS_DLL_PUBLIC int ws_getopt_long(int, char *const *, const char *, const struct option *, int *); +WS_DLL_PUBLIC int ws_getopt_long_only(int, char *const *, const char *, const struct option *, int *); + +WS_DLL_LOCAL void __getopt_msg(const char *a, const char *b, const char *c, size_t l); #define no_argument 0 #define required_argument 1 diff --git a/wsutil/ws_getopt_long.c b/wsutil/ws_getopt_long.c index 6949ab1c7e..07351e7eaf 100644 --- a/wsutil/ws_getopt_long.c +++ b/wsutil/ws_getopt_long.c @@ -1,13 +1,39 @@ -#define _GNU_SOURCE +/* + * musl as a whole is licensed under the following standard MIT license: + * + * ---------------------------------------------------------------------- + * Copyright © 2005-2020 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * ---------------------------------------------------------------------- + */ + +//#define _GNU_SOURCE #include #include #include -#include #include #include -#include "stdio_impl.h" +#include -extern int __optpos, __optreset; +#include static void permute(char *const *argv, int dest, int src) { @@ -24,42 +50,42 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) { int ret, skipped, resumed; - if (!optind || __optreset) { - __optreset = 0; - __optpos = 0; - optind = 1; + if (!ws_optind || ws_optreset) { + ws_optreset = 0; + ws_optpos = 0; + ws_optind = 1; } - if (optind >= argc || !argv[optind]) return -1; - skipped = optind; + if (ws_optind >= argc || !argv[ws_optind]) return -1; + skipped = ws_optind; if (optstring[0] != '+' && optstring[0] != '-') { int i; - for (i=optind; ; i++) { + for (i=ws_optind; ; i++) { if (i >= argc || !argv[i]) return -1; if (argv[i][0] == '-' && argv[i][1]) break; } - optind = i; + ws_optind = i; } - resumed = optind; + resumed = ws_optind; ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly); if (resumed > skipped) { - int i, cnt = optind-resumed; + int i, cnt = ws_optind-resumed; for (i=0; i