From 05fb681d5cc9535f12879d4b307808b5307861c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 18 May 2021 22:29:25 +0200 Subject: [PATCH] Process: add convenience helper functions to update merged command line related data --- Process.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Process.h | 4 ++++ 2 files changed, 75 insertions(+) diff --git a/Process.c b/Process.c index c2a2ed17..5236c8b1 100644 --- a/Process.c +++ b/Process.c @@ -1120,3 +1120,74 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField return SPACESHIP_NUMBER(p1->pid, p2->pid); } } + +void Process_updateComm(Process* this, const char* comm) { + if (!this->procComm && !comm) + return; + + if (this->procComm && comm && String_eq(this->procComm, comm)) + return; + + free(this->procComm); + this->procComm = comm ? xStrdup(comm) : NULL; + this->mergedCommand.commChanged = true; +} + +static int skipPotentialPath(const char* cmdline, int end) { + if (cmdline[0] != '/') + return 0; + + int slash = 0; + for (int i = 1; i < end; i++) { + if (cmdline[i] == '/' && cmdline[i+1] != '\0') { + slash = i + 1; + continue; + } + + if (cmdline[i] == ' ' && cmdline[i-1] != '\\') + return slash; + + if (cmdline[i] == ':' && cmdline[i+1] == ' ') + return slash; + } + + return slash; +} + +void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd) { + assert(basenameStart >= 0); + assert((cmdline && basenameStart < (int)strlen(cmdline)) || (!cmdline && basenameStart == 0)); + assert(basenameEnd >= 0); + assert((cmdline && basenameEnd <= (int)strlen(cmdline)) || (!cmdline && basenameEnd == 0)); + + if (!this->cmdline && !cmdline) + return; + + if (this->cmdline && cmdline && String_eq(this->cmdline, cmdline)) + return; + + free(this->cmdline); + this->cmdline = cmdline ? xStrdup(cmdline) : NULL; + this->cmdlineBasenameStart = (basenameStart || !cmdline) ? basenameStart : skipPotentialPath(cmdline, basenameEnd); + this->cmdlineBasenameEnd = basenameEnd; + this->mergedCommand.cmdlineChanged = true; +} + +void Process_updateExe(Process* this, const char* exe) { + if (!this->procExe && !exe) + return; + + if (this->procExe && exe && String_eq(this->procExe, exe)) + return; + + free(this->procExe); + if (exe) { + this->procExe = xStrdup(exe); + const char* lastSlash = strrchr(exe, '/'); + this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (lastSlash - exe + 1) : 0; + } else { + this->procExe = NULL; + this->procExeBasenameOffset = 0; + } + this->mergedCommand.exeChanged = true; +} diff --git a/Process.h b/Process.h index 0b2f198d..dfd28135 100644 --- a/Process.h +++ b/Process.h @@ -373,6 +373,10 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField // Avoid direct calls, use Process_getCommand instead const char *Process_getCommandStr(const Process *this); +void Process_updateComm(Process* this, const char* comm); +void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd); +void Process_updateExe(Process* this, const char* exe); + /* This function constructs the string that is displayed by * Process_writeCommand and also returned by Process_getCommandStr */ void Process_makeCommandStr(Process *this);