Revision: 44750
          http://brlcad.svn.sourceforge.net/brlcad/?rev=44750&view=rev
Author:   brlcad
Date:     2011-06-06 20:09:41 +0000 (Mon, 06 Jun 2011)

Log Message:
-----------
expand bu_basename() support to more closely mimic basename() while ensuring 
that NULL will not be returned.  return '.' for NULL and empty paths like 
basename() does.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/basename.c

Modified: brlcad/trunk/src/libbu/basename.c
===================================================================
--- brlcad/trunk/src/libbu/basename.c   2011-06-06 20:03:12 UTC (rev 44749)
+++ brlcad/trunk/src/libbu/basename.c   2011-06-06 20:09:41 UTC (rev 44750)
@@ -31,9 +31,12 @@
     int len;
 
     if (UNLIKELY(!str)) {
-       return NULL;
+       base_str = bu_malloc(MAXPATHLEN, "bu_basename empty");;
+       base_str[0] = '.';
+       return base_str;
     }
 
+    /* Skip leading '/'s */
     while (*p != '\0')
        if (*p++ == '/' && *p != '/' && *p != '\0')
            str = p;
@@ -41,12 +44,17 @@
     len = strlen(str);
     
     /* Remove trailing '/'s */
-    while (len > 1 && str[len - 1] == '/') len--;
+    while (len > 1 && str[len - 1] == '/')
+       len--;
     
     /* Create a new string */
-    base_str = bu_malloc(sizeof(char) * (len + 1), "bu_basename alloc");
-    bu_strlcpy(base_str, str, len + 1);
-    base_str[len] = '\0';
+    base_str = bu_calloc(len + 2, sizeof(char), "bu_basename alloc");
+    if (len > 0) {
+       bu_strlcpy(base_str, str, len+1);
+    } else {
+       base_str[0] = '.';
+    }
+
     return base_str;
 }
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Discover what all the cheering's about.
Get your free trial download today. 
http://p.sf.net/sfu/quest-dev2dev2 
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to