On Tue, Sep 6, 2016 at 4:22 PM, Jim Meyering <j...@meyering.net> wrote:
> On Tue, Sep 6, 2016 at 12:38 PM, Gisle Vanem <gva...@yahoo.no> wrote:
>> Jim Meyering wrote:
>>
>>> From the output of that mingw configure run, it appears they are not
>>> declared -- at least not in any header included by this particular
>>> test program:
>>>
>>>   checking whether program_invocation_name is declared... no
>>>   checking whether program_invocation_short_name is declared... no
>>
>> There are other options; both MinGW and MSVC have:
>>   extern char** __argv;
>>
>> in their <stdlib.h>.
>
> Thanks. I will extend getprogname to detect/use that, when possible.

I've pushed the attached, which also adds a minimal test case:
From 320679aaa13016445f7ba5aba5c7a5a96541d630 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyer...@fb.com>
Date: Wed, 7 Sep 2016 07:57:47 -0700
Subject: [PATCH] getprogname: port to systems with __argv (mingw, msvc)

* lib/getprogname.c (getprogname): Include "dirname.h" and use
last_component: more general than open coding it with hard-coded "/".
* lib/getprogname.h (getprogname): Prefer "char const *" consistently.
* modules/getprogname (Depends-on): Add dirname-lgpl.
(configure.ac): Check for __argv in <stdlib.h>.
* modules/getprogname-tests: New file.
* tests/test-getprogname.c: New file.
Suggested by Gisle Vanem in
https://lists.gnu.org/archive/html/bug-gnulib/2016-09/msg00014.html
---
 ChangeLog                 | 13 +++++++++++++
 lib/getprogname.c         | 33 ++++++++++++++-------------------
 lib/getprogname.h         |  2 +-
 modules/getprogname       |  2 ++
 modules/getprogname-tests | 13 +++++++++++++
 tests/test-getprogname.c  | 31 +++++++++++++++++++++++++++++++
 6 files changed, 74 insertions(+), 20 deletions(-)
 create mode 100644 modules/getprogname-tests
 create mode 100644 tests/test-getprogname.c

diff --git a/ChangeLog b/ChangeLog
index 8391aab..1786c81 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2016-09-07  Jim Meyering  <meyer...@fb.com>
+
+       getprogname: port to systems with __argv (mingw, msvc)
+       * lib/getprogname.c (getprogname): Include "dirname.h" and use
+       last_component: more general than open coding it with hard-coded "/".
+       * lib/getprogname.h (getprogname): Prefer "char const *" consistently.
+       * modules/getprogname (Depends-on): Add dirname-lgpl.
+       (configure.ac): Check for __argv in <stdlib.h>.
+       * modules/getprogname-tests: New file.
+       * tests/test-getprogname.c: New file.
+       Suggested by Gisle Vanem in
+       https://lists.gnu.org/archive/html/bug-gnulib/2016-09/msg00014.html
+
 2016-09-07  Paul Eggert  <egg...@cs.ucla.edu>

        flexmember: port better to GCC + valgrind
diff --git a/lib/getprogname.c b/lib/getprogname.c
index 522f3eb..77aaf18 100644
--- a/lib/getprogname.c
+++ b/lib/getprogname.c
@@ -20,34 +20,29 @@
 #include "getprogname.h"

 #include <errno.h> /* get program_invocation_name declaration */
-#include <stdlib.h>
-#include <string.h>
+#include <stdlib.h> /* get __argv declaration */

+#include "dirname.h"

 #ifndef HAVE_GETPROGNAME
-const char *
+
+char const *
 getprogname (void)
 {
-#if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
+# if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
   return program_invocation_short_name;
-#elif HAVE_DECL_PROGRAM_INVOCATION_NAME || HAVE_GETEXECNAME
-
-  const char *slash;
-# if HAVE_DECL_PROGRAM_INVOCATION_NAME
-  const char *base = program_invocation_name;
-# else
+# elif HAVE_DECL_PROGRAM_INVOCATION_NAME
+  return last_component (program_invocation_name);
+# elif HAVE_GETEXECNAME
   const char *base = getexecname ();
   if (!base)
     base = "?";
+  return last_component (program_invocation_name);
+# elif HAVE_DECL___ARGV
+  return last_component (__argv);
+# else
+#  error "getprogname module not ported to this OS"
 # endif
-
-  slash = strrchr (base, '/');
-  if (slash != NULL)
-    base = slash + 1;
-
-  return base;
-#else
- #error "getprogname module not ported to this OS"
-#endif
 }
+
 #endif
diff --git a/lib/getprogname.h b/lib/getprogname.h
index 1887261..36b8ba8 100644
--- a/lib/getprogname.h
+++ b/lib/getprogname.h
@@ -24,7 +24,7 @@ extern "C" {
 #endif

 #ifndef HAVE_GETPROGNAME
-extern const char *getprogname (void)
+extern char const *getprogname (void)
 # ifdef HAVE_DECL_PROGRAM_INVOCATION_NAME
   _GL_ATTRIBUTE_PURE
 # endif
diff --git a/modules/getprogname b/modules/getprogname
index efda4fa..1a26398 100644
--- a/modules/getprogname
+++ b/modules/getprogname
@@ -7,6 +7,7 @@ lib/getprogname.c
 m4/getprogname.m4

 Depends-on:
+dirname-lgpl
 extensions

 configure.ac:
@@ -14,6 +15,7 @@ gl_FUNC_GETPROGNAME
 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
 AC_CHECK_DECLS([program_invocation_name], [], [], [#include <errno.h>])
 AC_CHECK_DECLS([program_invocation_short_name], [], [], [#include <errno.h>])
+AC_CHECK_DECLS([__argv], [], [], [#include <stdlib.h>])

 Makefile.am:
 lib_SOURCES += getprogname.h getprogname.c
diff --git a/modules/getprogname-tests b/modules/getprogname-tests
new file mode 100644
index 0000000..071bf38
--- /dev/null
+++ b/modules/getprogname-tests
@@ -0,0 +1,13 @@
+Files:
+tests/test-getprogname.c
+
+Depends-on:
+assert-h
+string
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-getprogname
+check_PROGRAMS += test-getprogname
+test_getprogname_LDADD = $(LDADD)
diff --git a/tests/test-getprogname.c b/tests/test-getprogname.c
new file mode 100644
index 0000000..4d92170
--- /dev/null
+++ b/tests/test-getprogname.c
@@ -0,0 +1,31 @@
+/* Test the gnulib getprogname module.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include "getprogname.h"
+#include <string.h>
+#include <assert.h>
+
+#define STREQ(a, b) (strcmp (a, b) == 0)
+
+int
+main (void)
+{
+  char const *p = getprogname ();
+  assert (STREQ (p, "test-getprogname"));
+  return 0;
+}
-- 
2.7.4

Reply via email to