The following commit has been merged in the master branch:
commit 49a0022b72df33589fc416c30dc33a82f5f58ea6
Author: Guillem Jover <[email protected]>
Date:   Sat Sep 17 15:58:51 2011 +0200

    libdpkg: Refactor path basename code into new path_basename function
    
    This function is an equivalent of the GNU basename, but this one will
    work consistently on any system regardless of libc used.

diff --git a/dpkg-split/split.c b/dpkg-split/split.c
index 1b40f34..cae4531 100644
--- a/dpkg-split/split.c
+++ b/dpkg-split/split.c
@@ -3,7 +3,7 @@
  * split.c - splitting archives
  *
  * Copyright © 1995 Ian Jackson <[email protected]>
- * Copyright © 2010 Guillem Jover <[email protected]>
+ * Copyright © 2008-2011 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -40,6 +40,7 @@
 #include <dpkg/i18n.h>
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/path.h>
 #include <dpkg/subproc.h>
 #include <dpkg/buffer.h>
 #include <dpkg/ar.h>
@@ -160,10 +161,7 @@ mksplit(const char *file_src, const char *prefix, off_t 
maxpartsize,
                prefixdir = m_strdup(dirname(t));
                free(t);
 
-               t = m_strdup(prefix);
-               msdos_prefix = m_strdup(basename(t));
-               free(t);
-
+               msdos_prefix = m_strdup(path_basename(prefix));
                prefix = clean_msdos_filename(msdos_prefix);
        }
 
diff --git a/lib/dpkg/command.c b/lib/dpkg/command.c
index a711bfa..374e3a7 100644
--- a/lib/dpkg/command.c
+++ b/lib/dpkg/command.c
@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * command.c - command execution support
  *
- * Copyright © 2010 Guillem Jover <[email protected]>
+ * Copyright © 2010-2011 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/i18n.h>
+#include <dpkg/path.h>
 #include <dpkg/command.h>
 
 /**
@@ -43,11 +44,9 @@ void
 command_init(struct command *cmd, const char *filename, const char *name)
 {
        cmd->filename = filename;
-       if (name == NULL) {
-               const char *progname = strrchr(filename, '/');
-
-               cmd->name = progname ? progname + 1 : filename;
-       } else
+       if (name == NULL)
+               cmd->name = path_basename(filename);
+       else
                cmd->name = name;
        cmd->argc = 0;
        cmd->argv_size = 10;
diff --git a/lib/dpkg/path.c b/lib/dpkg/path.c
index cdb77b3..84b152d 100644
--- a/lib/dpkg/path.c
+++ b/lib/dpkg/path.c
@@ -3,7 +3,7 @@
  * path.c - path handling functions
  *
  * Copyright © 1995 Ian Jackson <[email protected]>
- * Copyright © 2008-2010 Guillem Jover <[email protected]>
+ * Copyright © 2008-2011 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -73,6 +73,25 @@ path_skip_slash_dotslash(const char *path)
 }
 
 /**
+ * Return the last component of a pathname.
+ *
+ * @param path The pathname to get the base name from.
+ *
+ * @return A pointer to the last component inside pathname.
+ */
+const char *
+path_basename(const char *path)
+{
+       const char *last_slash;
+
+       last_slash = strrchr(path, '/');
+       if (last_slash == NULL)
+               return path;
+       else
+               return last_slash + 1;
+}
+
+/**
  * Create a template for a temporary pathname.
  *
  * @param suffix The suffix to use for the template string.
diff --git a/lib/dpkg/path.h b/lib/dpkg/path.h
index 52d91a6..e3687c7 100644
--- a/lib/dpkg/path.h
+++ b/lib/dpkg/path.h
@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * path.h - path handling routines
  *
- * Copyright © 2008, 2009 Guillem Jover <[email protected]>
+ * Copyright © 2008-2011 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,6 +29,7 @@ DPKG_BEGIN_DECLS
 
 size_t path_trim_slash_slashdot(char *path);
 const char *path_skip_slash_dotslash(const char *path);
+const char *path_basename(const char *path);
 char *path_quote_filename(char *dst, const char *src, size_t size);
 
 char *path_make_temp_template(const char *suffix);
diff --git a/lib/dpkg/progname.c b/lib/dpkg/progname.c
index 38ec71e..abd5df5 100644
--- a/lib/dpkg/progname.c
+++ b/lib/dpkg/progname.c
@@ -22,9 +22,9 @@
 #include <compat.h>
 
 #include <errno.h>
-#include <string.h>
 #include <stdlib.h>
 
+#include <dpkg/path.h>
 #include <dpkg/progname.h>
 
 static const char *progname;
@@ -40,13 +40,7 @@ static const char *progname;
 void
 dpkg_set_progname(const char *name)
 {
-       const char *last_slash;
-
-       last_slash = strrchr(name, '/');
-       if (last_slash == NULL)
-               progname = name;
-       else
-               progname = last_slash + 1;
+       progname = path_basename(name);
 }
 
 #if defined(HAVE___PROGNAME)
diff --git a/lib/dpkg/test/t-path.c b/lib/dpkg/test/t-path.c
index 32e79ed..bb87c29 100644
--- a/lib/dpkg/test/t-path.c
+++ b/lib/dpkg/test/t-path.c
@@ -74,6 +74,18 @@ test_path_skip(void)
 }
 
 static void
+test_path_basename(void)
+{
+       test_str(path_basename("./."), ==, ".");
+       test_str(path_basename("./"), ==, "");
+       test_str(path_basename("/."), ==, ".");
+       test_str(path_basename("/"), ==, "");
+       test_str(path_basename("/foo"), ==, "foo");
+       test_str(path_basename("/foo/bar"), ==, "bar");
+       test_str(path_basename("/foo/bar/"), ==, "");
+}
+
+static void
 test_path_temp(void)
 {
        char *template;
@@ -162,6 +174,7 @@ test(void)
 {
        test_path_trim();
        test_path_skip();
+       test_path_basename();
        test_path_temp();
        test_path_quote();
 }
diff --git a/src/configure.c b/src/configure.c
index c75e8c3..a925baf 100644
--- a/src/configure.c
+++ b/src/configure.c
@@ -45,6 +45,7 @@
 #include <dpkg/string.h>
 #include <dpkg/buffer.h>
 #include <dpkg/file.h>
+#include <dpkg/path.h>
 #include <dpkg/subproc.h>
 #include <dpkg/command.h>
 #include <dpkg/triglib.h>
@@ -659,9 +660,7 @@ promptconfaction(struct pkginfo *pkg, const char *cfgfile,
                else if (what & cfof_install)
                        fprintf(stderr, _(" The default action is to install 
the new version.\n"));
 
-               s = strrchr(cfgfile, '/');
-               if (!s || !*++s)
-                       s = cfgfile;
+               s = path_basename(cfgfile);
                fprintf(stderr, "*** %s (Y/I/N/O/D/Z) %s ? ",
                        s,
                        (what & cfof_keep) ? _("[default=N]") :

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to