fix read_cmdline API and a small buglet where "[comm]" may be too narrow
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 11 Sep 2009 22:15:34 +0000 (00:15 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 11 Sep 2009 22:15:34 +0000 (00:15 +0200)
function                                             old     new   delta
func_args                                             25      26      +1
read_cmdline                                         119     114      -5
display_process_list                                1462    1447     -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 1/-20)             Total: -19 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/procps.c
procps/ps.c
procps/top.c

index 14a16674c334aa2e64cd50bd847f8cee983825ee..fd61517c28885c9e033ce09f1319ad9e01f0ef28 100644 (file)
@@ -1359,9 +1359,9 @@ enum {
 //procps_status_t* alloc_procps_scan(void) FAST_FUNC;
 void free_procps_scan(procps_status_t* sp) FAST_FUNC;
 procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
-/* Format cmdline (up to col chars) into char buf[col+1] */
+/* Format cmdline (up to col chars) into char buf[size] */
 /* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
-void read_cmdline(char *buf, int col, unsigned pid, const char *comm) FAST_FUNC;
+void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
 pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
 pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
 
index 9a8e42df77fe2bff1dd84bbfc503fe44927760e0..974661785310c5dfdeedaf1a2cd1113b7f704505 100644 (file)
@@ -464,18 +464,19 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
 
 void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
 {
-       ssize_t sz;
+       int sz;
        char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
 
        sprintf(filename, "/proc/%u/cmdline", pid);
-       sz = open_read_close(filename, buf, col);
+       sz = open_read_close(filename, buf, col - 1);
        if (sz > 0) {
                buf[sz] = '\0';
                while (--sz >= 0 && buf[sz] == '\0')
                        continue;
-               while (--sz >= 0)
+               do {
                        if ((unsigned char)(buf[sz]) < ' ')
                                buf[sz] = ' ';
+               } while (--sz >= 0);
        } else {
                snprintf(buf, col, "[%s]", comm);
        }
index 4a6b60bdcebe59220e3d85f88d8cdfb9789232af..6523f0f38111205c3d130eae7d37f60bff1694f7 100644 (file)
@@ -186,7 +186,7 @@ static void func_comm(char *buf, int size, const procps_status_t *ps)
 
 static void func_args(char *buf, int size, const procps_status_t *ps)
 {
-       read_cmdline(buf, size, ps->pid, ps->comm);
+       read_cmdline(buf, size+1, ps->pid, ps->comm);
 }
 
 static void func_pid(char *buf, int size, const procps_status_t *ps)
index 92360a0058d85275b3afd853ae8930219e76b063..7133e5c99dcfe1ae5b2720b082345d66a22a75f9 100644 (file)
@@ -617,7 +617,7 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
 #endif
                );
                if ((int)(col + 1) < scr_width)
-                       read_cmdline(line_buf + col, scr_width - col - 1, s->pid, s->comm);
+                       read_cmdline(line_buf + col, scr_width - col, s->pid, s->comm);
                fputs(line_buf, stdout);
                /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
                        cur_jif.busy - prev_jif.busy, cur_jif.total - prev_jif.total); */