From b308d81e927d28cb0223378049defc133787727d Mon Sep 17 00:00:00 2001
From: Denis Vlasenko <vda.linux@googlemail.com>
Date: Tue, 28 Aug 2007 19:35:34 +0000
Subject: top: use poll instead of select for waiting on one descriptor
 smart_ulltoa5: make it more cryptic. -50 bytes.

function                                             old     new   delta
passwd_main                                         1095    1103      +8
getNum                                               557     565      +8
buffer_fill_and_print                                 73      76      +3
udhcpc_main                                         2393    2395      +2
mkfs_minix_main                                     3071    3070      -1
dname_enc                                            377     373      -4
expmeta                                              480     472      -8
smart_ulltoa5                                        334     283     -51
top_main                                             911     815     -96
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/5 up/down: 21/-160)          Total: -139 bytes
   text    data     bss     dec     hex filename
 770872    1063   10788  782723   bf183 busybox_old
 770732    1063   10788  782583   bf0f7 busybox_unstripped
---
 procps/top.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

(limited to 'procps')

diff --git a/procps/top.c b/procps/top.c
index c02a959e0..abc7a4363 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -448,7 +448,7 @@ static void display_status(int count, int scr_width)
 #endif
 
 		if (s->vsz >= 100*1024)
-			sprintf(vsz_str_buf, "%6ldM", s->vsz/1024);
+			sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
 		else
 			sprintf(vsz_str_buf, "%7ld", s->vsz);
 		// PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
@@ -519,23 +519,29 @@ int top_main(int argc, char **argv);
 int top_main(int argc, char **argv)
 {
 	int count, lines, col;
-	unsigned interval = 5; /* default update rate is 5 seconds */
-	unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
+	unsigned interval;
+	int iterations = -1; /* infinite */
 	char *sinterval, *siterations;
 #if ENABLE_FEATURE_USE_TERMIOS
 	struct termios new_settings;
-	struct timeval tv;
-	fd_set readfds;
+	struct pollfd pfd[1];
 	unsigned char c;
+
+	pfd[0].fd = 0;
+	pfd[0].events = POLLIN;
 #endif /* FEATURE_USE_TERMIOS */
 
-	interval = 5;
+	interval = 5; /* default update rate is 5 seconds */
 
 	/* do normal option parsing */
 	opt_complementary = "-";
 	getopt32(argv, "d:n:b", &sinterval, &siterations);
-	if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
-	if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
+	if (option_mask32 & 0x1) {
+		/* Need to limit it to not overflow poll timeout */
+		interval = xatou16(sinterval); // -d
+	}
+	if (option_mask32 & 0x2)
+		iterations = xatoi_u(siterations); // -n
 	//if (option_mask32 & 0x4) // -b
 
 	/* change to /proc */
@@ -584,9 +590,8 @@ int top_main(int argc, char **argv)
 				| PSSCAN_UTIME
 				| PSSCAN_STATE
 				| PSSCAN_COMM
-				| PSSCAN_SID
 				| PSSCAN_UIDGID
-		))) {
+		)) != NULL) {
 			int n = ntop;
 			top = xrealloc(top, (++ntop) * sizeof(*top));
 			top[n].pid = p->pid;
@@ -622,15 +627,9 @@ int top_main(int argc, char **argv)
 		/* show status for each of the processes */
 		display_status(count, col);
 #if ENABLE_FEATURE_USE_TERMIOS
-		tv.tv_sec = interval;
-		tv.tv_usec = 0;
-		FD_ZERO(&readfds);
-		FD_SET(0, &readfds);
-		select(1, &readfds, NULL, NULL, &tv);
-		if (FD_ISSET(0, &readfds)) {
-			if (read(0, &c, 1) <= 0) {   /* signal */
-				return EXIT_FAILURE;
-			}
+		if (poll(pfd, 1, interval * 1000) != 0) {
+			if (read(0, &c, 1) != 1)    /* signal */
+				break;
 			if (c == 'q' || c == initial_settings.c_cc[VINTR])
 				break;
 			if (c == 'M') {
@@ -662,7 +661,7 @@ int top_main(int argc, char **argv)
 #endif
 			}
 		}
-		if (!--iterations)
+		if (iterations >= 0 && !--iterations)
 			break;
 #else
 		sleep(interval);
-- 
cgit v1.2.3