From a8ce1f9346beb8f6af8a2704f58ad1be8a491a40 Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Fri, 6 Nov 2015 16:30:33 +0000 Subject: Use getprogname(3) rather than __progname. Suggested by Joerg@ Sonnenberger (NetBSD). Last year, deraadt@ confirmed on tech@ that this "has the potential to be more portable", and micro-optimizing for speed is not relevant here. Also gets rid of one global variable. --- Makefile | 2 ++ compat_err.c | 4 +--- compat_progname.c | 42 ++++++++++++++++++++++++++++++++++++++++++ configure | 6 +++++- main.c | 33 ++++++++++++++++++--------------- mandoc_aux.c | 3 --- mandocdb.c | 8 +++----- test-progname.c | 9 +++++---- 8 files changed, 76 insertions(+), 31 deletions(-) create mode 100644 compat_progname.c diff --git a/Makefile b/Makefile index c9bd1f92..0687a13f 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,7 @@ SRCS = att.c \ compat_isblank.c \ compat_mkdtemp.c \ compat_ohash.c \ + compat_progname.c \ compat_reallocarray.c \ compat_sqlite3_errstr.c \ compat_strcasestr.c \ @@ -211,6 +212,7 @@ COMPAT_OBJS = compat_err.o \ compat_isblank.o \ compat_mkdtemp.o \ compat_ohash.o \ + compat_progname.o \ compat_reallocarray.o \ compat_sqlite3_errstr.o \ compat_strcasestr.o \ diff --git a/compat_err.c b/compat_err.c index 900769c2..01c2fefe 100644 --- a/compat_err.c +++ b/compat_err.c @@ -43,15 +43,13 @@ int dummy; #include #include -extern char *__progname; - static void vwarni(const char *, va_list); static void vwarnxi(const char *, va_list); static void vwarnxi(const char *fmt, va_list ap) { - fprintf(stderr, "%s: ", __progname); + fprintf(stderr, "%s: ", getprogname()); if (fmt != NULL) vfprintf(stderr, fmt, ap); } diff --git a/compat_progname.c b/compat_progname.c new file mode 100644 index 00000000..6951082b --- /dev/null +++ b/compat_progname.c @@ -0,0 +1,42 @@ +#include "config.h" + +#if HAVE_PROGNAME + +int dummy; + +#else + +/* $Id$ */ +/* + * Copyright (c) 2015 Ingo Schwarze + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +static const char *progname; + +void +setprogname(const char *name) +{ + + progname = name; +} + +const char * +getprogname(void) +{ + + return progname; +} + +#endif diff --git a/configure b/configure index 7bafcdde..8d9e525f 100755 --- a/configure +++ b/configure @@ -290,7 +290,6 @@ echo echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\"" echo "#define MANPATH_DEFAULT \"${MANPATH_DEFAULT}\"" [ -n "${OSNAME}" ] && echo "#define OSNAME \"${OSNAME}\"" -[ ${HAVE_PROGNAME} -eq 0 ] && echo "#define __progname mandoc_progname" [ -n "${HOMEBREWDIR}" ] && echo "#define HOMEBREWDIR \"${HOMEBREWDIR}\"" cat << __HEREDOC__ @@ -359,6 +358,11 @@ fi [ ${HAVE_MKDTEMP} -eq 0 ] && \ echo "extern char *mkdtemp(char *);" +if [ ${HAVE_PROGNAME} -eq 0 ]; then + echo "extern const char *getprogname(void);" + echo "extern void setprogname(const char *);" +fi + [ ${HAVE_REALLOCARRAY} -eq 0 ] && \ echo "extern void *reallocarray(void *, size_t, size_t);" diff --git a/main.c b/main.c index 7a720758..9e61eb07 100644 --- a/main.c +++ b/main.c @@ -101,8 +101,6 @@ static int toptions(struct curparse *, char *); static void usage(enum argmode) __attribute__((noreturn)); static int woptions(struct curparse *, char *); -extern char *__progname; - static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; static char help_arg[] = "help"; static char *help_argv[] = {help_arg, NULL}; @@ -116,6 +114,7 @@ main(int argc, char *argv[]) struct curparse curp; struct mansearch search; struct tag_files *tag_files; + const char *progname; char *auxpaths; char *defos; unsigned char *uc; @@ -132,17 +131,21 @@ main(int argc, char *argv[]) int use_pager; int c; -#if !HAVE_PROGNAME +#if HAVE_PROGNAME + progname = getprogname(); +#else if (argc < 1) - __progname = mandoc_strdup("mandoc"); - else if ((__progname = strrchr(argv[0], '/')) == NULL) - __progname = argv[0]; + progname = mandoc_strdup("mandoc"); + else if ((progname = strrchr(argv[0], '/')) == NULL) + progname = argv[0]; else - ++__progname; + ++progname; + setprogname(progname); #endif #if HAVE_SQLITE3 - if (strcmp(__progname, BINM_MAKEWHATIS) == 0) + if (strncmp(progname, "mandocdb", 8) == 0 || + strcmp(progname, BINM_MAKEWHATIS) == 0) return mandocdb(argc, argv); #endif @@ -155,13 +158,13 @@ main(int argc, char *argv[]) memset(&search, 0, sizeof(struct mansearch)); search.outkey = "Nd"; - if (strcmp(__progname, BINM_MAN) == 0) + if (strcmp(progname, BINM_MAN) == 0) search.argmode = ARG_NAME; - else if (strcmp(__progname, BINM_APROPOS) == 0) + else if (strcmp(progname, BINM_APROPOS) == 0) search.argmode = ARG_EXPR; - else if (strcmp(__progname, BINM_WHATIS) == 0) + else if (strcmp(progname, BINM_WHATIS) == 0) search.argmode = ARG_WORD; - else if (strncmp(__progname, "help", 4) == 0) + else if (strncmp(progname, "help", 4) == 0) search.argmode = ARG_NAME; else search.argmode = ARG_FILE; @@ -299,7 +302,7 @@ main(int argc, char *argv[]) */ if (search.argmode == ARG_NAME) { - if (*__progname == 'h') { + if (*progname == 'h') { if (argc == 0) { argv = help_argv; argc = 1; @@ -591,7 +594,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath, found: #if HAVE_SQLITE3 - warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s\n", + warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s", name, sec, paths->paths[ipath]); #endif *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage)); @@ -931,7 +934,7 @@ mmsg(enum mandocerr t, enum mandoclevel lvl, { const char *mparse_msg; - fprintf(stderr, "%s: %s:", __progname, file); + fprintf(stderr, "%s: %s:", getprogname(), file); if (line) fprintf(stderr, "%d:%d:", line, col + 1); diff --git a/mandoc_aux.c b/mandoc_aux.c index c83da7de..42080278 100644 --- a/mandoc_aux.c +++ b/mandoc_aux.c @@ -28,9 +28,6 @@ #include "mandoc.h" #include "mandoc_aux.h" -#if !HAVE_PROGNAME -const char *mandoc_progname; -#endif int mandoc_asprintf(char **dest, const char *fmt, ...) diff --git a/mandocdb.c b/mandocdb.c index d296c8df..94c1ba5a 100644 --- a/mandocdb.c +++ b/mandocdb.c @@ -183,8 +183,6 @@ static int set_basedir(const char *, int); static int treescan(void); static size_t utf8(unsigned int, char [7]); -extern char *__progname; - static char tempfilename[32]; static int nodb; /* no database changes */ static int mparse_options; /* abort the parse early */ @@ -335,7 +333,7 @@ mandocdb(int argc, char *argv[]) { struct manconf conf; struct mparse *mp; - const char *path_arg; + const char *path_arg, *progname; size_t j, sz; int ch, i; @@ -526,13 +524,13 @@ out: ohash_delete(&mlinks); return exitcode; usage: + progname = getprogname(); fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n" " %s [-aDnpQ] [-Tutf8] dir ...\n" " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n" " %s [-Dnp] -u dir [file ...]\n" " %s [-Q] -t file ...\n", - __progname, __progname, __progname, - __progname, __progname); + progname, progname, progname, progname, progname); return (int)MANDOCLEVEL_BADARG; } diff --git a/test-progname.c b/test-progname.c index 79add7eb..0418b3c0 100644 --- a/test-progname.c +++ b/test-progname.c @@ -1,9 +1,10 @@ -#include - -extern char *__progname; +#include int main(void) { - return !!strcmp(__progname, "test-progname"); + const char * progname; + + progname = getprogname(); + return progname == NULL; } -- cgit