getopt* functions receive also argv[0] argument. So use it for printing
program name instead of the global __argv[0] variable.

This avoids using and importing of the __argv[] DATA symbol from the linked
CRT library, as it is not needed.
---
 mingw-w64-crt/misc/getopt.c | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)

diff --git a/mingw-w64-crt/misc/getopt.c b/mingw-w64-crt/misc/getopt.c
index ac7f9fe16d6c..fc317e34e84f 100644
--- a/mingw-w64-crt/misc/getopt.c
+++ b/mingw-w64-crt/misc/getopt.c
@@ -80,12 +80,6 @@ char    *optarg;             /* argument associated with 
option */
 #define        BADARG          ((*options == ':') ? (int)':' : (int)'?')
 #define        INORDER         (int)1
 
-#ifndef __CYGWIN__
-#define __progname __argv[0]
-#else
-extern char __declspec(dllimport) *__progname;
-#endif
-
 #ifdef __CYGWIN__
 static char EMSG[] = "";
 #else
@@ -114,20 +108,20 @@ static const char illoptchar[] = "unknown option -- %c";
 static const char illoptstring[] = "unknown option -- %s";
 
 static void
-_vwarnx(const char *fmt,va_list ap)
+_vwarnx(const char *argv0,const char *fmt,va_list ap)
 {
-  (void)fprintf(stderr,"%s: ",__progname);
+  (void)fprintf(stderr,"%s: ",argv0);
   if (fmt != NULL)
     (void)vfprintf(stderr,fmt,ap);
   (void)fprintf(stderr,"\n");
 }
 
 static void
-warnx(const char *fmt,...)
+warnx(const char *argv0,const char *fmt,...)
 {
   va_list ap;
   va_start(ap,fmt);
-  _vwarnx(fmt,ap);
+  _vwarnx(argv0,fmt,ap);
   va_end(ap);
 }
 
@@ -244,7 +238,7 @@ parse_long_options(char * const *nargv, const char *options,
        if (ambiguous) {
                /* ambiguous abbreviation */
                if (PRINT_ERROR)
-                       warnx(ambig, (int)current_argv_len,
+                       warnx(nargv[0], ambig, (int)current_argv_len,
                             current_argv);
                optopt = 0;
                return (BADCH);
@@ -253,7 +247,7 @@ parse_long_options(char * const *nargv, const char *options,
                if (long_options[match].has_arg == no_argument
                    && has_equal) {
                        if (PRINT_ERROR)
-                               warnx(noarg, (int)current_argv_len,
+                               warnx(nargv[0], noarg, (int)current_argv_len,
                                     current_argv);
                        /*
                         * XXX: GNU sets optopt to val regardless of flag
@@ -283,7 +277,7 @@ parse_long_options(char * const *nargv, const char *options,
                         * should be generated.
                         */
                        if (PRINT_ERROR)
-                               warnx(recargstring,
+                               warnx(nargv[0], recargstring,
                                    current_argv);
                        /*
                         * XXX: GNU sets optopt to val regardless of flag
@@ -301,7 +295,7 @@ parse_long_options(char * const *nargv, const char *options,
                        return (-1);
                }
                if (PRINT_ERROR)
-                       warnx(illoptstring, current_argv);
+                       warnx(nargv[0], illoptstring, current_argv);
                optopt = 0;
                return (BADCH);
        }
@@ -467,7 +461,7 @@ start:
                if (!*place)
                        ++optind;
                if (PRINT_ERROR)
-                       warnx(illoptchar, optchar);
+                       warnx(nargv[0], illoptchar, optchar);
                optopt = optchar;
                return (BADCH);
        }
@@ -478,7 +472,7 @@ start:
                else if (++optind >= nargc) {   /* no arg */
                        place = EMSG;
                        if (PRINT_ERROR)
-                               warnx(recargchar, optchar);
+                               warnx(nargv[0], recargchar, optchar);
                        optopt = optchar;
                        return (BADARG);
                } else                          /* white space */
@@ -499,7 +493,7 @@ start:
                        if (++optind >= nargc) {        /* no arg */
                                place = EMSG;
                                if (PRINT_ERROR)
-                                       warnx(recargchar, optchar);
+                                       warnx(nargv[0], recargchar, optchar);
                                optopt = optchar;
                                return (BADARG);
                        } else
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to