Skip to content
Snippets Groups Projects
Commit 5c4d0247 authored by Jakob Olsson's avatar Jakob Olsson
Browse files

some refactoring regarding getting device name

parent 3af8f3e9
No related branches found
No related tags found
No related merge requests found
...@@ -219,7 +219,7 @@ fail: ...@@ -219,7 +219,7 @@ fail:
char *get_device_name(char *dir_name) char *get_device_name(char *dir_name)
{ {
char *path, *name, tmp_path[PATH_MAX]; char path[PATH_MAX], *name;
struct directory *dr, *sub_dr; struct directory *dr, *sub_dr;
struct dirent *de; struct dirent *de;
struct stat st; struct stat st;
...@@ -232,7 +232,6 @@ char *get_device_name(char *dir_name) ...@@ -232,7 +232,6 @@ char *get_device_name(char *dir_name)
goto fail; goto fail;
} }
dr->path = strdup(dir_name); dr->path = strdup(dir_name);
//snprintf(dr->path, PATH_MAX, "%s", dir_name); //remain allocated?
enqueue(dr, &stack); enqueue(dr, &stack);
while (!list_empty(&stack)) { while (!list_empty(&stack)) {
...@@ -258,24 +257,18 @@ char *get_device_name(char *dir_name) ...@@ -258,24 +257,18 @@ char *get_device_name(char *dir_name)
continue; continue;
if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) {
path_len = strlen(dr->path) + strlen(de->d_name)+2;
path = calloc(path_len, 1);
if (!path) {
perror("calloc");
goto fail_calloc;
}
snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name); snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name);
if (!search(path, &visited)) { if (!search(path, &visited)) {
sub_dr = malloc(sizeof(struct directory)); sub_dr = malloc(sizeof(struct directory));
if (!sub_dr) { if (!sub_dr) {
perror("malloc"); perror("malloc");
goto fail_dir; goto fail_malloc;
} }
sub_dr->path = path;
sub_dr->path = strdup(path);
enqueue(sub_dr, &stack); enqueue(sub_dr, &stack);
} else }
free(path);
continue; continue;
} }
...@@ -288,44 +281,30 @@ char *get_device_name(char *dir_name) ...@@ -288,44 +281,30 @@ char *get_device_name(char *dir_name)
} }
closedir(dir); closedir(dir);
} }
fail_dir:
//free(path); fail_malloc:
fail_calloc:
clear_list(&visited); clear_list(&visited);
fail_opendir: fail_opendir:
//clear_list(&stack); clear_list(&stack);
fail: fail:
return NULL; return NULL;
success: success:
printf("name %s\n", name);
clear_list(&visited); clear_list(&visited);
printf("name %s\n", name);
clear_list(&stack); clear_list(&stack);
printf("name %s\n", name);
closedir(dir); closedir(dir);
return name; return name;
} }
char *get_interface_name(char *usb_path, char *sub_dir_name)
{
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s/%s/", usb_path, sub_dir_name);
return get_device_name(path);
}
int get_devices(void) int get_devices(void)
{ {
char usb_path[PATH_MAX] = {0}, product_path[PATH_MAX] = {0}, *name; char usb_path[PATH_MAX], product_path[PATH_MAX], path[PATH_MAX], *name;
struct dirent *de; struct dirent *de;
DIR *dir; DIR *dir;
struct stat st; struct stat st;
struct device *node; struct device *node;
int rv; int rv;
printf("hello\n"); strncpy(usb_path, "/sys/bus/usb/devices/", PATH_MAX);
strncpy(usb_path, "/sys/bus/usb/devices", PATH_MAX);
if ((dir = opendir(usb_path)) != NULL) { if ((dir = opendir(usb_path)) != NULL) {
while ((de = readdir(dir)) != NULL) { while ((de = readdir(dir)) != NULL) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
...@@ -333,16 +312,19 @@ int get_devices(void) ...@@ -333,16 +312,19 @@ int get_devices(void)
if (strstr(de->d_name, ":") || strstr(de->d_name, "usb")) if (strstr(de->d_name, ":") || strstr(de->d_name, "usb"))
continue; continue;
snprintf(product_path, PATH_MAX, "%s/%s/product", usb_path, de->d_name); snprintf(product_path, PATH_MAX, "%s%s/product", usb_path, de->d_name);
printf("product_path %s\n", product_path); printf("product_path %s\n", product_path);
memset(&st, 0, sizeof(struct stat)); memset(&st, 0, sizeof(struct stat));
rv = stat(product_path, &st); rv = stat(product_path, &st);
if (rv < 0){ if (rv < 0){
perror("stat"); perror("stat");
continue; continue;
} }
name = get_interface_name(usb_path, de->d_name);
printf("returned name %s!\n", name); snprintf(path, PATH_MAX, "%s%s/", usb_path, de->d_name);
name = get_device_name(path);
if (!name) if (!name)
continue; continue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment