diff --git a/Makefile b/Makefile index 0b4affe63ae19ed1091d841a8f2bc376eba4be65..59af63a1ff7cce6d6ea302be900e2a5f92815709 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -g -Wall LIBS = -ljson-c -lubox -lubus -lcurl -lmobile -lblobmsg_json -all: libmobile1 libmobile2 common dongle_apn dongle_pin dongle_network dongle_infrastructure dongle +all: libmobile1 libmobile2 common dfs dongle_apn dongle_pin dongle_network dongle_infrastructure dongle MOBJS = libmobile.o MSRCS = libmobile.c @@ -19,6 +19,11 @@ CSRCS = common.c common: ${COBJS} ${CC} -c ${CSRCS} -o ${COBJS} +DFSOBJS = dfs.o +DFSSRCS = dfs.c +dfs: ${DFSOBJS} + ${CC} -c ${DFSSRCS} -o ${DFSOBJS} + DAOBJS = dongle_apn.o DASRCS = dongle_apn.c dongle_apn: ${DAOBJS} @@ -37,10 +42,10 @@ dongle_network: ${DNOBJS} DIOBJS = dongle_infrastructure.o DISRCS = dongle_infrastructure.c dongle_infrastructure: ${DIOBJS} - ${CC} -c ${CFLAGS} ${DISRCS} ${COBJS} -o ${DIOBJS} -L . ${LIBS} + ${CC} -c ${CFLAGS} ${DISRCS} ${COBJS} ${DFSOBJS} -o ${DIOBJS} -L . ${LIBS} dongle: dongle.o - ${CC} ${CFLAGS} dongle.o ${COBJS} ${DNOBJS} ${DPOBJS} ${DAOBJS} ${DIOBJS} -o dongle -L . ${LIBS} + ${CC} ${CFLAGS} dongle.o ${COBJS} ${DNOBJS} ${DPOBJS} ${DAOBJS} ${DIOBJS} ${DFSOBJS} -o dongle -L . ${LIBS} clean: rm -f dongle_apn dongle_pin dongle_network *.o *.so diff --git a/dfs.c b/dfs.c index 6b977984d7fdfe71d306d4ea2b001d93c1bb8dcf..a00a69535c6508fcfd32eed15e8aa821fa78ac09 100644 --- a/dfs.c +++ b/dfs.c @@ -19,7 +19,7 @@ void stack_enqueue(struct node *n, struct list_head *stack) { if (list_empty(stack)) INIT_LIST_HEAD(stack); - list_add(&node->list, stack); + list_add(&n->list, stack); } struct node *stack_dequeue(struct list_head *stack) @@ -27,7 +27,7 @@ struct node *stack_dequeue(struct list_head *stack) struct node *n; n = list_first_entry(stack, struct node, list); - list_del(n->list); + list_del(&n->list); return n; } @@ -36,7 +36,7 @@ void add_visited(struct node *n, struct list_head *visited) { if (list_empty(visited)) INIT_LIST_HEAD(visited); - list_add(&node->list, visited); + list_add(&n->list, visited); } bool is_visited(char *path, struct list_head *visited) @@ -57,7 +57,7 @@ void clear_list(struct list_head *visited) list_for_each_entry_safe(n, tmp, visited, list) { free(n->path); - list_del(n->list); + list_del(&n->list); free(n); } } \ No newline at end of file diff --git a/dfs.h b/dfs.h index ced31136ed76d5b48d6e48880a89a22e58fc9343..f773fc2633af2c0bc19bc57f275a248354c0e588 100644 --- a/dfs.h +++ b/dfs.h @@ -4,12 +4,12 @@ struct node { struct list_head list; - char *path; + char path[PATH_MAX]; //inefficient space complexity with defined memory allocation? char *name; }; -void enqueue_stack(struct node *n, struct list_head *stack); -struct node *dequeue_stack(struct list_head *stack); +void stack_enqueue(struct node *n, struct list_head *stack); +struct node *stack_dequeue(struct list_head *stack); void add_visited(struct node *n, struct list_head *visited); bool is_visited(char *path, struct list_head *visited); void clear_list(struct list_head *visited); diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index 3f50e582f122a87ab46779b414ca75ca1d204640..fe9bb7db36e7f29fab531ec321c4040836a77b81 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -236,15 +236,14 @@ DFS(G,v) ( v is the vertex where the search starts ) */ char *dfs_get_path_name(char *folder_name) { - char path[PATH_MAX] = {0}, *name; + char *path, *name; struct stat st; struct dirent *de; DIR *dr; - FILE *fp; - struct node *n, new_n; + struct node *n, *new_n; n = malloc(sizeof(struct node)); - n->path = strdup(folder_name); + snprintf(n->path, PATH_MAX, "%s", folder_name); //remain allocated? stack_enqueue(n, &stack); while (!list_empty(&stack)) { n = stack_dequeue(&stack); @@ -255,7 +254,7 @@ char *dfs_get_path_name(char *folder_name) goto fail_opendir; } - if (!is_visited(n, &visited)) { + 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) @@ -270,10 +269,10 @@ char *dfs_get_path_name(char *folder_name) 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); + snprintf(new_n->path, PATH_MAX, "%s/%s", path, de->d_name); + //new_n->path= strdup(path); stack_enqueue(new_n, &stack); } continue; @@ -303,7 +302,6 @@ char *get_path_name(char *folder_name) struct stat st; struct dirent *de; DIR *dr; - FILE *fp; dr = opendir(folder_name); if (!dr) { @@ -355,7 +353,7 @@ char *get_interface_name(char *usb_path, char *sub_dir_name) int get_devices(void) { - char usb_path[PATH_MAX], product_path[PATH_MAX], *name; + char usb_path[PATH_MAX] = {0}, product_path[PATH_MAX] = {0}, *name; struct dirent *de; DIR *dir; struct stat st;