summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--px.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/px.c b/px.c
index 2abcf07..877baee 100644
--- a/px.c
+++ b/px.c
@@ -1,3 +1,11 @@
+/*
+ * px - search for processes and print top(1)-like status
+ *
+ * To the extent possible under law, Leah Neukirchen <leah@vuxu.org>
+ * has waived all copyright and related or neighboring rights to this work.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
 #include <sys/auxv.h>
 #include <sys/sysinfo.h>
 
@@ -47,9 +55,7 @@ print_time(time_t t) {
 int
 main(int argc, char *argv[])
 {
-	proc_t **result;
 	int clktck;
-
 	if ((clktck = getauxval(AT_CLKTCK)) <= 0)
 		if ((clktck = sysconf(_SC_CLK_TCK)) <= 0)
 			clktck = 100; 	/* educated guess */
@@ -59,7 +65,8 @@ main(int argc, char *argv[])
 
 	time_t now = time(0);
 
-	result = readproctab(PROC_FILLMEM | PROC_FILLCOM | PROC_FILLUSR |
+	proc_t **result;
+	result = readproctab(PROC_FILLCOM | PROC_FILLUSR |
 	    PROC_FILLSTAT | PROC_FILLSTATUS);
 
 	int matched = 0;
@@ -82,29 +89,28 @@ match:
 			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;
-
+		time_t seconds, cputime;
 		cputime = (p->utime + p->stime) / clktck;
 		seconds = si.uptime - p->start_time / clktck;
 		if (seconds)
 			pcpu = (cputime * 100.0) / seconds;
-
 		printf(" %4.1f", pcpu);
+
 		print_human(p->vm_size*1024);
+
 		print_human(p->vm_rss*1024);
 
+		char buf[7];
 		time_t start;
 		time_t seconds_ago;
 		start = (now - si.uptime) + p->start_time / clktck;
 		seconds_ago = now - start;
 		if (seconds_ago < 0)
 			seconds_ago = 0;
-
-		char buf[7];
 		if (seconds_ago > 3600*24)
 			strftime(buf, sizeof buf, "%b%d", localtime(&start));
 		else
@@ -115,17 +121,14 @@ match:
 
 		print_time(cputime);
 
-		if (p->cmdline) {
-			for (int i = 0; p->cmdline[i]; i++) {
+		if (p->cmdline)
+			for (int i = 0; p->cmdline[i]; i++)
 				printf(" %s", p->cmdline[i]);
-			}
-		} else {
-			// kernel threads
+		else		// kernel threads
 			printf(" [%s]", p->cmd);
-		}
-
 		if (p->state == 'Z')
 			printf(" <defunct>");
+
 		printf("\n");
 	}