From d54721b3a44a87be75b83860a955ddc7a0ec6b3d Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Wed, 23 Mar 2011 22:26:57 +0100 Subject: [PATCH] dhcpd-pools.h: portable program_invocation_short_name The program_invocation_short_name portability code is copied from util-linux project almost as is. Reported-by: Roar Pettersen Signed-off-by: Sami Kerola --- configure.ac | 23 +++++++++++++++++++---- src/dhcpd-pools.h | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 5d2550a..a5adc2d 100644 --- a/configure.ac +++ b/configure.ac @@ -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 ], + [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])], diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 6d8df20..4bb9d2b 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -24,13 +24,46 @@ #define _POSIX_SOURCE 1 #define POSIXLY_CORRECT 1 -#ifdef HAVE_STDLIB_H +#ifdef HAVE_STDLIB_H #include #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 +# 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; -- 1.7.10.4