]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/message.c
Fix get_basename() -- rewrite
[bacula/bacula] / bacula / src / lib / message.c
index 5af7851074e1b4ad9fd9bae88125e44854c4f93b..48cd7a600be078dc35b93d709d7d3e95ab1c392d 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -92,6 +92,24 @@ const char *host_os = HOST_OS;
 const char *distname = DISTNAME;
 const char *distver = DISTVER;
 
+/*
+ * Walk back in a string from end looking for a
+ *  path separator.  
+ *  This routine is passed the start of the string and
+ *  the end of the string, it returns either the beginning
+ *  of the string or where it found a path separator.
+ */
+static const char *bstrrpath(const char *start, const char *end)
+{
+   while ( end > start ) {
+      end--;   
+      if (IsPathSeparator(*end)) {
+         break;
+      }
+   }
+   return end;
+}
+
 /* Some message class methods */
 void MSGS::lock()
 {
@@ -666,7 +684,7 @@ static bool open_dest_file(JCR *jcr, DEST *d, const char *mode)
 }
 
 /* Split the output for syslog (it converts \n to ' ' and is
- * limited to 1024c
+ *   limited to 1024 characters per syslog message
  */
 static void send_to_syslog(int mode, const char *msg)
 {
@@ -675,9 +693,8 @@ static void send_to_syslog(int mode, const char *msg)
    const char *p2;
    const char *p = msg;
 
-   while (*p && ((p2 = strchr(p, '\n')) != NULL))
-   {
-      len = MIN(sizeof(buf) - 1, p2 - p + 1); /* Add 1 to keep \n */
+   while (*p && ((p2 = strchr(p, '\n')) != NULL)) {
+      len = MIN((int)sizeof(buf) - 1, p2 - p + 1); /* Add 1 to keep \n */
       strncpy(buf, p, len);
       buf[len] = 0;
       syslog(mode, "%s", buf);
@@ -938,17 +955,15 @@ send_to_file:
 
 const char *get_basename(const char *pathname)
 {
-   const char *basename, *basename2;
+   const char *basename;
    
-   if ((basename = strrchr(pathname, PathSeparator)) == NULL) {
-      basename = pathname;
-      if ((basename2 = strrchr(pathname, PathSeparator)) != NULL) {
-         basename = basename2 + 1;
-      }
+   if ((basename = bstrrpath(pathname, pathname+strlen(pathname))) == pathname) {
+      /* empty */
+   } else if ((basename = bstrrpath(pathname, basename-1)) == pathname) {
+      /* empty */
    } else {
       basename++;
    }
-
    return basename;
 }