The regulator_init function does no longer use the global defined function.
Signed-off-by: Daniel Lezcano daniel.lezcano@free.fr --- powerdebug.c | 5 ++++- regulator.c | 18 ++++++------------ regulator.h | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index 81875eb..5f23e0c 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -353,8 +353,11 @@ int main(int argc, char **argv) return 1; }
- if (regulator_init()) + regulators_info = regulator_init(&numregulators); + if (!regulators_info) { + printf("not enough memory to allocate regulators info\n"); return 1; + }
if (mainloop(options)) return 1; diff --git a/regulator.c b/regulator.c index 91c3903..ddbeea3 100644 --- a/regulator.c +++ b/regulator.c @@ -17,15 +17,17 @@
#define SYSFS_REGULATOR "/sys/class/regulator"
-int regulator_init(void) +struct regulator_info *regulator_init(int *nr_regulators) { DIR *regdir; struct dirent *item;
+ *nr_regulators = 0; + regdir = opendir(SYSFS_REGULATOR); if (!regdir) { fprintf(stderr, "failed to open '%s': %m\n", SYSFS_REGULATOR); - return -1; + return NULL; }
while ((item = readdir(regdir))) { @@ -36,20 +38,12 @@ int regulator_init(void) if (!strcmp(item->d_name, "..")) continue;
- numregulators++; + (*nr_regulators)++; }
closedir(regdir);
- regulators_info = (struct regulator_info *)malloc(numregulators* - sizeof(struct regulator_info)); - if (!regulators_info) { - fprintf(stderr, "init_regulator_ds: Not enough memory to " - "read information for %d regulators!\n", numregulators); - return(1); - } - - return(0); + return malloc(*nr_regulators * sizeof(struct regulator_info)); }
static void print_string_val(char *name, char *val) diff --git a/regulator.h b/regulator.h index d46114a..a753299 100644 --- a/regulator.h +++ b/regulator.h @@ -40,6 +40,6 @@ struct regulator_info { int num_users; } *regulators_info;
-extern int regulator_init(void); +extern struct regulator_info *regulator_init(int *nr_regulators); extern int regulator_read_info(void); extern void regulator_print_info(struct regulator_info *reg_info, int verbose);