diff --git a/dfs.c b/dfs.c new file mode 100644 index 0000000000000000000000000000000000000000..d1e7741d674ccaf79cee0daef4fb1b9bd816b715 --- /dev/null +++ b/dfs.c @@ -0,0 +1,49 @@ +/* +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() +*/ +#include "dfs.h" + +void stack_enqueue(struct node *n, struct list_head *stack) +{ + if (list_empty(stack)) + INIT_LIST_HEAD(stack); + list_add(&node->list, stack); +} + +struct node *stack_dequeue(struct list_head *stack) +{ + struct node *n; + + return list_first_entry(stack, struct node, list); +} + +void add_visited(struct node *n, struct list_head *visited) +{ + if (list_empty(visited)) + INIT_LIST_HEAD(visited); + list_add(&node->list, visited); +} + +bool is_visited(struct node *n, struct list_head *visited) +{ + struct node *tmp; + + list_for_each_entry(tmp, visited, list) { + if (strncmp(tmp->path, n->path, 1024) == 0) + return true; + } + + return false; +} \ No newline at end of file diff --git a/dfs.h b/dfs.h new file mode 100644 index 0000000000000000000000000000000000000000..4666f5ea996397d239f7d9a4f4475c1048ab323d --- /dev/null +++ b/dfs.h @@ -0,0 +1,15 @@ +#ifndef DFS_H +#define DFS_H +#include "common.h" + +struct node { + struct list_head list; + char *path; + char *name; +}; + +void enqueue_stack(struct node *n, struct list_head *stack); +struct node *dequeue_stack(struct list_head *stack); +void add_visited(struct node *n, struct list_head *visited); +bool is_visited(struct node *n, struct list_head *visited); +#endif \ No newline at end of file diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index 4f8a4f0e1da95c22d0e7b523742ce181306eb02a..db55b2086b49c9759982edf44c54e6d1a44ff3de 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -17,6 +17,7 @@ #include "dongle_infrastructure.h" #include "dongle.h" +#include "dfs.h" enum { DEV, @@ -28,6 +29,8 @@ const struct blobmsg_policy dev_policy[__DEV_MAX] = { }; LIST_HEAD(devices); +LIST_HEAD(visited); +LIST_HEAD(unvisited); int devices_status(struct uloop_timeout *t) {