From 70ffc4419ef6c14add2b6fcb0b01cf86188a79cf Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakobols@kth.se>
Date: Wed, 30 May 2018 09:55:39 +0200
Subject: [PATCH] working dfs implementation to find device names instead of
 find

---
 dfs.c                   |  4 ++-
 dongle_infrastructure.c | 65 +++++++++++++++++++++--------------------
 2 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/dfs.c b/dfs.c
index 2a4064f..ffb6129 100644
--- a/dfs.c
+++ b/dfs.c
@@ -26,6 +26,8 @@ struct node *stack_dequeue(struct list_head *stack)
 {
 	struct node *n;
 
+	if (list_empty(stack))
+		return NULL;
 	n = list_first_entry(stack, struct node, list);
 	list_del(&n->list);
 
@@ -39,7 +41,7 @@ void add_visited(struct node *n, struct list_head *visited)
 	list_add(&n->list, visited);
 }
 
-bool is_visited(char *path, struct list_head *visited)
+bool is_visited(char *path, struct list_head *visited) // just visited
 {
 	struct node *tmp;
 
diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c
index fe9bb7d..44e957b 100644
--- a/dongle_infrastructure.c
+++ b/dongle_infrastructure.c
@@ -236,7 +236,7 @@ DFS(G,v)   ( v is the vertex where the search starts )
 */
 char *dfs_get_path_name(char *folder_name)
 {
-	char *path, *name;
+	char *path, *name, new_path[PATH_MAX];
 	struct stat st;
 	struct dirent *de;
 	DIR *dr;
@@ -245,44 +245,45 @@ char *dfs_get_path_name(char *folder_name)
 	n = malloc(sizeof(struct node));
 	snprintf(n->path, PATH_MAX, "%s", folder_name); //remain allocated?
 	stack_enqueue(n, &stack);
+
 	while (!list_empty(&stack)) {
 		n = stack_dequeue(&stack);
 		path = n->path;
-		while ((dr = opendir(path))!=NULL) {
-			if (!dr) {
-				perror("opendir");
-				goto fail_opendir;
-			}
+		dr = opendir(path);
+		if (!dr) {
+			perror("opendir");
+			goto fail_opendir;
+		}
 
-			if (!is_visited(path, &visited)) {
-				add_visited(n, &visited);
-				while ((de = readdir(dr)) != NULL) {
-					if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
-						continue;
+		if (!is_visited(path, &visited)) {
+			add_visited(n, &visited);
+			while ((de = readdir(dr)) != NULL) {
+				if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
+					continue;
 
-					if (fstatat(dirfd(dr), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
-						perror("fstatat");
-						continue;
-					}
+				if (fstatat(dirfd(dr), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
+					perror("fstatat");
+					continue;
+				}
 
-					if (!S_ISDIR(st.st_mode))
-						continue;
-
-					if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) {
-						if (!is_visited(path, &visited)) {
-							new_n = malloc(sizeof(struct node));
-							snprintf(new_n->path, PATH_MAX, "%s/%s", path, de->d_name);
-							//new_n->path= strdup(path);
-							stack_enqueue(new_n, &stack);
-						}
-						continue;
-					}
+				if (!S_ISDIR(st.st_mode))
+					continue;
 
-					if (strcmp(reverse_lexer(&path, '/'), "net") == 0) {
-						printf("de->d_name %s\n", de->d_name);
-						name = strdup(de->d_name);
-						goto success;
+				if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) {
+					snprintf(new_path, PATH_MAX, "%s/%s", path, de->d_name);
+					if (!is_visited(new_path, &visited)) {
+						new_n = malloc(sizeof(struct node));
+						strncpy(new_n->path, new_path, (PATH_MAX-1));
+						//new_n->path= strdup(path);
+						stack_enqueue(new_n, &stack);
 					}
+					continue;
+				}
+
+				if (strcmp(reverse_lexer(&path, '/'), "net") == 0) {
+					printf("de->d_name %s\n", de->d_name);
+					name = strdup(de->d_name);
+					goto success;
 				}
 			}
 		}
@@ -348,7 +349,7 @@ char *get_interface_name(char *usb_path, char *sub_dir_name)
 	char path[PATH_MAX], *name;
 
 	snprintf(path, PATH_MAX, "%s/%s/", usb_path, sub_dir_name);
-	return get_path_name(path);
+	return dfs_get_path_name(path);
 }
 
 int get_devices(void)
-- 
GitLab