aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_procs.c8
-rw-r--r--plugins/tests/test_utils.c5
-rw-r--r--plugins/utils.c27
-rw-r--r--plugins/utils.h3
4 files changed, 35 insertions, 8 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index 01acc937..d05020b5 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 5aa0028a..67c304a3 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 bb4ffbc2..f2593a16 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 4bbe33d0..ed6243df 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