diff --git a/tools/nut-scanner/nut-scan.h b/tools/nut-scanner/nut-scan.h index e638a454f4..c2df0c4701 100644 --- a/tools/nut-scanner/nut-scan.h +++ b/tools/nut-scanner/nut-scan.h @@ -155,7 +155,7 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char *start_ip, const char nutscan_device_t * nutscan_scan_nut(const char * startIP, const char * stopIP, const char * port, useconds_t usec_timeout); -nutscan_device_t * nutscan_scan_nut_simulation(); +nutscan_device_t * nutscan_scan_nut_simulation(void); nutscan_device_t * nutscan_scan_avahi(useconds_t usec_timeout); diff --git a/tools/nut-scanner/scan_nut_simulation.c b/tools/nut-scanner/scan_nut_simulation.c index 0d325f4ad5..402293ad47 100644 --- a/tools/nut-scanner/scan_nut_simulation.c +++ b/tools/nut-scanner/scan_nut_simulation.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Arnaud Quette + * Copyright (C) 2023-2024 Arnaud Quette * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,9 +25,6 @@ #include "nut-scan.h" #include "nut_stdint.h" #include -#if !HAVE_DECL_REALPATH -# include -#endif #define SCAN_NUT_SIMULATION_DRIVERNAME "dummy-ups" @@ -38,30 +35,11 @@ static nutscan_device_t * dev_ret = NULL; static pthread_mutex_t dev_mutex; #endif -/* return 1 when filter is ok (.dev or .seq) */ -static int filter_ext(const struct dirent *dir) -{ - if(!dir) - return 0; - - if(dir->d_type == DT_REG) { /* only deal with regular file */ - const char *ext = strrchr(dir->d_name,'.'); - if((!ext) || (ext == dir->d_name)) - return 0; - else { - if ((strcmp(ext, ".dev") == 0) || (strcmp(ext, ".seq") == 0)) { - return 1; - } - } - } - return 0; -} - -nutscan_device_t * nutscan_scan_nut_simulation() +nutscan_device_t * nutscan_scan_nut_simulation(void) { + DIR *dp; + struct dirent *dirp; nutscan_device_t * dev = NULL; - struct dirent **namelist; - int n; #if HAVE_PTHREAD pthread_mutex_init(&dev_mutex, NULL); @@ -69,19 +47,28 @@ nutscan_device_t * nutscan_scan_nut_simulation() upsdebugx(1,"Scanning: %s", CONFPATH); - n = scandir(CONFPATH, &namelist, filter_ext, alphasort); - if (n < 0) { - fatal_with_errno(EXIT_FAILURE, "Failed to scandir"); + if ((dp = opendir(CONFPATH)) == NULL) { + fatal_with_errno(EXIT_FAILURE, "Failed to open %s", CONFPATH); return NULL; } - else { - while (n--) { - upsdebugx(1,"Found simulation file: %s", namelist[n]->d_name); + + while ((dirp = readdir(dp)) != NULL) + { + const char *ext; + + upsdebugx(5, "Comparing file %s with simulation file extensions", dirp->d_name); + ext = strrchr(dirp->d_name, '.'); + if((!ext) || (ext == dirp->d_name)) + continue; + + /* Filter on '.dev' and '.seq' extensions' */ + if ((strcmp(ext, ".dev") == 0) || (strcmp(ext, ".seq") == 0)) { + upsdebugx(1,"Found simulation file: %s", dirp->d_name); dev = nutscan_new_device(); dev->type = TYPE_NUT_SIMULATION; dev->driver = strdup(SCAN_NUT_SIMULATION_DRIVERNAME); - dev->port = strdup(namelist[n]->d_name); + dev->port = strdup(dirp->d_name); #ifdef HAVE_PTHREAD pthread_mutex_lock(&dev_mutex); @@ -90,10 +77,9 @@ nutscan_device_t * nutscan_scan_nut_simulation() #ifdef HAVE_PTHREAD pthread_mutex_unlock(&dev_mutex); #endif - free(namelist[n]); } - free(namelist); } + closedir(dp); #if HAVE_PTHREAD pthread_mutex_destroy(&dev_mutex);