+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
#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
#endif
#ifndef HAVE_GETPROGNAME
-extern const char *getprogname (void)
+extern char const *getprogname (void)
# ifdef HAVE_DECL_PROGRAM_INVOCATION_NAME
_GL_ATTRIBUTE_PURE
# endif
m4/getprogname.m4
Depends-on:
+dirname-lgpl
extensions
configure.ac:
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
--- /dev/null
+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)
--- /dev/null
+/* 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;
+}