diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index 11ac634888904079658551fc0722a04274c1d10f..0b5eb27cdba15dc3fdfcfde23395b311e258ee3c 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -200,7 +200,7 @@ fail: char *get_device_name(char *dir_name) { - char path[PATH_MAX], *name; + char path[PATH_MAX], *name = NULL; struct directory *dr, *sub_dr; struct dirent *de; struct stat st; @@ -210,9 +210,10 @@ char *get_device_name(char *dir_name) dr = malloc(sizeof(struct directory)); if (!dr) { perror("malloc"); - goto fail; + goto fail_dr; } dr->path = strdup(dir_name); + // if (!dr->path) ... enqueue(dr, &stack); while (!list_empty(&stack)) { @@ -220,46 +221,49 @@ char *get_device_name(char *dir_name) dir = opendir(dr->path); if (!dir) { perror("opendir"); - goto fail_opendir; + continue; + //goto fail_opendir; } - if (!search(dr->path, &visited)) { - enqueue(dr, &visited); - /* Perhaps make this a <traver_directory> function? */ - while ((de = readdir(dir)) != NULL) { - if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) - continue; + if (search(dr->path, &visited)) + continue; - if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { - perror("fstatat"); - continue; - } + enqueue(dr, &visited); + /* Perhaps make this a <traver_directory> function? */ + while ((de = readdir(dir)) != NULL) { + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; - if (!S_ISDIR(st.st_mode)) - continue; + if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { + perror("fstatat"); + continue; + } - if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { - snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name); + if (!S_ISDIR(st.st_mode)) + continue; - if (!search(path, &visited)) { - sub_dr = malloc(sizeof(struct directory)); - if (!sub_dr) { - perror("malloc"); - goto fail_malloc; - } + if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { + snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name); - sub_dr->path = strdup(path); - enqueue(sub_dr, &stack); + if (!search(path, &visited)) { + sub_dr = malloc(sizeof(struct directory)); + if (!sub_dr) { + perror("malloc"); + goto fail_malloc; } - continue; - } - if (strstr(dr->path, "net")) { - name = strdup(de->d_name); - goto success; + sub_dr->path = strdup(path); + enqueue(sub_dr, &stack); } + continue; + } + + if (strstr(dr->path, "net")) { + name = strdup(de->d_name); + goto success; } } + closedir(dir); } @@ -268,12 +272,11 @@ fail_malloc: closedir(dir); fail_opendir: clear_list(&stack); -fail: - return NULL; success: clear_list(&visited); clear_list(&stack); closedir(dir); +fail_dr: return name; } @@ -340,7 +343,7 @@ char *get_device_ip(char *device_name) { char *iface, *destination, *gateway, *flags, *route, *binary, ipv4_addr[IPV4_MAX]; bool host_flag; - uint32_t dec_addr; + uint32_t dec_addr;// replace with in_addr FILE *fp; fp = fopen("/proc/net/route", "r"); @@ -354,6 +357,7 @@ char *get_device_ip(char *device_name) while ((fgets(route, 1024, fp)) != NULL) { remove_newline(route); + // free us iface = lexer(&route, "\t"); destination = lexer(&route, "\t"); gateway = lexer(&route, "\t"); @@ -369,7 +373,7 @@ char *get_device_ip(char *device_name) dec_addr = strtoul(destination, NULL, IPV4_MAX); inet_ntop(AF_INET, &dec_addr, ipv4_addr, IPV4_MAX); printf("ipv4_addr %s\n", ipv4_addr); - goto success; + goto success; // delete me please }