From ef173bde3fda2db510e0b3b72c9654c9dcffa9b7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 27 May 2010 17:24:28 +0200 Subject: [PATCH] lscpu: cleanup path_scanstr() Signed-off-by: Karel Zak --- sys-utils/lscpu.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c index 77a3e5c2..e104dfc9 100644 --- a/sys-utils/lscpu.c +++ b/sys-utils/lscpu.c @@ -122,8 +122,10 @@ struct cpu_desc { char pathbuf[PATH_MAX] = "/"; -static void path_scanstr(char *result, const char *path, ...) - __attribute__ ((__format__ (__printf__, 2, 3))); +static void path_getstr(char *result, size_t len, const char *path, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); +static int path_getnum(const char *path, ...) + __attribute__ ((__format__ (__printf__, 1, 2))); static int path_exist(const char *path, ...) __attribute__ ((__format__ (__printf__, 1, 2))); static int path_sibling(const char *path, ...) @@ -146,22 +148,43 @@ path_vfopen(const char *mode, const char *path, va_list ap) } static void -path_scanstr(char *result, const char *path, ...) +path_getstr(char *result, size_t len, const char *path, ...) +{ + FILE *fd; + va_list ap; + + va_start(ap, path); + fd = path_vfopen("r", path, ap); + va_end(ap); + + if (!fgets(result, len, fd)) + err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); + fclose(fd); + + len = strlen(result); + if (result[len - 1] == '\n') + result[len - 1] = '\0'; +} + +static int +path_getnum(const char *path, ...) { FILE *fd; va_list ap; + int result; va_start(ap, path); fd = path_vfopen("r", path, ap); va_end(ap); - if (fscanf(fd, "%s", result) != 1) { + if (fscanf(fd, "%d", &result) != 1) { if (ferror(fd)) err(EXIT_FAILURE, _("error: %s"), pathbuf); else errx(EXIT_FAILURE, _("error parse: %s"), pathbuf); } fclose(fd); + return result; } static int @@ -461,7 +484,8 @@ read_cache(struct cpu_desc *cpu) continue; /* cache type */ - path_scanstr(buf, _PATH_SYS_CPU0 "/cache/%s/type", dir->d_name); + path_getstr(buf, sizeof(buf), + _PATH_SYS_CPU0 "/cache/%s/type", dir->d_name); if (!strcmp(buf, "Data")) type = 'd'; else if (!strcmp(buf, "Instruction")) @@ -470,8 +494,8 @@ read_cache(struct cpu_desc *cpu) type = 0; /* cache level */ - path_scanstr(buf, _PATH_SYS_CPU0 "/cache/%s/level", dir->d_name); - level = atoi(buf); + level = path_getnum(_PATH_SYS_CPU0 "/cache/%s/level", + dir->d_name); if (type) snprintf(buf, sizeof(buf), "L%d%c", level, type); @@ -481,7 +505,8 @@ read_cache(struct cpu_desc *cpu) cpu->cache[cpu->ct_cache].caname = xstrdup(buf); /* cache size */ - path_scanstr(buf, _PATH_SYS_CPU0 "/cache/%s/size", dir->d_name); + path_getstr(buf, sizeof(buf), + _PATH_SYS_CPU0 "/cache/%s/size", dir->d_name); cpu->cache[cpu->ct_cache].casize = xstrdup(buf); /* information about how CPUs share different caches */ -- 2.39.5