Previously, the return value of sscanf() was treated as an indication of
the digits it would have read. Yet, sscanf() only returns the amount of
valid matches.
Therefore, introduce a function to calculate the decimal digits of the
read number and use this one to commence a colon search, as originally
intended.
Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
+int calc_digits(int num)
+{
+ int count = 0;
+
+ while (num != 0) {
+ num /= 10;
+ count++;
+ }
+
+ return count;
+}
+
/**
* find_type_by_name() - function to match top level types by name
* @name: top level type instance name
/**
* find_type_by_name() - function to match top level types by name
* @name: top level type instance name
int find_type_by_name(const char *name, const char *type)
{
const struct dirent *ent;
int find_type_by_name(const char *name, const char *type)
{
const struct dirent *ent;
+ int number, numstrlen, ret;
strcmp(ent->d_name, "..") != 0 &&
strlen(ent->d_name) > strlen(type) &&
strncmp(ent->d_name, type, strlen(type)) == 0) {
strcmp(ent->d_name, "..") != 0 &&
strlen(ent->d_name) > strlen(type) &&
strncmp(ent->d_name, type, strlen(type)) == 0) {
- numstrlen = sscanf(ent->d_name + strlen(type),
- "%d",
- &number);
+ errno = 0;
+ ret = sscanf(ent->d_name + strlen(type), "%d", &number);
+ if (ret < 0) {
+ ret = -errno;
+ printf("failed to read element number\n");
+ goto error_close_dir;
+ } else if (ret != 1) {
+ ret = -EIO;
+ printf("failed to match element number\n");
+ goto error_close_dir;
+ }
+
+ numstrlen = calc_digits(number);
/* verify the next character is not a colon */
if (strncmp(ent->d_name + strlen(type) + numstrlen,
":",
/* verify the next character is not a colon */
if (strncmp(ent->d_name + strlen(type) + numstrlen,
":",
}
closedir(dp);
return -ENODEV;
}
closedir(dp);
return -ENODEV;
+
+error_close_dir:
+ if (closedir(dp) == -1)
+ perror("find_type_by_name(): Failed to close directory");
+ return ret;
}
int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
}
int _write_sysfs_int(char *filename, char *basedir, int val, int verify)