diff options
author | Christian Göttsche <cgzones@googlemail.com> | 2021-05-18 22:29:25 +0200 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2021-05-23 09:22:21 +0200 |
commit | 05fb681d5cc9535f12879d4b307808b5307861c1 (patch) | |
tree | cde2cc10fb946e93a84d5f3ef479b2f1ed45e3c4 /Process.c | |
parent | 7c654559c9615489da61803a626af7d3e9a05fa7 (diff) |
Process: add convenience helper functions to update merged command line related data
Diffstat (limited to 'Process.c')
-rw-r--r-- | Process.c | 71 |
1 files changed, 71 insertions, 0 deletions
@@ -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; +} |