From 89ddb19c0a115febfbaaa51e68a1fb2ca879a456 Mon Sep 17 00:00:00 2001 From: PHO Date: Sat, 27 Dec 2008 23:07:34 +0900 Subject: [PATCH] getprogname(3) wasn't portable enough. --- Makefile.am | 3 ++- configure.ac | 2 ++ daemon.c | 3 +++ getprogname.c | 20 ++++++++++++++++++++ getprogname.h | 10 ++++++++++ pidfile.c | 3 +++ 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 getprogname.c create mode 100644 getprogname.h diff --git a/Makefile.am b/Makefile.am index 4ccbe69..0f60bfd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,11 +4,12 @@ sbin_PROGRAMS = daemon daemon_SOURCES = \ daemon.c \ + getprogname.h \ pidfile.c pidfile.h \ flopen.c flopen.h \ $(NULL) man8_MANS = daemon.8 -EXTRA_DIST = $(man8_MANS) +EXTRA_DIST = $(man8_MANS) getprogname.c AM_CFLAGS = -Wall diff --git a/configure.ac b/configure.ac index bb8b380..f5646c5 100644 --- a/configure.ac +++ b/configure.ac @@ -27,6 +27,8 @@ AC_TYPE_SSIZE_T AC_CHECK_FUNCS([ftruncate]) AC_CHECK_FUNCS([strtol]) AC_CHECK_FUNCS([strerror]) +AC_CHECK_FUNCS([strrchr]) +AC_REPLACE_FUNCS([getprogname]) AC_FUNC_MALLOC AC_CONFIG_FILES([ diff --git a/daemon.c b/daemon.c index 5b769dd..35b7269 100644 --- a/daemon.c +++ b/daemon.c @@ -30,6 +30,7 @@ #include "config.h" #include "pidfile.h" +#include "getprogname.h" #include @@ -51,6 +52,8 @@ main(int argc, char *argv[]) const char *pidfile, *user; pid_t otherpid; + setprogname(argv[0]); + nochdir = noclose = 1; pidfile = user = NULL; while ((ch = getopt(argc, argv, "-cfp:u:")) != -1) { diff --git a/getprogname.c b/getprogname.c new file mode 100644 index 0000000..bff83e4 --- /dev/null +++ b/getprogname.c @@ -0,0 +1,20 @@ +#include "getprogname.h" +#include + +static const char* _progname = NULL; + +const char* getprogname(void) { + return _progname; +} + +void setprogname(const char* progname) { + const char* last_backslash; + + last_backslash = strrchr(progname, '/'); + if (last_backslash) { + _progname = last_backslash + 1; + } + else { + _progname = progname; + } +} diff --git a/getprogname.h b/getprogname.h new file mode 100644 index 0000000..87f1a8d --- /dev/null +++ b/getprogname.h @@ -0,0 +1,10 @@ +#ifndef GETPROGNAME_H_INCLUDED +#define GETPROGNAME_H_INCLUDED +#include "config.h" + +#if !defined(HAVE_GETPROGNAME) +const char* getprogname(void); +void setprogname(const char* progname); +#endif + +#endif diff --git a/pidfile.c b/pidfile.c index b699efb..58790d3 100644 --- a/pidfile.c +++ b/pidfile.c @@ -24,8 +24,11 @@ * SUCH DAMAGE. */ +#include "config.h" + #include "pidfile.h" #include "flopen.h" +#include "getprogname.h" #include #include -- 2.40.0