diff --git a/CHANGES b/CHANGES index 50c5006b..a2835946 100644 --- a/CHANGES +++ b/CHANGES @@ -21,6 +21,7 @@ SVN HEAD Delete sensors_do_all_sets(), unused Pass structures by reference Drop support for dummy type + New function sensors_snprintf_chip_name() Makefile: Drop the package and version targets Man page sensors.conf.5: Update the chip statement section Programs doc/*: Delete, obsolete diff --git a/lib/access.c b/lib/access.c index 5bd6efa6..d90ef428 100644 --- a/lib/access.c +++ b/lib/access.c @@ -133,7 +133,7 @@ sensors_lookup_feature_name(const sensors_chip_name *chip, const char *feature) /* Check whether the chip name is an 'absolute' name, which can only match one chip, or whether it has wildcards. Returns 0 if it is absolute, 1 if there are wildcards. */ -static int sensors_chip_name_has_wildcards(const sensors_chip_name *chip) +int sensors_chip_name_has_wildcards(const sensors_chip_name *chip) { if ((chip->prefix == SENSORS_CHIP_NAME_PREFIX_ANY) || (chip->bus == SENSORS_CHIP_NAME_BUS_ANY) || diff --git a/lib/access.h b/lib/access.h index 0298ae54..b5639246 100644 --- a/lib/access.h +++ b/lib/access.h @@ -29,6 +29,11 @@ extern const sensors_chip_feature *sensors_lookup_feature_nr(const sensors_chip_name *chip, int feature); +/* Check whether the chip name is an 'absolute' name, which can only match + one chip, or whether it has wildcards. Returns 0 if it is absolute, 1 + if there are wildcards. */ +int sensors_chip_name_has_wildcards(const sensors_chip_name *chip); + sensors_feature_type sensors_feature_get_type(const char *name, int *nr); #endif /* def LIB_SENSORS_ACCESS_H */ diff --git a/lib/data.c b/lib/data.c index 8d43e605..1a3c2dff 100644 --- a/lib/data.c +++ b/lib/data.c @@ -20,6 +20,7 @@ #include #include +#include "access.h" #include "error.h" #include "data.h" #include "sensors.h" @@ -154,6 +155,25 @@ ERROR: return -SENSORS_ERR_CHIP_NAME; } +int sensors_snprintf_chip_name(char *str, size_t size, + const sensors_chip_name *chip) +{ + if (sensors_chip_name_has_wildcards(chip)) + return -SENSORS_ERR_WILDCARDS; + + switch (chip->bus) { + case SENSORS_CHIP_NAME_BUS_ISA: + return snprintf(str, size, "%s-isa-%04x", chip->prefix, + chip->addr); + case SENSORS_CHIP_NAME_BUS_PCI: + return snprintf(str, size, "%s-pci-%04x", chip->prefix, + chip->addr); + default: + return snprintf(str, size, "%s-i2c-%d-%02x", chip->prefix, + chip->bus, chip->addr); + } +} + int sensors_parse_i2cbus_name(const char *name, int *res) { char *endptr; diff --git a/lib/sensors.h b/lib/sensors.h index b2a31838..2a691892 100644 --- a/lib/sensors.h +++ b/lib/sensors.h @@ -60,6 +60,12 @@ extern void sensors_cleanup(void); extern int sensors_parse_chip_name(const char *orig_name, sensors_chip_name *res); +/* Print a chip name from its internal representation. Note that chip should + not contain wildcard values! Return the number of characters printed on + success (same as snprintf), <0 on error. */ +int sensors_snprintf_chip_name(char *str, size_t size, + const sensors_chip_name *chip); + /* Compare two chips name descriptions, to see whether they could match. Return 0 if it does not match, return 1 if it does match. */ extern int sensors_match_chip(const sensors_chip_name *chip1, diff --git a/prog/sensord/sense.c b/prog/sensord/sense.c index 701dd03b..bf7f5695 100644 --- a/prog/sensord/sense.c +++ b/prog/sensord/sense.c @@ -74,12 +74,8 @@ static const char * chipName (const sensors_chip_name *chip) { static char buffer[256]; - if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA) - sprintf (buffer, "%s-isa-%04x", chip->prefix, chip->addr); - else if (chip->bus == SENSORS_CHIP_NAME_BUS_PCI) - sprintf (buffer, "%s-pci-%04x", chip->prefix, chip->addr); - else - sprintf (buffer, "%s-i2c-%d-%02x", chip->prefix, chip->bus, chip->addr); + if (sensors_snprintf_chip_name(buffer, 256, chip) < 0) + return NULL; return buffer; } diff --git a/prog/sensors/main.c b/prog/sensors/main.c index 929db027..6da5e391 100644 --- a/prog/sensors/main.c +++ b/prog/sensors/main.c @@ -298,13 +298,8 @@ const char *sprintf_chip_name(const sensors_chip_name *name) #define BUF_SIZE 200 static char buf[BUF_SIZE]; - if (name->bus == SENSORS_CHIP_NAME_BUS_ISA) - snprintf(buf, BUF_SIZE, "%s-isa-%04x", name->prefix, name->addr); - else if (name->bus == SENSORS_CHIP_NAME_BUS_PCI) - snprintf(buf, BUF_SIZE, "%s-pci-%04x", name->prefix, name->addr); - else - snprintf(buf, BUF_SIZE, "%s-i2c-%d-%02x", name->prefix, name->bus, - name->addr); + if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0) + return NULL; return buf; }