From 3fffa6c6072826f70aebe72863968bffb9d714cc Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Fri, 6 Nov 2015 16:30:33 +0000 Subject: [PATCH] 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 | 4 +++- compat_err.c | 6 ++---- compat_progname.c | 42 ++++++++++++++++++++++++++++++++++++++++++ configure | 6 +++++- main.c | 35 +++++++++++++++++++---------------- mandoc_aux.c | 5 +---- mandocdb.c | 10 ++++------ test-progname.c | 9 +++++---- 8 files changed, 81 insertions(+), 36 deletions(-) create mode 100644 compat_progname.c diff --git a/Makefile b/Makefile index ec9eec60..cb2a2095 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.472 2015/11/05 17:47:51 schwarze Exp $ +# $Id: Makefile,v 1.473 2015/11/06 16:30:33 schwarze Exp $ # # Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons # Copyright (c) 2011, 2013, 2014, 2015 Ingo Schwarze @@ -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 5107c460..3a5bac03 100644 --- a/compat_err.c +++ b/compat_err.c @@ -6,7 +6,7 @@ int dummy; #else -/* $Id: compat_err.c,v 1.1 2015/10/11 21:12:54 schwarze Exp $ */ +/* $Id: compat_err.c,v 1.2 2015/11/06 16:30:33 schwarze Exp $ */ /* * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. @@ -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..9840cc7c --- /dev/null +++ b/compat_progname.c @@ -0,0 +1,42 @@ +#include "config.h" + +#if HAVE_PROGNAME + +int dummy; + +#else + +/* $Id: compat_progname.c,v 1.1 2015/11/06 16:30:33 schwarze Exp $ */ +/* + * 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 31dc9994..1267bbfd 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.253 2015/10/22 21:54:23 schwarze Exp $ */ +/* $Id: main.c,v 1.254 2015/11/06 16:30:33 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -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 ee37f04d..fa72ba0d 100644 --- a/mandoc_aux.c +++ b/mandoc_aux.c @@ -1,4 +1,4 @@ -/* $Id: mandoc_aux.c,v 1.7 2015/10/12 21:09:54 schwarze Exp $ */ +/* $Id: mandoc_aux.c,v 1.8 2015/11/06 16:30:33 schwarze Exp $ */ /* * Copyright (c) 2009, 2011 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -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 cf7d6f9e..26c59b62 100644 --- a/mandocdb.c +++ b/mandocdb.c @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.205 2015/10/22 22:06:43 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.206 2015/11/06 16:30:33 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2015 Ingo Schwarze @@ -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; } -- 2.47.1