From 893be621d97b7984a46ff142afa12c1b404731ae Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 23 Aug 2008 14:35:24 +0200 Subject: [PATCH] src/common.[ch]: Pass user data to `walk_directory'. `walk_directory' in turn passes the directory name and the user data back to the callback functions. --- src/battery.c | 6 ++++-- src/common.c | 24 ++++++++++++++++++------ src/common.h | 6 ++++-- src/thermal.c | 12 ++++++++---- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/battery.c b/src/battery.c index 9f1bee65..416f3397 100644 --- a/src/battery.c +++ b/src/battery.c @@ -315,7 +315,8 @@ static void get_via_generic_iokit (double *ret_charge, #endif /* HAVE_IOKIT_IOKITLIB_H */ #if KERNEL_LINUX -static int battery_read_acpi (const char *name) +static int battery_read_acpi (const char *dir, const char *name, + void *user_data) { double current = INVALID_VALUE; double voltage = INVALID_VALUE; @@ -506,7 +507,8 @@ static int battery_read (void) battery_submit ("0", "voltage", voltage); } - walk_directory (battery_acpi_dir, battery_read_acpi); + walk_directory (battery_acpi_dir, battery_read_acpi, + /* user_data = */ NULL); #endif /* KERNEL_LINUX */ diff --git a/src/common.c b/src/common.c index fd7c1993..61a759b1 100644 --- a/src/common.c +++ b/src/common.c @@ -865,32 +865,44 @@ int notification_init (notification_t *n, int severity, const char *message, return (0); } /* int notification_init */ -int walk_directory (const char *dir, dirwalk_callback_f callback) +int walk_directory (const char *dir, dirwalk_callback_f callback, + void *user_data) { struct dirent *ent; DIR *dh; - int ok = 0; + int success; + int failure; + + success = 0; + failure = 0; if ((dh = opendir (dir)) == NULL) { char errbuf[1024]; - ERROR ("Cannot open '%s': %s", dir, + ERROR ("walk_directory: Cannot open '%s': %s", dir, sstrerror (errno, errbuf, sizeof (errbuf))); return -1; } while ((ent = readdir (dh)) != NULL) { + int status; + if (ent->d_name[0] == '.') continue; - if (!callback(ent->d_name)) - ++ok; + status = (*callback) (dir, ent->d_name, user_data); + if (status != 0) + failure++; + else + success++; } closedir (dh); - return ok ? 0 : -1; + if ((success == 0) && (failure > 0)) + return (-1); + return (0); } int read_file_contents (const char *filename, char *buf, int bufsize) diff --git a/src/common.h b/src/common.h index 119cee6d..f463b77e 100644 --- a/src/common.h +++ b/src/common.h @@ -203,8 +203,10 @@ int notification_init (notification_t *n, int severity, const char *message, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \ (ds)->type, (vl)->type_instance) -typedef int (*dirwalk_callback_f)(const char *filename); -int walk_directory (const char *dir, dirwalk_callback_f callback); +typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename, + void *user_data); +int walk_directory (const char *dir, dirwalk_callback_f callback, + void *user_data); int read_file_contents (const char *filename, char *buf, int bufsize); #endif /* COMMON_H */ diff --git a/src/thermal.c b/src/thermal.c index 93781328..b6136480 100644 --- a/src/thermal.c +++ b/src/thermal.c @@ -61,7 +61,8 @@ static void thermal_submit (const char *plugin_instance, enum dev_type dt, plugin_dispatch_values (&vl); } -static int thermal_sysfs_device_read (const char *name) +static int thermal_sysfs_device_read (const char *dir, const char *name, + void *user_data) { char filename[256]; char data[1024]; @@ -112,7 +113,8 @@ static int thermal_sysfs_device_read (const char *name) return ok ? 0 : -1; } -static int thermal_procfs_device_read (const char *name) +static int thermal_procfs_device_read (const char *dir, const char *name, + void *user_data) { const char str_temp[] = "temperature:"; char filename[256]; @@ -215,12 +217,14 @@ static int thermal_config (const char *key, const char *value) static int thermal_sysfs_read (void) { - return walk_directory (dirname_sysfs, thermal_sysfs_device_read); + return walk_directory (dirname_sysfs, thermal_sysfs_device_read, + /* user_data = */ NULL); } static int thermal_procfs_read (void) { - return walk_directory (dirname_procfs, thermal_procfs_device_read); + return walk_directory (dirname_procfs, thermal_procfs_device_read, + /* user_data = */ NULL); } static int thermal_init (void) -- 2.11.0