summaryrefslogtreecommitdiff
path: root/sys-utils
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2010-05-27 17:24:28 +0200
committerKarel Zak <kzak@redhat.com>2010-06-01 11:10:01 +0200
commitef173bde3fda2db510e0b3b72c9654c9dcffa9b7 (patch)
tree176f7335220003d8ce16082208baadc32a288880 /sys-utils
parentde878776623b120fc1e96568f4cd69c349ec2677 (diff)
downloadutil-linux-old-ef173bde3fda2db510e0b3b72c9654c9dcffa9b7.tar.gz
lscpu: cleanup path_scanstr()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils')
-rw-r--r--sys-utils/lscpu.c41
1 files 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 */