[PATCH 06/16] modinfo: clean up grab_module()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Reorganize pathname building and use asprintf and strndup instead
of malloc, sprintf and memcpy.

Module file names are now truncated *after* copying.
It wastes a few bytes, but makes the code much easier to read.

Signed-off-by: Andreas Robinson <andr345@xxxxxxxxx>
---
 modinfo.c |   47 +++++++++++++++++------------------------------
 1 files changed, 17 insertions(+), 30 deletions(-)

diff --git a/modinfo.c b/modinfo.c
index 4f16c78..8e3b701 100644
--- a/modinfo.c
+++ b/modinfo.c
@@ -197,22 +197,15 @@ static void *grab_module(const char *name, unsigned long *size, char**filename,
 		}
 	}
 
-	if (kernel) {
-		if (strlen(basedir))
-			asprintf(&moddir, "%s/%s/%s",
-				basedir, MODULE_DIR, kernel);
-		else
-			asprintf(&moddir, "%s/%s",
-				MODULE_DIR, kernel);
-	} else {
+	if (!kernel) {
 		uname(&buf);
-		if (strlen(basedir))
-			asprintf(&moddir, "%s/%s/%s",
-			 	basedir, MODULE_DIR, buf.release);
-		else
-			asprintf(&moddir, "%s/%s",
-				MODULE_DIR, buf.release);
+		kernel = buf.release;
 	}
+	if (strlen(basedir))
+		asprintf(&moddir, "%s/%s/%s", basedir, MODULE_DIR, kernel);
+	else
+		asprintf(&moddir, "%s/%s", MODULE_DIR, kernel);
+
 	asprintf(&depname, "%s/%s", moddir, "modules.dep");
 
 	/* Search for it in modules.dep. */
@@ -227,24 +220,18 @@ static void *grab_module(const char *name, unsigned long *size, char**filename,
 	for (p = data; p < data + *size; p = next_line(p, data + *size)) {
 		if (name_matches(p, data + *size, name)) {
 			int namelen = strcspn(p, ":");
+			const char *dir;
+
+			if ('/' == p[0])
+				dir = basedir; /* old style deps - abs. path */
+			else
+				dir = moddir; /* new style - relative path */
 
-			if ('/' == p[0]) { /* old style deps - absolute path */
-				*filename = malloc(namelen + strlen(basedir)+2);
-				if (strlen(basedir)) {
-					sprintf(*filename, "%s/", basedir);
-					memcpy(*filename+strlen(basedir)+1,p,
-						namelen);
-					(*filename)[namelen
-						+strlen(basedir)+1] = '\0';
-				} else {
-					memcpy(*filename,p,namelen);
-					(*filename)[namelen] = '\0';
-				}
+			if (strlen(dir)) {
+				asprintf(filename, "%s/%s", dir, p);
+				(*filename)[namelen + strlen(dir) + 1] = '\0';
 			} else {
-				*filename = malloc(namelen + strlen(moddir)+2);
-				sprintf(*filename, "%s/", moddir);
-				memcpy(*filename+strlen(moddir)+1, p,namelen);
-				(*filename)[namelen+strlen(moddir)+1] ='\0';
+				*filename = strndup(p, namelen);
 			}
 			release_file(data, *size);
 			data = grab_file(*filename, size);
-- 
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-modules" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux