Mercurial > gnulib
changeset 18404:ee1b22cf7503
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
author | Jim Meyering <meyering@fb.com> |
---|---|
date | Wed, 07 Sep 2016 07:57:47 -0700 |
parents | 4434607ac1ce |
children | c202f10ffda5 |
files | ChangeLog lib/getprogname.c lib/getprogname.h modules/getprogname modules/getprogname-tests tests/test-getprogname.c |
diffstat | 6 files changed, 74 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Wed Sep 07 02:01:42 2016 -0700 +++ b/ChangeLog Wed Sep 07 07:57:47 2016 -0700 @@ -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
--- a/lib/getprogname.c Wed Sep 07 02:01:42 2016 -0700 +++ b/lib/getprogname.c Wed Sep 07 07:57:47 2016 -0700 @@ -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
--- a/lib/getprogname.h Wed Sep 07 02:01:42 2016 -0700 +++ b/lib/getprogname.h Wed Sep 07 07:57:47 2016 -0700 @@ -24,7 +24,7 @@ #endif #ifndef HAVE_GETPROGNAME -extern const char *getprogname (void) +extern char const *getprogname (void) # ifdef HAVE_DECL_PROGRAM_INVOCATION_NAME _GL_ATTRIBUTE_PURE # endif
--- a/modules/getprogname Wed Sep 07 02:01:42 2016 -0700 +++ b/modules/getprogname Wed Sep 07 07:57:47 2016 -0700 @@ -7,6 +7,7 @@ m4/getprogname.m4 Depends-on: +dirname-lgpl extensions configure.ac: @@ -14,6 +15,7 @@ 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 Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/getprogname-tests Wed Sep 07 07:57:47 2016 -0700 @@ -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)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-getprogname.c Wed Sep 07 07:57:47 2016 -0700 @@ -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; +}