Explorar el Código

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.
Guillem Jover hace 15 años
padre
commit
49a0022b72
Se han modificado 7 ficheros con 47 adiciones y 24 borrados
  1. 3 5
      dpkg-split/split.c
  2. 5 6
      lib/dpkg/command.c
  3. 20 1
      lib/dpkg/path.c
  4. 2 1
      lib/dpkg/path.h
  5. 2 8
      lib/dpkg/progname.c
  6. 13 0
      lib/dpkg/test/t-path.c
  7. 2 3
      src/configure.c

+ 3 - 5
dpkg-split/split.c

@@ -3,7 +3,7 @@
  * split.c - splitting archives
  *
  * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2010 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
  *
  * 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);
 	}
 

+ 5 - 6
lib/dpkg/command.c

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * command.c - command execution support
  *
- * Copyright © 2010 Guillem Jover <guillem@debian.org>
+ * Copyright © 2010-2011 Guillem Jover <guillem@debian.org>
  *
  * 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;

+ 20 - 1
lib/dpkg/path.c

@@ -3,7 +3,7 @@
  * path.c - path handling functions
  *
  * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2008-2010 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -72,6 +72,25 @@ path_skip_slash_dotslash(const char *path)
 	return 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.
  *

+ 2 - 1
lib/dpkg/path.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * path.h - path handling routines
  *
- * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
  *
  * 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);

+ 2 - 8
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)

+ 13 - 0
lib/dpkg/test/t-path.c

@@ -73,6 +73,18 @@ test_path_skip(void)
 	test_str(path_skip_slash_dotslash("/./foo/bar/./"), ==, "foo/bar/./");
 }
 
+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)
 {
@@ -162,6 +174,7 @@ test(void)
 {
 	test_path_trim();
 	test_path_skip();
+	test_path_basename();
 	test_path_temp();
 	test_path_quote();
 }

+ 2 - 3
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]") :