From 57681664603c269c8964662a432baab839ab4ee3 Mon Sep 17 00:00:00 2001 From: Kang Daeyoun Date: Fri, 26 May 2023 18:28:18 +0900 Subject: [PATCH] Use relative filepath in watchpid if possible --- src/include/pv-internal.h | 1 + src/pv/state.c | 11 +++++++++++ src/pv/watchpid.c | 16 ++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/include/pv-internal.h b/src/include/pv-internal.h index db65eaa..eb13826 100644 --- a/src/include/pv-internal.h +++ b/src/include/pv-internal.h @@ -85,6 +85,7 @@ struct pvstate_s { * Program status * ******************/ const char *program_name; /* program name for error reporting */ + char cwd[4096]; /* current working directory for relative path */ const char *current_file; /* current file being read */ int exit_status; /* exit status to give (0=OK) */ diff --git a/src/pv/state.c b/src/pv/state.c index be41fa2..7be9dd1 100644 --- a/src/pv/state.c +++ b/src/pv/state.c @@ -7,6 +7,7 @@ #include #include #include +#include /* @@ -36,6 +37,16 @@ pvstate_t pv_state_alloc(const char *program_name) #endif /* HAVE_SPLICE */ state->display_visible = false; + //get cwd from system and set to state + if (NULL == getcwd(state->cwd, sizeof(state->cwd))) { + // ignore error, using full path + state->cwd[0] = '\0'; + } + if ('\0' == state->cwd[1]) { + // ignore when current working directory is root directory + state->cwd[0] = '\0'; + } + return state; } diff --git a/src/pv/watchpid.c b/src/pv/watchpid.c index 3fccde9..abd04dd 100644 --- a/src/pv/watchpid.c +++ b/src/pv/watchpid.c @@ -366,11 +366,19 @@ int pv_watchpid_scanfds(pvstate_t state, pvstate_t pristine, */ void pv_watchpid_setname(pvstate_t state, pvwatchfd_t info) { - int path_length, max_display_length; + int path_length, cwd_length, max_display_length; + char *file_fdpath = info->file_fdpath; memset(info->display_name, 0, sizeof(info->display_name)); path_length = strlen(info->file_fdpath); + cwd_length = strlen(state->cwd); + if (cwd_length > 0 && path_length > cwd_length) { + if (0 == strncmp(info->file_fdpath, state->cwd, cwd_length)) { + file_fdpath += cwd_length + 1; + path_length -= cwd_length + 1; + } + } max_display_length = (state->width / 2) - 6; if (max_display_length >= path_length) { @@ -380,7 +388,7 @@ void pv_watchpid_setname(pvstate_t state, pvwatchfd_t info) #else sprintf(info->display_name, #endif - "%4d:%.498s", info->watch_fd, info->file_fdpath); + "%4d:%.498s", info->watch_fd, file_fdpath); } else { int prefix_length, suffix_length; @@ -394,9 +402,9 @@ void pv_watchpid_setname(pvstate_t state, pvwatchfd_t info) sprintf(info->display_name, #endif "%4d:%.*s...%.*s", - info->watch_fd, prefix_length, info->file_fdpath, + info->watch_fd, prefix_length, file_fdpath, suffix_length, - info->file_fdpath + path_length - suffix_length); + file_fdpath + path_length - suffix_length); } debug("%s: %d: [%s]", "set name for fd", info->watch_fd,