summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-03-22 00:04:02 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-03-22 00:04:02 +0100
commit6bf1e0058116a093e135dc3424ed52bfbc53ee23 (patch)
tree912704e4f32bf529cf443c2a2d44ff3cb6c9eae6
parentae074a9a2e57d727f04f99f90ca0856ecc81ee08 (diff)
downloadpx-6bf1e0058116a093e135dc3424ed52bfbc53ee23.tar.gz
px-6bf1e0058116a093e135dc3424ed52bfbc53ee23.tar.xz
px-6bf1e0058116a093e135dc3424ed52bfbc53ee23.zip
refactor time formatting
-rw-r--r--px.c55
1 files changed, 24 insertions, 31 deletions
diff --git a/px.c b/px.c
index 5776476..2abcf07 100644
--- a/px.c
+++ b/px.c
@@ -27,6 +27,23 @@ print_human(intmax_t i)
                 printf("%4.0f%s", d, u);
 }
 
+static void
+print_time(time_t t) {
+	unsigned int dd, hh, mm, ss;
+
+	ss = t % 60; t /= 60;
+	mm = t % 60; t /= 60;
+	hh = t % 24; t /= 24;
+	dd = t;
+
+	if (dd)
+		printf(" %3ud%02uh", dd, hh);
+	else if (hh)
+		printf("  %2uh%02um", hh, mm);
+	else
+		printf("   %2u:%02u", mm, ss);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -62,17 +79,19 @@ match:
 		}
 
 		if (matched == 1)
-			printf("  PID USER     %%CPU  VSZ  RSS START ELAPSED   TIME COMMAND\n");
+			printf("  PID USER     %%CPU  VSZ  RSS START ELAPSED CPUTIME COMMAND\n");
 
 		printf("%5d", p->tid);
 		printf(" %-8.8s", p->euser);
 
 		double pcpu = 0;
 		time_t seconds;
+		time_t cputime;
 
+		cputime = (p->utime + p->stime) / clktck;
 		seconds = si.uptime - p->start_time / clktck;
 		if (seconds)
-			pcpu = ((p->utime + p->stime) * 100.0 / clktck) / seconds;
+			pcpu = (cputime * 100.0) / seconds;
 
 		printf(" %4.1f", pcpu);
 		print_human(p->vm_size*1024);
@@ -83,7 +102,7 @@ match:
 		start = (now - si.uptime) + p->start_time / clktck;
 		seconds_ago = now - start;
 		if (seconds_ago < 0)
-			seconds_ago=0;
+			seconds_ago = 0;
 
 		char buf[7];
 		if (seconds_ago > 3600*24)
@@ -92,35 +111,9 @@ match:
 			strftime(buf, sizeof buf, "%H:%M", localtime(&start));
 		printf(" %s", buf);
 
-		time_t t = seconds_ago;
-		unsigned dd,hh,mm,ss;
-		ss = t%60;
-		t /= 60;
-		mm = t%60;
-		t /= 60;
-		hh = t%24;
-		t /= 24;
-		dd = t;
-
-		if (dd)
-			printf(" %3ud%02uh", dd, hh);
-		else if (hh)
-			printf("  %2uh%02um", hh, mm);
-		else
-			printf("   %2u:%02u", mm, ss);
+		print_time(seconds_ago);
 
-
-		t = (p->utime + p->stime) / clktck;
-		ss = t%60;
-		t /= 60;
-		mm = t%60;
-		t /= 60;
-		hh = t%24;
-
-		if (hh)
-			printf(" %3uh%02u", hh, mm);
-		else
-			printf("  %2u:%02u", mm, ss);
+		print_time(cputime);
 
 		if (p->cmdline) {
 			for (int i = 0; p->cmdline[i]; i++) {