dhcpd-pools.h: portable program_invocation_short_name
authorSami Kerola <kerolasa@iki.fi>
Wed, 23 Mar 2011 21:26:57 +0000 (22:26 +0100)
committerSami Kerola <kerolasa@iki.fi>
Wed, 23 Mar 2011 21:26:57 +0000 (22:26 +0100)
The program_invocation_short_name portability code is copied
from util-linux project almost as is.

Reported-by: Roar Pettersen <rope1968@gmail.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>

configure.ac
src/dhcpd-pools.h

index 5d2550a..a5adc2d 100644 (file)
@@ -42,12 +42,27 @@ AC_CHECK_FUNCS([inet_ntoa memset strerror strstr strdup])
 
 # GNU commandline parsing in getopt.h present?
 AC_CHECK_HEADERS(getopt.h, AC_DEFINE(HAVE_GETOPT_H),
-        import_sources="getopt.c getopt1.c $import_sources")
+       import_sources="getopt.c getopt1.c $import_sources")
 
 AC_CONFIG_FILES([Makefile
-                 man/Makefile
-                 src/Makefile
-                 contrib/Makefile])
+                man/Makefile
+                src/Makefile
+                contrib/Makefile])
+
+AC_MSG_CHECKING(whether program_invocation_short_name is defined)
+AC_TRY_COMPILE([#include <argp.h>],
+               [program_invocation_short_name = "test";],
+               AC_DEFINE(HAVE_PROGRAM_INVOCATION_SHORT_NAME, 1,
+                       [Define if program_invocation_short_name is defined])
+               AC_MSG_RESULT(yes),
+               AC_MSG_RESULT(no))
+
+AC_MSG_CHECKING([whether __progname is defined])
+AC_LINK_IFELSE([AC_LANG_PROGRAM([extern char *__progname;],
+       [if (*__progname == 0) return;])],
+       AC_DEFINE(HAVE___PROGNAME, 1, [Define if __progname is defined])
+       AC_MSG_RESULT(yes),
+       AC_MSG_RESULT(no))
 
 AC_ARG_WITH(dhcpd-conf,
        [AC_HELP_STRING([--with-dhcpd-conf], [default path of dhcpd.conf])],
index 6d8df20..4bb9d2b 100644 (file)
 #define _POSIX_SOURCE 1
 #define POSIXLY_CORRECT 1
 
-#ifdef  HAVE_STDLIB_H
+#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #else
 extern void exit();
 extern char *malloc();
 #endif                         /* STDC_HEADERS */
 
+#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
+#  ifdef HAVE___PROGNAME
+extern char *__progname;
+#    define program_invocation_short_name __progname
+#  else                                /* HAVE___PROGNAME */
+#    ifdef HAVE_GETEXECNAME
+#      include <stdlib.h>
+#      define program_invocation_short_name \
+       prog_inv_sh_nm_from_file(getexecname(), 0)
+#    else                      /* HAVE_GETEXECNAME */
+#      define program_invocation_short_name \
+       prog_inv_sh_nm_from_file(__FILE__, 1)
+#    endif                     /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
+static char prog_inv_sh_nm_buf[256];
+static inline char *prog_inv_sh_nm_from_file(char *f, char stripext)
+{
+       char *t;
+       if ((t = strrchr(f, '/')) != NULL) {
+               t++;
+       } else {
+               t = f;
+       }
+       strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
+       prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
+
+       if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL) {
+               *t = '\0';
+       }
+       return prog_inv_sh_nm_buf;
+}
+#  endif
+#endif
+
 /* Structures and unions */
 struct configuration_t {
        char *dhcpdconf_file;