diff --git a/dfs.c b/dfs.c index d1e7741d674ccaf79cee0daef4fb1b9bd816b715..8210d66116f5c33ca8702bd08b4e09e420f8991c 100644 --- a/dfs.c +++ b/dfs.c @@ -36,12 +36,12 @@ void add_visited(struct node *n, struct list_head *visited) list_add(&node->list, visited); } -bool is_visited(struct node *n, struct list_head *visited) +bool is_visited(char *path, struct list_head *visited) { struct node *tmp; list_for_each_entry(tmp, visited, list) { - if (strncmp(tmp->path, n->path, 1024) == 0) + if (strncmp(tmp->path, path, 1024) == 0) return true; } diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index c92eb6409ae8bcbcc356f183ce1ac0427c109024..e2eb86b26716e9d5c5dc83cf7794534ec65295ab 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -219,6 +219,21 @@ fail: return NULL; } +/* +DFS(G,v) ( v is the vertex where the search starts ) + Stack S := {}; ( start with an empty stack ) + for each vertex u, set visited[u] := false; + push S, v; + while (S is not empty) do + u := pop S; + if (not visited[u]) then + visited[u] := true; + for each unvisited neighbour w of u + push S, w; + end if + end while + END DFS() +*/ char *dfs_get_path_name(char *folder_name) { char path[PATH_MAX] = {0}, *name; @@ -226,41 +241,50 @@ char *dfs_get_path_name(char *folder_name) struct dirent *de; DIR *dr; FILE *fp; - struct node *n; - - while ((dr = opendir(folder_name))!=NULL) { - if (!dr) { - perror("opendir"); - goto fail_opendir; - } - - n->path = strdup(folder_name); - stack_enqueue(n, &stack); - //while !list_empty(stack) ? eller nått - 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; + struct node *n, new_n; + + n = malloc(sizeof(struct node)); + n->path = strdup(folder_name); + 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; } - if (!S_ISDIR(st.st_mode)) - continue; - - if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { - snprintf(path, PATH_MAX, "%s/%s", folder_name, de->d_name); - name = get_path_name(path); - if (name) - goto success; - continue; - } - - if (strcmp(reverse_lexer(&folder_name, '/'), "net") == 0) { - printf("de->d_name %s\n", de->d_name); - name = strdup(de->d_name); - goto success; + if (!is_visited(n, &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 (!S_ISDIR(st.st_mode)) + continue; + + if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { + snprintf(path, PATH_MAX, "%s/%s", path, de->d_name); + if (!is_visited(path, &visited)) { + new_n = malloc(sizeof(struct node)); + 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; + } + } } } closedir(dr);