diff --git a/cmdline/mingw.c b/cmdline/mingw.c index 02cd0565..35de77e5 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -1792,31 +1792,11 @@ static int devscan(tommy_list* list) /* LCOV_EXCL_STOP */ } - while (1) { - char buf[256]; - char* s; - - s = fgets(buf, sizeof(buf), f); - - if (s == 0) - break; - - if (s[0] == '/') { - char* sep = strchr(s, ' '); - if (sep) { - devinfo_t* devinfo; - - /* clear everything after the first space */ - *sep = 0; - - devinfo = calloc_nofail(1, sizeof(devinfo_t)); - - pathcpy(devinfo->file, sizeof(devinfo->file), s); - - /* insert in the list */ - tommy_list_insert_tail(list, &devinfo->node, devinfo); - } - } + if (smartctl_scan(f, list) != 0) { + /* LCOV_EXCL_START */ + pclose(f); + return -1; + /* LCOV_EXCL_STOP */ } ret = pclose(f); diff --git a/cmdline/support.c b/cmdline/support.c index 10a23daa..b4fb4c3b 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -439,7 +439,6 @@ int smartctl_attribute(FILE* f, uint64_t* smart, char* serial) char* s; s = fgets(buf, sizeof(buf), f); - if (s == 0) break; @@ -489,3 +488,34 @@ int smartctl_attribute(FILE* f, uint64_t* smart, char* serial) return 0; } +int smartctl_scan(FILE* f, tommy_list* list) +{ + while (1) { + char buf[256]; + char* s; + + s = fgets(buf, sizeof(buf), f); + if (s == 0) + break; + + if (*s == '/') { + char* sep = strchr(s, ' '); + if (sep) { + devinfo_t* devinfo; + + /* clear everything after the first space */ + *sep = 0; + + devinfo = calloc_nofail(1, sizeof(devinfo_t)); + + pathcpy(devinfo->file, sizeof(devinfo->file), s); + + /* insert in the list */ + tommy_list_insert_tail(list, &devinfo->node, devinfo); + } + } + } + + return 0; +} + diff --git a/cmdline/support.h b/cmdline/support.h index f715c87f..3e5d92f7 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -126,9 +126,15 @@ void malloc_fail(size_t size); /** * Reads smartctl attributes from a stream. - * Returns -1 on error. + * Returns 0 on success. */ int smartctl_attribute(FILE* f, uint64_t* smart, char* serial); +/** + * Reads smartctl --scan from a stream. + * Returns 0 on success. + */ +int smartctl_scan(FILE* f, tommy_list* list); + #endif