From 6b9cc76d0a27631fbab19a31ab8bd46e143b7580 Mon Sep 17 00:00:00 2001
From: Ton Voon <tonvoon@users.sourceforge.net>
Date: Thu, 13 Jul 2006 08:54:57 +0000
Subject: Using coreutils' base_name function because of portability issues
 with Tru64

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1450 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/configure.in b/configure.in
index 27b2b40..54d5700 100644
--- a/configure.in
+++ b/configure.in
@@ -602,7 +602,7 @@ AC_TRY_COMPILE([#include <sys/time.h>],
 
 dnl Checks for library functions.
 AC_CHECK_FUNCS(memmove select socket strdup strstr strtod strtol strtoul floor)
-AC_CHECK_FUNCS(basename poll)
+AC_CHECK_FUNCS(poll)
 
 AC_MSG_CHECKING(return type of socket size)
 AC_TRY_COMPILE([#include <stdlib.h>
diff --git a/lib/basename.c b/lib/basename.c
new file mode 100644
index 0000000..5cc97cd
--- /dev/null
+++ b/lib/basename.c
@@ -0,0 +1,79 @@
+/* basename.c -- return the last element in a file name
+
+   Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
+   Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "dirname.h"
+#include <string.h>
+
+/* In general, we can't use the builtin `basename' function if available,
+   since it has different meanings in different environments.
+   In some environments the builtin `basename' modifies its argument.
+
+   Return the address of the last file name component of NAME.  If
+   NAME has no file name components because it is all slashes, return
+   NAME if it is empty, the address of its last slash otherwise.  */
+
+char *
+base_name (char const *name)
+{
+  char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
+  char const *p;
+
+  for (p = base; *p; p++)
+    {
+      if (ISSLASH (*p))
+	{
+	  /* Treat multiple adjacent slashes like a single slash.  */
+	  do p++;
+	  while (ISSLASH (*p));
+
+	  /* If the file name ends in slash, use the trailing slash as
+	     the basename if no non-slashes have been found.  */
+	  if (! *p)
+	    {
+	      if (ISSLASH (*base))
+		base = p - 1;
+	      break;
+	    }
+
+	  /* *P is a non-slash preceded by a slash.  */
+	  base = p;
+	}
+    }
+
+  return (char *) base;
+}
+
+/* Return the length of of the basename NAME.  Typically NAME is the
+   value returned by base_name.  Act like strlen (NAME), except omit
+   redundant trailing slashes.  */
+
+size_t
+base_len (char const *name)
+{
+  size_t len;
+
+  for (len = strlen (name);  1 < len && ISSLASH (name[len - 1]);  len--)
+    continue;
+
+  return len;
+}
diff --git a/lib/dirname.h b/lib/dirname.h
new file mode 100644
index 0000000..1688ae8
--- /dev/null
+++ b/lib/dirname.h
@@ -0,0 +1,47 @@
+/*  Take file names apart into directory and base names.
+
+    Copyright (C) 1998, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2, or (at your option)
+    any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software Foundation,
+    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifndef DIRNAME_H_
+# define DIRNAME_H_ 1
+
+# include <stdbool.h>
+# include <stddef.h>
+
+# ifndef DIRECTORY_SEPARATOR
+#  define DIRECTORY_SEPARATOR '/'
+# endif
+
+# ifndef ISSLASH
+#  define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
+# endif
+
+# ifndef FILE_SYSTEM_PREFIX_LEN
+#  define FILE_SYSTEM_PREFIX_LEN(File_name) 0
+# endif
+
+# define IS_ABSOLUTE_FILE_NAME(F) ISSLASH ((F)[FILE_SYSTEM_PREFIX_LEN (F)])
+# define IS_RELATIVE_FILE_NAME(F) (! IS_ABSOLUTE_FILE_NAME (F))
+
+char *base_name (char const *file);
+char *dir_name (char const *file);
+size_t base_len (char const *file);
+size_t dir_len (char const *file);
+
+bool strip_trailing_slashes (char *file);
+
+#endif /* not DIRNAME_H_ */
diff --git a/m4/basename.m4 b/m4/basename.m4
new file mode 100644
index 0000000..69a0996
--- /dev/null
+++ b/m4/basename.m4
@@ -0,0 +1,14 @@
+# basename.m4 serial 1
+dnl Copyright (C) 2005 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_BASENAME],
+[
+  AC_LIBSOURCES([basename.c, dirname.h])
+  AC_LIBOBJ([basename])
+
+  dnl Prerequisites of lib/basename.c.
+  AC_REQUIRE([gl_AC_DOS])
+])
diff --git a/m4/dos.m4 b/m4/dos.m4
new file mode 100644
index 0000000..0713cf1
--- /dev/null
+++ b/m4/dos.m4
@@ -0,0 +1,58 @@
+#serial 9
+
+# Define some macros required for proper operation of code in lib/*.c
+# on MSDOS/Windows systems.
+
+# Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# From Jim Meyering.
+
+AC_DEFUN([gl_AC_DOS],
+  [
+    AC_CACHE_CHECK([whether system is Windows or MSDOS], [ac_cv_win_or_dos],
+      [
+        AC_TRY_COMPILE([],
+        [#if !defined _WIN32 && !defined __WIN32__ && !defined __MSDOS__ && !defined __CYGWIN__
+neither MSDOS nor Windows
+#endif],
+        [ac_cv_win_or_dos=yes],
+        [ac_cv_win_or_dos=no])
+      ])
+
+    if test x"$ac_cv_win_or_dos" = xyes; then
+      ac_fs_accepts_drive_letter_prefix=1
+      ac_fs_backslash_is_file_name_separator=1
+    else
+      ac_fs_accepts_drive_letter_prefix=0
+      ac_fs_backslash_is_file_name_separator=0
+    fi
+
+    AH_VERBATIM(FILE_SYSTEM_PREFIX_LEN,
+    [#if FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX
+# define FILE_SYSTEM_PREFIX_LEN(Filename) \
+  ((Filename)[0] && (Filename)[1] == ':' ? 2 : 0)
+#else
+# define FILE_SYSTEM_PREFIX_LEN(Filename) 0
+#endif])
+
+    AC_DEFINE_UNQUOTED([FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX],
+      $ac_fs_accepts_drive_letter_prefix,
+      [Define on systems for which file names may have a so-called
+       `drive letter' prefix, define this to compute the length of that
+       prefix, including the colon.])
+
+    AH_VERBATIM(ISSLASH,
+    [#if FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR
+# define ISSLASH(C) ((C) == '/' || (C) == '\\')
+#else
+# define ISSLASH(C) ((C) == '/')
+#endif])
+
+    AC_DEFINE_UNQUOTED([FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR],
+      $ac_fs_backslash_is_file_name_separator,
+      [Define if the backslash character may also serve as a file name
+       component separator.])
+  ])
diff --git a/m4/np_coreutils.m4 b/m4/np_coreutils.m4
index 5360bbd..77cb9f6 100644
--- a/m4/np_coreutils.m4
+++ b/m4/np_coreutils.m4
@@ -11,6 +11,7 @@ dnl Usually in coreutils' prereq.m4, but this is a subset that we need
 AC_DEFUN([np_COREUTILS],
 [
   AC_REQUIRE([AM_STDBOOL_H])
+  AC_REQUIRE([gl_BASENAME])
   AC_REQUIRE([gl_C_STRTOLD])
   AC_REQUIRE([gl_EXITFAIL])
   AC_REQUIRE([gl_FCNTL_SAFER])
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index f6438f2..82a21eb 100644
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
@@ -189,7 +189,7 @@ main (int argc, char **argv)
 			strip (procargs);
 
 			/* Some ps return full pathname for command. This removes path */
-			procprog = basename(procprog);
+			procprog = base_name(procprog);
 
 			/* we need to convert the elapsed time to seconds */
 			procseconds = convert_to_seconds(procetime);
diff --git a/plugins/utils.c b/plugins/utils.c
index a455f22..cb01341 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -640,33 +640,6 @@ 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
-
 /******************************************************************************
  *
  * Print perfdata in a standard format
diff --git a/plugins/utils.h b/plugins/utils.h
index ed6243d..4bbe33d 100644
--- a/plugins/utils.h
+++ b/plugins/utils.h
@@ -80,9 +80,6 @@ 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
-- 
cgit v0.10-9-g596f

