diff --git a/Process.c b/Process.c index 0aade6df..4999bfcb 100644 --- a/Process.c +++ b/Process.c @@ -26,6 +26,7 @@ in the source distribution for its full text. #include #include #include +#include #ifdef __ANDROID__ #define SYS_ioprio_get __NR_ioprio_get @@ -72,6 +73,11 @@ typedef enum ProcessFields { TGID = 52, } ProcessField; +typedef struct ProcessPidColumn_ { + int id; + char* label; +} ProcessPidColumn; + typedef struct Process_ { Object super; @@ -145,14 +151,13 @@ typedef struct ProcessFieldData_ { } ProcessFieldData; // Implemented in platform-specific code: -void Process_setupColumnWidths(); void Process_writeField(Process* this, RichString* str, ProcessField field); long Process_compare(const void* v1, const void* v2); void Process_delete(Object* cast); bool Process_isThread(Process* this); extern ProcessFieldData Process_fields[]; -extern char* Process_pidFormat; -extern char* Process_tpgidFormat; +extern ProcessPidColumn Process_pidColumns[]; +extern char Process_pidFormat[20]; typedef Process*(*Process_New)(struct Settings_*); typedef void (*Process_WriteField)(Process*, RichString*, ProcessField); @@ -176,6 +181,23 @@ static int Process_getuid = -1; #define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K) #define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K) +char Process_pidFormat[20] = "%7u "; + +static char Process_titleBuffer[20][20]; + +void Process_setupColumnWidths() { + int maxPid = Platform_getMaxPid(); + if (maxPid == -1) return; + int digits = ceil(log10(maxPid)); + assert(digits < 20); + for (int i = 0; Process_pidColumns[i].label; i++) { + assert(i < 20); + sprintf(Process_titleBuffer[i], "%*s ", digits, Process_pidColumns[i].label); + Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i]; + } + sprintf(Process_pidFormat, "%%%du ", digits); +} + void Process_humanNumber(RichString* str, unsigned long number, bool coloring) { char buffer[11]; int len; @@ -426,7 +448,7 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break; case TIME: Process_printTime(str, this->time); return; case TGID: snprintf(buffer, n, Process_pidFormat, this->tgid); break; - case TPGID: snprintf(buffer, n, Process_tpgidFormat, this->tpgid); break; + case TPGID: snprintf(buffer, n, Process_pidFormat, this->tpgid); break; case TTY_NR: snprintf(buffer, n, "%5u ", this->tty_nr); break; case USER: { if (Process_getuid != (int) this->st_uid) diff --git a/Process.h b/Process.h index ab7acb28..d856c035 100644 --- a/Process.h +++ b/Process.h @@ -53,6 +53,11 @@ typedef enum ProcessFields { TGID = 52, } ProcessField; +typedef struct ProcessPidColumn_ { + int id; + char* label; +} ProcessPidColumn; + typedef struct Process_ { Object super; @@ -126,14 +131,13 @@ typedef struct ProcessFieldData_ { } ProcessFieldData; // Implemented in platform-specific code: -void Process_setupColumnWidths(); void Process_writeField(Process* this, RichString* str, ProcessField field); long Process_compare(const void* v1, const void* v2); void Process_delete(Object* cast); bool Process_isThread(Process* this); extern ProcessFieldData Process_fields[]; -extern char* Process_pidFormat; -extern char* Process_tpgidFormat; +extern ProcessPidColumn Process_pidColumns[]; +extern char Process_pidFormat[20]; typedef Process*(*Process_New)(struct Settings_*); typedef void (*Process_WriteField)(Process*, RichString*, ProcessField); @@ -154,6 +158,10 @@ typedef struct ProcessClass_ { #define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K) #define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K) +extern char Process_pidFormat[20]; + +void Process_setupColumnWidths(); + void Process_humanNumber(RichString* str, unsigned long number, bool coloring); void Process_colorNumber(RichString* str, unsigned long long number, bool coloring); diff --git a/darwin/Platform.c b/darwin/Platform.c index 57c4d0ce..2c8ed2cf 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -82,7 +82,6 @@ void Platform_setBindings(Htop_Action* keys) { int Platform_numberOfFields = 100; char* Process_pidFormat = "%7u "; -char* Process_tpgidFormat = "%7u "; int Platform_getUptime() { struct timeval bootTime, currTime; @@ -117,29 +116,15 @@ int Platform_getMaxPid() { return 99999; } -void Process_setupColumnWidths() { - int maxPid = Platform_getMaxPid(); - if (maxPid == -1) return; - if (maxPid > 99999) { - Process_fields[PID].title = " PID "; - Process_fields[PPID].title = " PPID "; - Process_fields[TPGID].title = " TPGID "; - Process_fields[TGID].title = " TGID "; - Process_fields[PGRP].title = " PGRP "; - Process_fields[SESSION].title = " SESN "; - Process_pidFormat = "%7u "; - Process_tpgidFormat = "%7d "; - } else { - Process_fields[PID].title = " PID "; - Process_fields[PPID].title = " PPID "; - Process_fields[TPGID].title = "TPGID "; - Process_fields[TGID].title = " TGID "; - Process_fields[PGRP].title = " PGRP "; - Process_fields[SESSION].title = " SESN "; - Process_pidFormat = "%5u "; - Process_tpgidFormat = "%5d "; - } -} +ProcessPidColumn Process_pidColumns[] = { + { .id = PID, .label = "PID" }, + { .id = PPID, .label = "PPID" }, + { .id = TPGID, .label = "TPGID" }, + { .id = TGID, .label = "TGID" }, + { .id = PGRP, .label = "PGRP" }, + { .id = SESSION, .label = "SESN" }, + { .id = 0, .label = NULL }, +}; double Platform_setCPUValues(Meter* mtr, int cpu) { /* All just from CPUMeter.c */ diff --git a/darwin/Platform.h b/darwin/Platform.h index eaf0cd7f..f670c7f6 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -24,7 +24,6 @@ void Platform_setBindings(Htop_Action* keys); extern int Platform_numberOfFields; extern char* Process_pidFormat; -extern char* Process_tpgidFormat; int Platform_getUptime(); @@ -32,8 +31,6 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen); int Platform_getMaxPid(); -void Process_setupColumnWidths(); - double Platform_setCPUValues(Meter* mtr, int cpu); void Platform_setMemoryValues(Meter* mtr); diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c index 6e5720bb..b5734f81 100644 --- a/freebsd/FreeBSDProcess.c +++ b/freebsd/FreeBSDProcess.c @@ -75,32 +75,15 @@ ProcessFieldData Process_fields[] = { [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; -char* Process_pidFormat = "%7u "; -char* Process_tpgidFormat = "%7u "; - -void Process_setupColumnWidths() { - int maxPid = Platform_getMaxPid(); - if (maxPid == -1) return; - if (maxPid > 99999) { - Process_fields[PID].title = " PID "; - Process_fields[PPID].title = " PPID "; - Process_fields[TPGID].title = " TPGID "; - Process_fields[TGID].title = " TGID "; - Process_fields[PGRP].title = " PGRP "; - Process_fields[SESSION].title = " SESN "; - Process_pidFormat = "%7u "; - Process_tpgidFormat = "%7d "; - } else { - Process_fields[PID].title = " PID "; - Process_fields[PPID].title = " PPID "; - Process_fields[TPGID].title = "TPGID "; - Process_fields[TGID].title = " TGID "; - Process_fields[PGRP].title = " PGRP "; - Process_fields[SESSION].title = " SESN "; - Process_pidFormat = "%5u "; - Process_tpgidFormat = "%5d "; - } -} +ProcessPidColumn Process_pidColumns[] = { + { .id = PID, .label = "PID" }, + { .id = PPID, .label = "PPID" }, + { .id = TPGID, .label = "TPGID" }, + { .id = TGID, .label = "TGID" }, + { .id = PGRP, .label = "PGRP" }, + { .id = SESSION, .label = "SESN" }, + { .id = 0, .label = NULL }, +}; FreeBSDProcess* FreeBSDProcess_new(Settings* settings) { FreeBSDProcess* this = calloc(sizeof(FreeBSDProcess), 1); diff --git a/freebsd/FreeBSDProcess.h b/freebsd/FreeBSDProcess.h index bf0acae8..e11d40d0 100644 --- a/freebsd/FreeBSDProcess.h +++ b/freebsd/FreeBSDProcess.h @@ -33,9 +33,6 @@ extern ProcessClass FreeBSDProcess_class; extern ProcessFieldData Process_fields[]; extern char* Process_pidFormat; -extern char* Process_tpgidFormat; - -void Process_setupColumnWidths(); FreeBSDProcess* FreeBSDProcess_new(Settings* settings); diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 16e7f55b..fe2f6e18 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -217,40 +217,19 @@ ProcessFieldData Process_fields[] = { [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; -char* Process_pidFormat = "%7u "; -char* Process_tpgidFormat = "%7u "; - -void Process_setupColumnWidths() { - int maxPid = Platform_getMaxPid(); - if (maxPid == -1) return; - if (maxPid > 99999) { - Process_fields[PID].title = " PID "; - Process_fields[PPID].title = " PPID "; - #ifdef HAVE_OPENVZ - Process_fields[VPID].title = " VPID "; - #endif - Process_fields[TPGID].title = " TPGID "; - Process_fields[TGID].title = " TGID "; - Process_fields[PGRP].title = " PGRP "; - Process_fields[SESSION].title = " SESN "; - Process_fields[OOM].title = " OOM "; - Process_pidFormat = "%7u "; - Process_tpgidFormat = "%7d "; - } else { - Process_fields[PID].title = " PID "; - Process_fields[PPID].title = " PPID "; - #ifdef HAVE_OPENVZ - Process_fields[VPID].title = " VPID "; - #endif - Process_fields[TPGID].title = "TPGID "; - Process_fields[TGID].title = " TGID "; - Process_fields[PGRP].title = " PGRP "; - Process_fields[SESSION].title = " SESN "; - Process_fields[OOM].title = " OOM "; - Process_pidFormat = "%5u "; - Process_tpgidFormat = "%5d "; - } -} +ProcessPidColumn Process_pidColumns[] = { + { .id = PID, .label = "PID" }, + { .id = PPID, .label = "PPID" }, + #ifdef HAVE_OPENVZ + { .id = VPID, .label = "VPID" }, + #endif + { .id = TPGID, .label = "TPGID" }, + { .id = TGID, .label = "TGID" }, + { .id = PGRP, .label = "PGRP" }, + { .id = SESSION, .label = "SESN" }, + { .id = OOM, .label = "OOM" }, + { .id = 0, .label = NULL }, +}; ProcessClass LinuxProcess_class = { .super = { diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h index aa63b599..f2d81aa3 100644 --- a/linux/LinuxProcess.h +++ b/linux/LinuxProcess.h @@ -129,10 +129,7 @@ typedef struct LinuxProcess_ { extern ProcessFieldData Process_fields[]; -extern char* Process_pidFormat; -extern char* Process_tpgidFormat; - -void Process_setupColumnWidths(); +extern ProcessPidColumn Process_pidColumns[]; extern ProcessClass LinuxProcess_class; diff --git a/unsupported/Platform.c b/unsupported/Platform.c index 5f74ec2e..5dd2e8f8 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -80,7 +80,6 @@ void Platform_setBindings(Htop_Action* keys) { int Platform_numberOfFields = 100; char* Process_pidFormat = "%7u "; -char* Process_tpgidFormat = "%7u "; int Platform_getUptime() { return 0; @@ -107,7 +106,6 @@ void Process_setupColumnWidths() { Process_fields[PGRP].title = " PGRP "; Process_fields[SESSION].title = " SESN "; Process_pidFormat = "%7u "; - Process_tpgidFormat = "%7d "; } else { Process_fields[PID].title = " PID "; Process_fields[PPID].title = " PPID "; @@ -116,7 +114,6 @@ void Process_setupColumnWidths() { Process_fields[PGRP].title = " PGRP "; Process_fields[SESSION].title = " SESN "; Process_pidFormat = "%5u "; - Process_tpgidFormat = "%5d "; } } diff --git a/unsupported/Platform.h b/unsupported/Platform.h index 2b987405..155b2d30 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -24,7 +24,6 @@ void Platform_setBindings(Htop_Action* keys); extern int Platform_numberOfFields; extern char* Process_pidFormat; -extern char* Process_tpgidFormat; int Platform_getUptime();