dhcpd-pools.h: portable program_invocation_short_name
[debian/dhcpd-pools.git] / src / dhcpd-pools.h
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;