[PATCH 4/7] treewide: replace basename with kbasename

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Nov 25 07:29:24 PST 2024


barebox has three functions for basename():

  - posix_basename: This is basename() as specified by POSIX and
    may modify the buffer pointed at by its char * argument.

  - kbasename: The Linux kernel version never modifies the input and
    takes a const char * to indicate this

  - basename: The GNU version of basename behaves like the kernel
    version, but takes a char * as argument, which is internally casted
    away.

Let's improve const-safety in barebox by explicitly using kbasename.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/nand.c      | 4 ++--
 commands/partition.c | 6 +++---
 commands/tftp.c      | 5 +++--
 common/boot.c        | 2 +-
 common/complete.c    | 8 ++++----
 fs/bpkfs.c           | 8 ++++----
 net/nfs.c            | 8 ++++----
 7 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/commands/nand.c b/commands/nand.c
index d80ec24a7b92..d756092c64c0 100644
--- a/commands/nand.c
+++ b/commands/nand.c
@@ -71,7 +71,7 @@ static int do_nand(int argc, char *argv[])
 
 	if (command == NAND_ADD) {
 		while (optind < argc) {
-			if (dev_add_bb_dev(basename(argv[optind]), NULL))
+			if (dev_add_bb_dev(kbasename(argv[optind]), NULL))
 				return 1;
 
 			optind++;
@@ -82,7 +82,7 @@ static int do_nand(int argc, char *argv[])
 
 	if (command == NAND_DEL) {
 		while (optind < argc) {
-			if (dev_remove_bb_dev(basename(argv[optind])))
+			if (dev_remove_bb_dev(kbasename(argv[optind])))
 				return 1;
 			optind++;
 		}
diff --git a/commands/partition.c b/commands/partition.c
index 14f400ccb433..1b655f853b3b 100644
--- a/commands/partition.c
+++ b/commands/partition.c
@@ -27,7 +27,7 @@
 
 static int do_addpart(int argc, char *argv[])
 {
-	char *devname;
+	const char *devname;
 	loff_t devsize;
 	struct stat s;
 	int opt;
@@ -50,7 +50,7 @@ static int do_addpart(int argc, char *argv[])
 	}
 	devsize = s.st_size;
 
-	devname = basename(argv[optind]);
+	devname = kbasename(argv[optind]);
 
 	return cmdlinepart_do_parse(devname, argv[optind + 1], devsize, flags);
 }
@@ -84,7 +84,7 @@ static int do_delpart(int argc, char *argv[])
 	int i, err;
 
 	for (i = 1; i < argc; i++) {
-		err = devfs_del_partition(basename(argv[i]));
+		err = devfs_del_partition(kbasename(argv[i]));
 		if (err) {
 			printf("cannot delete %s: %s\n", argv[i], strerror(-err));
 			break;
diff --git a/commands/tftp.c b/commands/tftp.c
index 6ac822c9e832..5e8d8d17761d 100644
--- a/commands/tftp.c
+++ b/commands/tftp.c
@@ -18,7 +18,8 @@
 
 static int do_tftpb(int argc, char *argv[])
 {
-	char *source, *dest, *freep;
+	const char *source, *dest;
+	char *freep;
 	int opt;
 	int tftp_push = 0;
 	int port = -1;
@@ -50,7 +51,7 @@ static int do_tftpb(int argc, char *argv[])
 	source = argv[optind++];
 
 	if (argc == optind)
-		dest = basename(source);
+		dest = kbasename(source);
 	else
 		dest = argv[optind];
 
diff --git a/common/boot.c b/common/boot.c
index cbfe6649b3a3..89eb7a974566 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -204,7 +204,7 @@ static int bootscript_create_entry(struct bootentries *bootentries, const char *
 	bs->entry.release = bootscript_entry_release;
 	bs->entry.boot = bootscript_boot;
 	bs->scriptpath = xstrdup(name);
-	bs->entry.title = xstrdup(basename(bs->scriptpath));
+	bs->entry.title = xstrdup(kbasename(bs->scriptpath));
 	bs->entry.description = basprintf("script: %s", name);
 	bootentries_add_entry(bootentries, &bs->entry);
 
diff --git a/common/complete.c b/common/complete.c
index 3911535621b1..5b8b499ed38f 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -43,9 +43,9 @@ static int file_complete(struct string_list *sl, char *instr,
 	DIR *dir;
 	struct dirent *d;
 	char tmp[PATH_MAX];
-	char *base;
+	const char *base;
 
-	base = basename(instr);
+	base = kbasename(instr);
 	dirn = dirn ?: dirname(path);
 
 	dir = opendir(dirn);
@@ -236,12 +236,12 @@ EXPORT_SYMBOL(devicetree_alias_complete);
 int devicetree_nodepath_complete(struct string_list *sl, char *instr)
 {
 	struct device_node *node, *child;
-	char *dirn, *base;
+	const char *dirn, *base;
 	char *path = strdup(instr);
 
 	if (*instr == '/') {
 		dirn = dirname(path);
-		base = basename(instr);
+		base = kbasename(instr);
 		node = of_find_node_by_path(dirn);
 		if (!node)
 			goto out;
diff --git a/fs/bpkfs.c b/fs/bpkfs.c
index ea2c27958520..7b714de66158 100644
--- a/fs/bpkfs.c
+++ b/fs/bpkfs.c
@@ -132,7 +132,7 @@ static int bpkfs_open(struct device *dev, FILE *f, const char *filename)
 	struct bpkfs_handle *priv = dev->priv;
 	struct bpkfs_handle_data *d;
 	struct bpkfs_handle_hw *h;
-	char *dir, *file;
+	const char *dir, *file;
 	int ret = -EINVAL;
 	char *tmp = xstrdup(filename);
 	char *tmp2 = xstrdup(filename);
@@ -146,7 +146,7 @@ static int bpkfs_open(struct device *dev, FILE *f, const char *filename)
 	if (!h)
 		goto out;
 
-	file = basename(tmp2);
+	file = kbasename(tmp2);
 	d = bpkfs_data_get_by_name(h, file);
 	if (!d)
 		goto out;
@@ -284,7 +284,7 @@ static int bpkfs_stat(struct device *dev, const char *filename,
 	struct bpkfs_handle *priv = dev->priv;
 	struct bpkfs_handle_data *d;
 	struct bpkfs_handle_hw *h;
-	char *dir, *file;
+	const char *dir, *file;
 	int ret = -EINVAL;
 	char *tmp = xstrdup(filename);
 	char *tmp2 = xstrdup(filename);
@@ -311,7 +311,7 @@ static int bpkfs_stat(struct device *dev, const char *filename,
 	if (!h)
 		goto out;
 
-	file = basename(tmp2);
+	file = kbasename(tmp2);
 	d = bpkfs_data_get_by_name(h, file);
 	if (!d)
 		goto out;
diff --git a/net/nfs.c b/net/nfs.c
index df0840e4e7dc..68c35e11f6a9 100644
--- a/net/nfs.c
+++ b/net/nfs.c
@@ -147,7 +147,7 @@ static int	nfs_state;
 #define STATE_READLINK_REQ		7
 #define STATE_DONE			8
 
-static char *nfs_filename;
+static const char *nfs_filename;
 static char *nfs_path;
 static char nfs_path_buff[2048];
 
@@ -322,7 +322,7 @@ static void nfs_readlink_req(void)
 /**************************************************************************
 NFS_LOOKUP - Lookup Pathname
 **************************************************************************/
-static void nfs_lookup_req(char *fname)
+static void nfs_lookup_req(const char *fname)
 {
 	uint32_t data[1024];
 	uint32_t *p;
@@ -621,7 +621,7 @@ static void nfs_handler(void *ctx, char *packet, unsigned len)
 
 		debug("Symlink --> %s\n", nfs_path);
 
-		nfs_filename = basename(nfs_path);
+		nfs_filename = kbasename(nfs_path);
 		nfs_path     = dirname(nfs_path);
 
 		nfs_state = STATE_MOUNT_REQ;
@@ -664,7 +664,7 @@ static void nfs_start(char *p)
 
 	strcpy(nfs_path, p);
 
-	nfs_filename = basename (nfs_path);
+	nfs_filename = kbasename (nfs_path);
 	nfs_path     = dirname (nfs_path);
 
 	printf("\nFilename '%s/%s'.\n", nfs_path, nfs_filename);
-- 
2.39.5




More information about the barebox mailing list