From a3c8285237c673189b28c897910cf6eb8629bfaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Wed, 27 Jan 2021 15:11:42 +0100 Subject: [PATCH] Refactor to tty_nr process field display If no terminal name can be found, fall back to generic display method with major and minor device numbers. Print special value '(none)' in case both are zero. --- Process.c | 12 +++++++++++- linux/LinuxProcess.c | 13 ++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Process.c b/Process.c index 3f91b67d..f164e5f5 100644 --- a/Process.c +++ b/Process.c @@ -365,7 +365,17 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field case TIME: Process_printTime(str, this->time); return; case TGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tgid); break; case TPGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tpgid); break; - case TTY_NR: xSnprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break; + case TTY_NR: { + unsigned int major = major(this->tty_nr); + unsigned int minor = minor(this->tty_nr); + if (major == 0 && minor == 0) { + attr = CRT_colors[PROCESS_SHADOW]; + xSnprintf(buffer, n, "(none) "); + } else { + xSnprintf(buffer, n, "%3u:%3u ", major, minor); + } + break; + } case USER: { if (Process_getuid != this->st_uid) attr = CRT_colors[PROCESS_SHADOW]; diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 488b9372..79e73e84 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -607,15 +607,14 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces int attr = CRT_colors[DEFAULT_COLOR]; size_t n = sizeof(buffer) - 1; switch (field) { - case TTY_NR: { + case TTY_NR: if (lp->ttyDevice) { - xSnprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */); - } else { - attr = CRT_colors[PROCESS_SHADOW]; - xSnprintf(buffer, n, "? "); + xSnprintf(buffer, n, "%-8s ", lp->ttyDevice + 5 /* skip "/dev/" */); + break; } - break; - } + + Process_writeField(this, str, field); + return; case CMINFLT: Process_colorNumber(str, lp->cminflt, coloring); return; case CMAJFLT: Process_colorNumber(str, lp->cmajflt, coloring); return; case M_DRS: Process_humanNumber(str, lp->m_drs * pageSizeKB, coloring); return;