getprogname(3) wasn't portable enough.
authorPHO <pho@cielonegro.org>
Sat, 27 Dec 2008 14:07:34 +0000 (23:07 +0900)
committerPHO <pho@cielonegro.org>
Sat, 27 Dec 2008 14:07:34 +0000 (23:07 +0900)
Makefile.am
configure.ac
daemon.c
getprogname.c [new file with mode: 0644]
getprogname.h [new file with mode: 0644]
pidfile.c

index 4ccbe6900aa41c8e1fe858200e35b2bf8069abc5..0f60bfdc1922cfbab731aef604aafd4f780c464a 100644 (file)
@@ -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
index bb8b3806396ea4d2c3e9ed118a4b3d98268e8105..f5646c5f0df03e0a03c00512974451dff4912c35 100644 (file)
@@ -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([
index 5b769dd49863cee1d56c120217edf75d0e870720..35b7269dbd17993bafcbe51a2e2c44dacc450e6c 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -30,6 +30,7 @@
 
 #include "config.h"
 #include "pidfile.h"
+#include "getprogname.h"
 
 #include <sys/param.h>
 
@@ -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 (file)
index 0000000..bff83e4
--- /dev/null
@@ -0,0 +1,20 @@
+#include "getprogname.h"
+#include <string.h>
+
+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 (file)
index 0000000..87f1a8d
--- /dev/null
@@ -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
index b699efb22949fdb53ef6749e457e63b540040440..58790d36ffa3571751ffbbc03d4d01b1b5765c9d 100644 (file)
--- a/pidfile.c
+++ b/pidfile.c
  * SUCH DAMAGE.
  */
 
+#include "config.h"
+
 #include "pidfile.h"
 #include "flopen.h"
+#include "getprogname.h"
 
 #include <sys/cdefs.h>
 #include <sys/file.h>