From 34cb16d283298885b73f75146433a703e93c0d4f Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 27 Apr 2006 13:25:10 +0000 Subject: [PATCH] Internal version of basename if one not found in system git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1383 f882894a-f735-0410-b71e-b25c423dba1c --- configure.in | 1 + plugins/check_procs.c | 8 +------- plugins/tests/test_utils.c | 5 ++++- plugins/utils.c | 27 +++++++++++++++++++++++++++ plugins/utils.h | 3 +++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/configure.in b/configure.in index ae6253b..1313b2f 100644 --- a/configure.in +++ b/configure.in @@ -600,6 +600,7 @@ AC_TRY_COMPILE([#include ], dnl Checks for library functions. AC_CHECK_FUNCS(memmove select socket strdup strstr strtod strtol strtoul floor) +AC_CHECK_FUNCS(basename) AC_MSG_CHECKING(return type of socket size) AC_TRY_COMPILE([#include diff --git a/plugins/check_procs.c b/plugins/check_procs.c index 01acc93..d05020b 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c @@ -96,9 +96,6 @@ main (int argc, char **argv) char procstat[8]; char procetime[MAX_INPUT_BUFFER] = { '\0' }; char *procargs; -#ifdef HAVE_BASENAME - char *temp_string; -#endif const char *zombie = "Z"; @@ -179,10 +176,7 @@ main (int argc, char **argv) strip (procargs); /* Some ps return full pathname for command. This removes path */ -#ifdef HAVE_BASENAME - temp_string = strdup(procprog); - procprog = basename(temp_string); -#endif /* HAVE_BASENAME */ + procprog = basename(procprog); /* we need to convert the elapsed time to seconds */ procseconds = convert_to_seconds(procetime); diff --git a/plugins/tests/test_utils.c b/plugins/tests/test_utils.c index 5aa0028..67c304a 100644 --- a/plugins/tests/test_utils.c +++ b/plugins/tests/test_utils.c @@ -34,7 +34,7 @@ main (int argc, char **argv) thresholds *thresholds = NULL; int rc; - plan_tests(73); + plan_tests(74); range = parse_range_string("6"); ok( range != NULL, "'6' is valid range"); @@ -165,6 +165,9 @@ main (int argc, char **argv) ok( strcmp(test, "everything") == 0, "everything okay"); free(test); + test = basename("/here/is/a/path"); + ok( strcmp(test, "path") == 0, "basename okay"); + return exit_status(); } diff --git a/plugins/utils.c b/plugins/utils.c index bb4ffbc..f2593a1 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -5,6 +5,7 @@ * Library of useful functions for plugins * * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net) + * Copyright (c) 2006 Nagios Plugin Development Team * License: GPL * * $Revision$ @@ -639,6 +640,32 @@ strpcat (char *dest, const char *src, const char *str) return dest; } +#ifndef HAVE_BASENAME +/* function modified from coreutils base_name function - see ACKNOWLEDGEMENTS */ +char *basename(const char *path) { + char const *base = path; + char const *p; + for (p = base; *p; p++) { + if (*p == '/') { + /* Treat multiple adjacent slashes like single slash */ + do p++; + while (*p == '/'); + + /* If filename ends in slash, use trailing slash + as basename if no non-slashes found */ + if (! *p) { + if (*base == '/') + base = p - 1; + break; + } + + /* *p is non-slash preceded by slash */ + base = p; + } + } + return (char *) base; +} +#endif /****************************************************************************** * diff --git a/plugins/utils.h b/plugins/utils.h index 4bbe33d..ed6243d 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -80,6 +80,9 @@ void set_thresholds(thresholds **, char *, char *); int check_range(double, range *); int get_status(double, thresholds *); +/* I think this needs to be defined even if you use the system version */ +char *basename(const char *path); + #ifndef HAVE_GETTIMEOFDAY int gettimeofday(struct timeval *, struct timezone *); #endif -- 2.30.2