summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/path.h5
-rw-r--r--lib/path.c6
-rw-r--r--sys-utils/lscpu.c6
-rw-r--r--sys-utils/lsmem.c19
4 files changed, 20 insertions, 16 deletions
diff --git a/include/path.h b/include/path.h
index 11c336759..ae36d7f4c 100644
--- a/include/path.h
+++ b/include/path.h
@@ -4,8 +4,11 @@
#include <stdio.h>
#include <stdint.h>
-extern char *path_strdup(const char *path, ...)
+/* Returns a pointer to a static buffer which may be destroyed by any later
+path_* function call. NULL means error and errno will be set. */
+extern const char *path_get(const char *path, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
+
extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));
extern void path_read_str(char *result, size_t len, const char *path, ...)
diff --git a/lib/path.c b/lib/path.c
index 3c587e433..79c1e7a68 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -50,8 +50,8 @@ path_vcreate(const char *path, va_list ap)
return pathbuf;
}
-char *
-path_strdup(const char *path, ...)
+const char *
+path_get(const char *path, ...)
{
const char *p;
va_list ap;
@@ -60,7 +60,7 @@ path_strdup(const char *path, ...)
p = path_vcreate(path, ap);
va_end(ap);
- return p ? strdup(p) : NULL;
+ return p;
}
static FILE *
diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index f6e47277e..83f3a7d27 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -1430,12 +1430,12 @@ read_nodes(struct lscpu_desc *desc)
int i = 0;
DIR *dir;
struct dirent *d;
- char *path;
+ const char *path;
/* number of NUMA node */
- path = path_strdup(_PATH_SYS_NODE);
+ if (!(path = path_get(_PATH_SYS_NODE)))
+ return;
dir = opendir(path);
- free(path);
while (dir && (d = readdir(dir))) {
if (is_node_dirent(d))
diff --git a/sys-utils/lsmem.c b/sys-utils/lsmem.c
index e1ee5a5ce..4db678966 100644
--- a/sys-utils/lsmem.c
+++ b/sys-utils/lsmem.c
@@ -248,15 +248,14 @@ static void print_summary(struct lsmem *lsmem)
static int memory_block_get_node(char *name)
{
struct dirent *de;
- char *path;
+ const char *path;
DIR *dir;
int node;
- path = path_strdup(_PATH_SYS_MEMORY"/%s", name);
- dir = opendir(path);
- free(path);
- if (!dir)
- err(EXIT_FAILURE, _("Failed to open %s"), path);
+ path = path_get(_PATH_SYS_MEMORY"/%s", name);
+ if (!path || !(dir= opendir(path)))
+ err(EXIT_FAILURE, _("Failed to open %s"), path ? path : name);
+
node = -1;
while ((de = readdir(dir)) != NULL) {
if (strncmp("node", de->d_name, 4))
@@ -348,14 +347,16 @@ static int memory_block_filter(const struct dirent *de)
static void read_basic_info(struct lsmem *lsmem)
{
- char *dir;
+ const char *dir;
if (!path_exist(_PATH_SYS_MEMORY_BLOCK_SIZE))
errx(EXIT_FAILURE, _("This system does not support memory blocks"));
- dir = path_strdup(_PATH_SYS_MEMORY);
+ dir = path_get(_PATH_SYS_MEMORY);
+ if (!dir)
+ err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
+
lsmem->ndirs = scandir(dir, &lsmem->dirs, memory_block_filter, versionsort);
- free(dir);
if (lsmem->ndirs <= 0)
err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);