]> Savannah Git Hosting - gnulib.git/commitdiff
getprogname: port to systems with __argv (mingw, msvc)
authorJim Meyering <meyering@fb.com>
Wed, 7 Sep 2016 14:57:47 +0000 (07:57 -0700)
committerJim Meyering <meyering@fb.com>
Wed, 7 Sep 2016 15:29:36 +0000 (08:29 -0700)
* 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
lib/getprogname.c
lib/getprogname.h
modules/getprogname
modules/getprogname-tests [new file with mode: 0644]
tests/test-getprogname.c [new file with mode: 0644]

index 8391aab9bd7a22ae5872bef7fc34d071d5d859a3..1786c81834e1e069b92993a2ed727677fbe5a8d2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2016-09-07  Jim Meyering  <meyering@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  <eggert@cs.ucla.edu>
 
        flexmember: port better to GCC + valgrind
index 522f3eb9bf7ba37fb86b809e222b32a9d04fb7a2..77aaf1838a318468911aebb0537f4dd45fd46e1a 100644 (file)
 #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
index 1887261d2fd720182931702d4e9c4d54c9364c36..36b8ba84b06dbbe4c6cd137840f2c18cb85f6501 100644 (file)
@@ -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
index efda4fa24fc8443eae26f38f84e41cd025fc3332..1a263984082d779268dcfdddece49a52cc63d210 100644 (file)
@@ -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 (file)
index 0000000..071bf38
--- /dev/null
@@ -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 (file)
index 0000000..4d92170
--- /dev/null
@@ -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;
+}