libbb: move nuke_str() from passwd into libbb
[oweals/busybox.git] / libbb / procps.c
index e15ddd1e5e1820d7d70eebd9d837780bcb3133f6..5b68d343138566656441d00f7f508107ee601be4 100644 (file)
@@ -127,10 +127,11 @@ static unsigned long fast_strtoul_16(char **endptr)
        char *str = *endptr;
        unsigned long n = 0;
 
-       while ((c = *str++) != ' ') {
+       /* Need to stop on both ' ' and '\n' */
+       while ((c = *str++) > ' ') {
                c = ((c|0x20) - '0');
                if (c > 9)
-                       // c = c + '0' - 'a' + 10:
+                       /* c = c + '0' - 'a' + 10: */
                        c = c - ('a' - '0' - 10);
                n = n*16 + c;
        }
@@ -143,11 +144,12 @@ static unsigned long fast_strtoul_16(char **endptr)
 /* We cut a lot of corners here for speed */
 static unsigned long fast_strtoul_10(char **endptr)
 {
-       char c;
+       unsigned char c;
        char *str = *endptr;
        unsigned long n = *str - '0';
 
-       while ((c = *++str) != ' ')
+       /* Need to stop on both ' ' and '\n' */
+       while ((c = *++str) > ' ')
                n = n*10 + (c - '0');
 
        *endptr = str + 1; /* We skip trailing space! */
@@ -178,7 +180,7 @@ static char *skip_fields(char *str, int count)
 
 #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
 int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
-                     void (*cb)(struct smaprec *, void *), void *data)
+               void (*cb)(struct smaprec *, void *), void *data)
 {
        FILE *file;
        struct smaprec currec;
@@ -198,7 +200,7 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
        memset(&currec, 0, sizeof(currec));
        while (fgets(buf, PROCPS_BUFSIZE, file)) {
                // Each mapping datum has this form:
-               // f7d29000-f7d39000 rw-s ADR M:m OFS FILE
+               // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
                // Size:                nnn kB
                // Rss:                 nnn kB
                // .....
@@ -223,7 +225,7 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
                tp = strchr(buf, '-');
                if (tp) {
                        // We reached next mapping - the line of this form:
-                       // f7d29000-f7d39000 rw-s ADR M:m OFS FILE
+                       // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
 
                        if (cb) {
                                /* If we have a previous record, there's nothing more
@@ -242,7 +244,7 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
 
                        strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
 
-                       // skipping "rw-s ADR M:m OFS "
+                       // skipping "rw-s FILEOFS M:m INODE "
                        tp = skip_whitespace(skip_fields(tp, 4));
                        // filter out /dev/something (something != zero)
                        if (strncmp(tp, "/dev/", 5) != 0 || strcmp(tp, "/dev/zero\n") == 0) {
@@ -423,7 +425,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
                        if (n < 11)
                                continue; /* bogus data, get next /proc/XXX */
 # if ENABLE_FEATURE_TOP_SMP_PROCESS
-                       if (n < 11+15)
+                       if (n == 11)
                                sp->last_seen_on_cpu = 0;
 # endif
 
@@ -581,6 +583,8 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
                buf[sz] = '\0';
                while (--sz >= 0 && buf[sz] == '\0')
                        continue;
+               /* Prevent basename("process foo/bar") = "bar" */
+               strchrnul(buf, ' ')[0] = '\0';
                base = bb_basename(buf); /* before we replace argv0's NUL with space */
                while (sz >= 0) {
                        if ((unsigned char)(buf[sz]) < ' ')