[PATCH v2 08/23] libusbg: Fix wrong binding parsing.

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

 



usbg_parse_bindings() function has done parsing incorrectly
and in inefficent way. Changes made in this function provide
better performance and fix wrong linking issue when having
two instances of the same function.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 src/usbg.c |   35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/usbg.c b/src/usbg.c
index 198fc7a..8fb61ee 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -408,9 +408,12 @@ static usbg_config_strs *usbg_parse_config_strs(char *path, char *name,
 
 static void usbg_parse_config_bindings(usbg_config *c)
 {
-	int i, n;
+	int i, n, nmb;
 	struct dirent **dent;
 	char bpath[USBG_MAX_PATH_LENGTH];
+	char file_name[USBG_MAX_PATH_LENGTH];
+	char target[USBG_MAX_STR_LENGTH];
+	char *target_name;
 	usbg_gadget *g = c->parent;
 	usbg_binding *b;
 	usbg_function *f;
@@ -420,23 +423,27 @@ static void usbg_parse_config_bindings(usbg_config *c)
 	TAILQ_INIT(&c->bindings);
 
 	n = scandir(bpath, &dent, bindings_select, alphasort);
-	for (i=0; i < n; i++) {
-		TAILQ_FOREACH(f, &g->functions, fnode) {
-			int n;
-			char contents[USBG_MAX_STR_LENGTH];
-			char cpath[USBG_MAX_PATH_LENGTH];
-			char fname[40];
-
-			sprintf(cpath, "%s/%s", bpath, dent[i]->d_name);
-			n = readlink(cpath, contents, USBG_MAX_PATH_LENGTH);
-			if (n<0)
-				ERRORNO("bytes %d contents %s\n", n, contents);
-			strcpy(fname, f->name);
-			if (strstr(contents, strtok(fname, "."))) {
+	for (i = 0; i < n; i++) {
+		sprintf(file_name, "%s/%s", bpath, dent[i]->d_name);
+		nmb = readlink(file_name, target, USBG_MAX_PATH_LENGTH);
+		if (nmb < 0)
+			ERRORNO("bytes %d contents %s\n", n, target);
+
+		/* readlink() don't add this, so we have to do it manually */
+		target[nmb] = '\0';
+		/* Target contains a full path
+		 *  but we need only function dir name */
+		target_name = strrchr(target, '/') + 1;
+
+		TAILQ_FOREACH(f, &g->functions, fnode)
+		{
+			/* Check if this is our target function */
+			if (strcmp(f->name, target_name) == 0) {
 				b = malloc(sizeof(usbg_binding));
 				strcpy(b->name, dent[i]->d_name);
 				strcpy(b->path, bpath);
 				b->target = f;
+				b->parent = c;
 				TAILQ_INSERT_TAIL(&c->bindings, b, bnode);
 				break;
 			}
-- 
1.7.9.5

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




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux