/* * mpl-core.c * * by Alexander Zhirov (alexander@zhirov.kz) * Telegram @alexanderzhirov */ #include "mpl-lib.h" #include "mpl-core.h" #include #include #include int device_path(const char *dirname, int num) { DIR *dir; struct dirent *ent; regex_t regex; const char *pattern = "^ttyUSB[0-9]*$"; int ret = regcomp(®ex, pattern, REG_EXTENDED); if (ret != 0) { printf("Failed to compile regular expression\n"); return 1; } dir = opendir(dirname); if (dir == NULL) { printf("Failed to open directory\n"); regfree(®ex); return 1; } while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } ret = regexec(®ex, ent->d_name, 0, NULL, 0); if (ret == 0) { printf("Found a file matching the pattern: %s\n", ent->d_name); char *path = (char *)calloc(strlen(MPL_DEV_PATH) + strlen(ent->d_name) + 1, sizeof(char)); strcpy(path, MPL_DEV_PATH); strcat(path, ent->d_name); int fd = open(path, O_RDWR); if (fd < 0) { printf("Не удалось открыть USB устройство.\n"); free(path); continue; } char *tty = ttyname(fd); close(fd); if (tty != NULL) { printf("Найдено устройство tty: %s\n", tty); // if (!num) // print_imei(tty); } else { printf("Устройство tty не найдено\n"); } } else if (ret != REG_NOMATCH) { printf("Failed to apply regular expression\n"); } } regfree(®ex); closedir(dir); return 0; }