From: Jim Meyering Date: Fri, 20 Jul 2007 15:48:39 +0000 (+0000) Subject: Eliminate uses of strdup+basename. Use last_path_component instead. X-Git-Tag: v2_02_91~4056 X-Git-Url: https://sourceware.org/git/?a=commitdiff_plain;h=6c35e3c84c7f785dd7266b86be3f6ba2698551ac;p=lvm2.git Eliminate uses of strdup+basename. Use last_path_component instead. * 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. --- diff --git a/WHATS_NEW b/WHATS_NEW index 6d7597e0e..852a7261c 100644 --- 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. diff --git a/include/.symlinks b/include/.symlinks index dbd3dbf46..a127e2cfa 100644 --- a/include/.symlinks +++ b/include/.symlinks @@ -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 diff --git a/lib/Makefile.in b/lib/Makefile.in index e63f13e80..2fe732d1b 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -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 \ diff --git a/lib/misc/lib.h b/lib/misc/lib.h index c3d523185..baed9fe6c 100644 --- a/lib/misc/lib.h +++ b/lib/misc/lib.h @@ -29,6 +29,7 @@ #include "intl.h" #include "lvm-types.h" #include "lvm-wrappers.h" +#include "util.h" #include diff --git a/lib/misc/util.c b/lib/misc/util.c new file mode 100644 index 000000000..724b65712 --- /dev/null +++ b/lib/misc/util.c @@ -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 index 000000000..afee86601 --- /dev/null +++ b/lib/misc/util.h @@ -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 diff --git a/tools/fsadm/fsadm.c b/tools/fsadm/fsadm.c index 61e428c7a..38bf71169 100644 --- a/tools/fsadm/fsadm.c +++ b/tools/fsadm/fsadm.c @@ -32,6 +32,8 @@ #include #include +#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 | resize ]", - basename(cmd)); + last_path_component(cmd)); } /* FIXME Make this more robust - /proc, multiple mounts, TMPDIR + security etc. */ diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index 039eb519f..aeb829685 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -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;