fix read_cmdline API and a small buglet where "[comm]" may be too narrow
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>
diff --git a/libbb/procps.c b/libbb/procps.c
index 9a8e42d..9746617 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -464,18 +464,19 @@
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);
}