Process: add convenience helper functions to update merged command line related data

This commit is contained in:
Christian Göttsche 2021-05-18 22:29:25 +02:00 committed by BenBE
parent 7c654559c9
commit 05fb681d5c
2 changed files with 75 additions and 0 deletions

View File

@ -1120,3 +1120,74 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField
return SPACESHIP_NUMBER(p1->pid, p2->pid); 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;
}

View File

@ -373,6 +373,10 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField
// Avoid direct calls, use Process_getCommand instead // Avoid direct calls, use Process_getCommand instead
const char *Process_getCommandStr(const Process *this); 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 /* This function constructs the string that is displayed by
* Process_writeCommand and also returned by Process_getCommandStr */ * Process_writeCommand and also returned by Process_getCommandStr */
void Process_makeCommandStr(Process *this); void Process_makeCommandStr(Process *this);