Subject: Re: [PATCH] getprogname: port to OpenBSD 5.1

From: Jim Meyering <[email-masked]>
Date: Wed, 28 Sep 2016 21:14:35 -0700

On Wed, Sep 28, 2016 at 11:28 AM, Jim Meyering <[email-masked]> wrote:
> FYI: grep (due to its use of the new getprogname module) would fail to
> configure on OpenBSD 5.1. I'll soon push this to gnulib and then
> update grep's gnulib submodule to latest to pull it in.

Glad I tested on Fedora 25-to-be. The above caused
program_invocation_short_name to always be the empty string. I fixed
it by declaring __progname only in the narrow/later case in which it
is required:
From af62896a5bd7c63b9569813a0eff11b6b4ab665d Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Wed, 28 Sep 2016 20:57:24 -0700
Subject: [PATCH] getprogname: avoid __progname vs
 program_invocation_short_name pitfall

I.e., don't let the OpenBSD 5.1 fix induce failure when using newer
glibc.  Would have caused failure with Fedora 25's glibc-2.24-3, but
not with Fedora 24's glibc-2.23.1-10.
* lib/getprogname.c (__progname): Move this declaration down...
(getprogname): ... into the #elif block where used, and make it
explicitly "extern".
---
 ChangeLog         |  8 ++++++++
 lib/getprogname.c | 11 +++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 121c0c3..21a55e8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2016-09-28  Jim Meyering  <[email-masked]>

+	getprogname: avoid __progname vs program_invocation_short_name pitfall
+	I.e., don't let the OpenBSD 5.1 fix induce failure when using newer
+	glibc.  Would have caused failure with Fedora 25's glibc-2.24-3, but
+	not with Fedora 24's glibc-2.23.1-10.
+	* lib/getprogname.c (__progname): Move this declaration down...
+	(getprogname): ... into the #elif block where used, and make it
+	explicitly "extern".
+
 	getprogname: port to OpenBSD 5.1
 	* lib/getprogname.c (__progname) [HAVE_DECL___PROGNAME]: Declare.
 	(getprogname) [HAVE_DECL___PROGNAME]: Return __progname or "?".
diff --git a/lib/getprogname.c b/lib/getprogname.c
index 1e2ba20..7de2317 100644
--- a/lib/getprogname.c
+++ b/lib/getprogname.c
@@ -32,10 +32,6 @@

 #ifndef HAVE_GETPROGNAME

-# ifdef HAVE_DECL___PROGNAME
-char *__progname;
-# endif
-
 char const *
 getprogname (void)
 {
@@ -52,8 +48,11 @@ getprogname (void)
   const char *p = __argv && __argv[0] ? __argv[0] : "?";
   return last_component (p);
 # elif HAVE_DECL___PROGNAME
-  /* This variable exists at least on OpenBSD 5.1 and glibc-2.23,
-     and appears always to be the basename component of argv[0].  */
+  /* Be careful to declare this only when we absolutely need it
+     (OpenBSD 5.1), rather than when it's available.  Otherwise,
+     its mere declaration makes program_invocation_short_name
+     malfunction (have zero length) with Fedora 25's glibc.  */
+  extern char *__progname;
   const char *p = __progname;
   return p && p[0] ? p : "?";
 # elif _AIX
-- 
2.7.4