From c1a2817a9572750b347fe6db5699e07ad4ce726d Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Thu, 22 Jul 2010 14:34:05 +0300 Subject: [PATCH] parser: Factor out duplicate code into a separate function Signed-off-by: Timo Kokkonen --- parse.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/parse.c b/parse.c index c2dc22d..1916a7c 100644 --- a/parse.c +++ b/parse.c @@ -320,44 +320,44 @@ restart: return atoi(dirent->d_name); } -static int get_next_tid(int pid, DIR **dir) +static int opendir_check(DIR **dir, const char *path) { int error; - if (*dir == NULL) { - char path[64]; - - snprintf(path, sizeof(path), "/proc/%d/task/", pid); - *dir = opendir(path); - if (!*dir) { + if (!*dir) { + *dir = opendir("/proc"); + if (!dir) { error = errno; - printf("Failed to open %s directory: %s\n", - path, + fprintf(stderr, "Failed to open /proc directory: %s\n", strerror(error)); return -1; } } - return parse_pid(dir); + return 0; } -static int get_next_pid(DIR **dir) +static int get_next_tid(int pid, DIR **dir) { - int error; + if (*dir == NULL) { + char path[64]; - if (!*dir) { - *dir = opendir("/proc"); - if (!dir) { - error = errno; - printf("Failed to open /proc directory: %s\n", - strerror(error)); + snprintf(path, sizeof(path), "/proc/%d/task/", pid); + if (opendir_check(dir, path)) return -1; - } } return parse_pid(dir); } +static int get_next_pid(DIR **dir) +{ + if (opendir_check(dir, "/proc")) + return -1; + + return parse_pid(dir); +} + static void read_pageframe_with_threads(int pid, struct pageframe *pageframe, struct process **process_list, -- 2.44.0