]> sourceware.org Git - lvm2.git/commitdiff
Eliminate uses of strdup+basename. Use last_path_component instead.
authorJim Meyering <jim@meyering.net>
Fri, 20 Jul 2007 15:48:39 +0000 (15:48 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 20 Jul 2007 15:48:39 +0000 (15:48 +0000)
* lib/misc/util.c, lib/misc/util.h (last_path_component): New files.
* lib/Makefile.in (SOURCES): Add misc/util.c.
* lib/misc/lib.h: Include "util.h".
* tools/fsadm/fsadm.c: Include "util.h". (_usage): Use last_path_component,
not basename.
* tools/lvmcmdline.c (_find_command, lvm2_main): Likewise.
* include/.symlinks: Add lib/misc/util.h.

WHATS_NEW
include/.symlinks
lib/Makefile.in
lib/misc/lib.h
lib/misc/util.c [new file with mode: 0644]
lib/misc/util.h [new file with mode: 0644]
tools/fsadm/fsadm.c
tools/lvmcmdline.c

index 6d7597e0e0d13dfc1faff7289d9310ffeee96bb5..852a7261c6e0e2d2efbc8731aedcd9a55d028175 100644 (file)
--- a/WHATS_NEW
+++ b/WHATS_NEW
@@ -1,4 +1,5 @@
 Version 2.02.28 -
+  Eliminate uses of strdup+basename.  Use last_path_component instead.
   Use gcc's printf attribute wherever possible.
   In _line_append, use "sizeof buf - 1" rather than equivalent "4095"
   Introduce is_same_inode macro, now including a comparison of st_dev.
index dbd3dbf4628a60fae7e3b009a7fdfa5de6344cbe..a127e2cfa151bacfcc6697d335def8d04e75e26e 100644 (file)
@@ -38,6 +38,7 @@
 ../lib/misc/configure.h
 ../lib/misc/crc.h
 ../lib/misc/intl.h
+../lib/misc/util.h
 ../lib/misc/lib.h
 ../lib/misc/lvm-exec.h
 ../lib/misc/lvm-file.h
index e63f13e80c5bc3de31b45621f3956767854a8a78..2fe732d1b9f9117bd88fa1e0afa1dd2dd4d88dcb 100644 (file)
@@ -80,6 +80,7 @@ SOURCES =\
        misc/lvm-string.c \
        misc/lvm-wrappers.c \
        misc/timestamp.c \
+       misc/util.c \
        mm/memlock.c \
        report/report.c \
        striped/striped.c \
index c3d523185705afb53641d7b70f64fd977ba1c238..baed9fe6c5ad724097e4fd1d9421548de83d3e5c 100644 (file)
@@ -29,6 +29,7 @@
 #include "intl.h"
 #include "lvm-types.h"
 #include "lvm-wrappers.h"
+#include "util.h"
 
 #include <libdevmapper.h>
 
diff --git a/lib/misc/util.c b/lib/misc/util.c
new file mode 100644 (file)
index 0000000..724b657
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ * Return the address of the last file name component of NAME.
+ * If NAME ends in a slash, return the empty string.
+ */
+char *last_path_component(char const *name)
+{
+       char const *slash = strrchr (name, '/');
+       char const *res = slash ? slash + 1 : name;
+       return (char *) res;
+}
diff --git a/lib/misc/util.h b/lib/misc/util.h
new file mode 100644 (file)
index 0000000..afee866
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _LVM_UTIL_H
+#define _LVM_UTIL_H
+
+char *last_path_component(char const *name);
+
+#endif
index 61e428c7a0e27e16f8296241fba3c9e34cc211ff..38bf7116913d02dce40892302b57a8a45cc7de86 100644 (file)
@@ -32,6 +32,8 @@
 #include <sys/mount.h>
 #include <sys/vfs.h>
 
+#include "util.h"
+
 #define log_error(str, x...) fprintf(stderr, "%s(%u):  " str "\n", __FILE__, __LINE__, x)
 
 /* Filesystem related information */
@@ -45,7 +47,7 @@ struct fsinfo {
 static void _usage(const char *cmd)
 {
        log_error("Usage: %s [check <filesystem> | resize <filesystem> <size>]",
-                 basename(cmd));
+                 last_path_component(cmd));
 }
 
 /* FIXME Make this more robust - /proc, multiple mounts, TMPDIR + security etc. */
index 039eb519f25db8b7757ca52309b10badaf555a08..aeb82968543483527428cc2417e0baf86e81c019 100644 (file)
@@ -479,18 +479,15 @@ void lvm_register_commands(void)
 static struct command *_find_command(const char *name)
 {
        int i;
-       char *namebase, *base;
+       char *base;
 
-       namebase = strdup(name);
-       base = basename(namebase);
+       base = last_path_component(name);
 
        for (i = 0; i < _cmdline.num_commands; i++) {
                if (!strcmp(base, _cmdline.commands[i].name))
                        break;
        }
 
-       free(namebase);
-
        if (i >= _cmdline.num_commands)
                return 0;
 
@@ -1138,14 +1135,13 @@ static void _exec_lvm1_command(char **argv)
 
 int lvm2_main(int argc, char **argv, unsigned is_static)
 {
-       char *namebase, *base;
+       char *base;
        int ret, alias = 0;
        struct cmd_context *cmd;
 
        _close_stray_fds();
 
-       namebase = strdup(argv[0]);
-       base = basename(namebase);
+       base = last_path_component(argv[0]);
        while (*base == '/')
                base++;
        if (strcmp(base, "lvm") && strcmp(base, "lvm.static") &&
@@ -1160,8 +1156,6 @@ int lvm2_main(int argc, char **argv, unsigned is_static)
                unsetenv("LVM_DID_EXEC");
        }
 
-       free(namebase);
-
        if (!(cmd = init_lvm(is_static)))
                return -1;
 
This page took 0.04147 seconds and 5 git commands to generate.