Skip to content
Snippets Groups Projects
Commit 37c5e8dc authored by Ionuț-Alex Oprea's avatar Ionuț-Alex Oprea
Browse files

some suggestions2

parent 13d282c2
No related tags found
No related merge requests found
...@@ -200,7 +200,7 @@ fail: ...@@ -200,7 +200,7 @@ fail:
char *get_device_name(char *dir_name) char *get_device_name(char *dir_name)
{ {
char path[PATH_MAX], *name; char path[PATH_MAX], *name = NULL;
struct directory *dr, *sub_dr; struct directory *dr, *sub_dr;
struct dirent *de; struct dirent *de;
struct stat st; struct stat st;
...@@ -210,9 +210,10 @@ char *get_device_name(char *dir_name) ...@@ -210,9 +210,10 @@ char *get_device_name(char *dir_name)
dr = malloc(sizeof(struct directory)); dr = malloc(sizeof(struct directory));
if (!dr) { if (!dr) {
perror("malloc"); perror("malloc");
goto fail; goto fail_dr;
} }
dr->path = strdup(dir_name); dr->path = strdup(dir_name);
// if (!dr->path) ...
enqueue(dr, &stack); enqueue(dr, &stack);
while (!list_empty(&stack)) { while (!list_empty(&stack)) {
...@@ -220,46 +221,49 @@ char *get_device_name(char *dir_name) ...@@ -220,46 +221,49 @@ char *get_device_name(char *dir_name)
dir = opendir(dr->path); dir = opendir(dr->path);
if (!dir) { if (!dir) {
perror("opendir"); perror("opendir");
goto fail_opendir; continue;
//goto fail_opendir;
} }
if (!search(dr->path, &visited)) { if (search(dr->path, &visited))
enqueue(dr, &visited); continue;
/* 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 (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { enqueue(dr, &visited);
perror("fstatat"); /* Perhaps make this a <traver_directory> function? */
continue; while ((de = readdir(dir)) != NULL) {
} if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
if (!S_ISDIR(st.st_mode)) if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
continue; perror("fstatat");
continue;
}
if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { if (!S_ISDIR(st.st_mode))
snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name); continue;
if (!search(path, &visited)) { if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) {
sub_dr = malloc(sizeof(struct directory)); snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name);
if (!sub_dr) {
perror("malloc");
goto fail_malloc;
}
sub_dr->path = strdup(path); if (!search(path, &visited)) {
enqueue(sub_dr, &stack); sub_dr = malloc(sizeof(struct directory));
if (!sub_dr) {
perror("malloc");
goto fail_malloc;
} }
continue;
}
if (strstr(dr->path, "net")) { sub_dr->path = strdup(path);
name = strdup(de->d_name); enqueue(sub_dr, &stack);
goto success;
} }
continue;
}
if (strstr(dr->path, "net")) {
name = strdup(de->d_name);
goto success;
} }
} }
closedir(dir); closedir(dir);
} }
...@@ -268,12 +272,11 @@ fail_malloc: ...@@ -268,12 +272,11 @@ fail_malloc:
closedir(dir); closedir(dir);
fail_opendir: fail_opendir:
clear_list(&stack); clear_list(&stack);
fail:
return NULL;
success: success:
clear_list(&visited); clear_list(&visited);
clear_list(&stack); clear_list(&stack);
closedir(dir); closedir(dir);
fail_dr:
return name; return name;
} }
...@@ -340,7 +343,7 @@ char *get_device_ip(char *device_name) ...@@ -340,7 +343,7 @@ char *get_device_ip(char *device_name)
{ {
char *iface, *destination, *gateway, *flags, *route, *binary, ipv4_addr[IPV4_MAX]; char *iface, *destination, *gateway, *flags, *route, *binary, ipv4_addr[IPV4_MAX];
bool host_flag; bool host_flag;
uint32_t dec_addr; uint32_t dec_addr;// replace with in_addr
FILE *fp; FILE *fp;
fp = fopen("/proc/net/route", "r"); fp = fopen("/proc/net/route", "r");
...@@ -354,6 +357,7 @@ char *get_device_ip(char *device_name) ...@@ -354,6 +357,7 @@ char *get_device_ip(char *device_name)
while ((fgets(route, 1024, fp)) != NULL) { while ((fgets(route, 1024, fp)) != NULL) {
remove_newline(route); remove_newline(route);
// free us
iface = lexer(&route, "\t"); iface = lexer(&route, "\t");
destination = lexer(&route, "\t"); destination = lexer(&route, "\t");
gateway = lexer(&route, "\t"); gateway = lexer(&route, "\t");
...@@ -369,7 +373,7 @@ char *get_device_ip(char *device_name) ...@@ -369,7 +373,7 @@ char *get_device_ip(char *device_name)
dec_addr = strtoul(destination, NULL, IPV4_MAX); dec_addr = strtoul(destination, NULL, IPV4_MAX);
inet_ntop(AF_INET, &dec_addr, ipv4_addr, IPV4_MAX); inet_ntop(AF_INET, &dec_addr, ipv4_addr, IPV4_MAX);
printf("ipv4_addr %s\n", ipv4_addr); printf("ipv4_addr %s\n", ipv4_addr);
goto success; goto success; // delete me please
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment