From ed2c6fc1eeab6234f26f9e1817bba8a7d9c84063 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 24 Nov 2022 14:44:52 +0000 Subject: getopt: use name supplied with '-n' to report error On Windows: $ getopt -o a -n my_script -- -z getopt: unknown option -- z On Linux: $ getopt -o a -n my_script -- -z my_script: unknown option -- z The difference arises because the Mingw-w64 implementation of getopt(3) uses __argv[0] to report errors, not the first element of the argument array passed to it. Make __argv[0] point to the name supplied with the '-n' option to match behaviour when glibc getopt(3) is used. (GitHub issue #274) --- util-linux/getopt.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util-linux/getopt.c b/util-linux/getopt.c index 4148586d3..42e0a2730 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c @@ -412,6 +412,11 @@ int getopt_main(int argc, char **argv) bb_simple_error_msg_and_die("missing optstring argument"); } +#if ENABLE_PLATFORM_MINGW32 + // Mingw-w64 getopt(3) uses __argv[0] in error messages, not the + // first element of its argument array. + __argv[0] = +#endif argv[n] = name ? name : argv[0]; return generate_output(argv + n, argc - n, optstr, long_options); } -- cgit v1.2.3-55-g6feb