From 5adbe6cff3e5c00d35bbd0ae4e610b8b1f71025d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 20 Sep 2021 13:16:06 +0200 Subject: [PATCH] lsfd: cleanup fdinfo handling Signed-off-by: Karel Zak --- misc-utils/lsfd-file.c | 10 ++++++---- misc-utils/lsfd.c | 13 +++++++------ misc-utils/lsfd.h | 6 +++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 8165b3ada2..dab7edca11 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -28,6 +28,7 @@ #include "nls.h" #include "buffer.h" #include "idcache.h" +#include "strutils.h" #include "libsmartcols.h" @@ -305,7 +306,7 @@ static bool file_fill_column(struct proc *proc, xasprintf(&str, "---"); break; case COL_POS: - xasprintf(&str, "%llu", + xasprintf(&str, "%" PRIu64, (does_file_has_fdinfo_alike(file))? file->pos: 0); break; case COL_FLAGS: { @@ -343,13 +344,14 @@ static bool file_fill_column(struct proc *proc, static int file_handle_fdinfo(struct file *file, const char *key, const char* value) { if (strcmp(key, "pos") == 0) { - file->pos = strtoull(value, NULL, 10); + ul_strtou64(value, &file->pos, 10); return 1; } else if (strcmp(key, "flags") == 0) { - file->sys_flags = (int)strtol(value, NULL, 8); + ul_strtou32(value, &file->sys_flags, 8); + return 1; } else if (strcmp(key, "mnt_id") == 0) { - file->mnt_id = (int)strtol(value, NULL, 10); + ul_strtou32(value, &file->mnt_id, 10); return 1; } return 0; diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 7de7ee73d3..9e9fc0940a 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -316,17 +316,18 @@ static void free_proc(struct proc *proc) static void read_fdinfo(struct file *file, FILE *fdinfo) { - const struct file_class *class; char buf[1024]; - char *val; while (fgets(buf, sizeof(buf), fdinfo)) { - val = strchr(buf, ':'); + const struct file_class *class; + char *val = strchr(buf, ':'); + if (!val) continue; - *val++ = '\0'; - while (*val == '\t' || *val == ' ') - val++; + *val++ = '\0'; /* terminate key */ + + val = (char *) skip_space(val); + rtrim_whitespace((unsigned char *) val); class = file->class; while (class) { diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index f03f12d5b2..31783d71f7 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -111,13 +111,13 @@ struct file { struct stat stat; mode_t mode; struct proc *proc; - unsigned long long pos; + uint64_t pos; unsigned long map_start; unsigned long map_end; - int sys_flags; - int mnt_id; + unsigned int sys_flags; + unsigned int mnt_id; }; #define is_association(_f, a) ((_f)->association < 0 && (_f)->association == -ASSOC_ ## a) -- 2.47.3