Only remove directories when removing recursively.
[oweals/busybox.git] / utility.c
index 8827b4a6514d0a1b0e55d721cb1f59362768cdaa..8c3c5e5fad4c10a6a29274b1685a30ab876e4ffa 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -1290,7 +1290,7 @@ extern pid_t* findPidByName( char* pidName)
         * some new processes start up while we wait. The kernel will
         * just ignore any extras if we give it too many, and will trunc.
         * the list if we give it too few.  */
-       pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
+       pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
        pid_array[0] = num_pids+10;
 
        /* Now grab the pid list */
@@ -1316,9 +1316,7 @@ extern pid_t* findPidByName( char* pidName)
 
                if ((strstr(info.command_line, pidName) != NULL)
                                && (strlen(pidName) == strlen(info.command_line))) {
-                       pidList=realloc( pidList, sizeof(pid_t) * (j+2));
-                       if (pidList==NULL)
-                               fatalError(memory_exhausted);
+                       pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
                        pidList[j++]=info.pid;
                }
        }
@@ -1362,14 +1360,12 @@ extern pid_t* findPidByName( char* pidName)
                FILE *status;
                char filename[256];
                char buffer[256];
-               char* p;
 
                /* If it isn't a number, we don't want it */
                if (!isdigit(*next->d_name))
                        continue;
 
-               /* Now open the status file */
-               sprintf(filename, "/proc/%s/status", next->d_name);
+               sprintf(filename, "/proc/%s/cmdline", next->d_name);
                status = fopen(filename, "r");
                if (!status) {
                        continue;
@@ -1377,24 +1373,12 @@ extern pid_t* findPidByName( char* pidName)
                fgets(buffer, 256, status);
                fclose(status);
 
-               /* Make sure we only match on the process name */
-               p=buffer+5; /* Skip the name */
-               while ((p)++) {
-                       if (*p==0 || *p=='\n') {
-                               *p='\0';
-                               break;
-                       }
-               }
-               p=buffer+6; /* Skip the "Name:\t" */
-
-               if ((strstr(p, pidName) != NULL)
-                               && (strlen(pidName) == strlen(p))) {
-                       pidList=realloc( pidList, sizeof(pid_t) * (i+2));
-                       if (pidList==NULL)
-                               fatalError(memory_exhausted);
+               if (strstr(get_last_path_component(buffer), pidName) != NULL) {
+                       pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
                        pidList[i++]=strtol(next->d_name, NULL, 0);
                }
        }
+
        if (pidList)
                pidList[i]=0;
        return pidList;
@@ -1624,7 +1608,7 @@ extern char *get_line_from_file(FILE *file)
                        break;
                /* grow the line buffer as necessary */
                if (idx > linebufsz-2)
-                       linebuf = realloc(linebuf, linebufsz += GROWBY);
+                       linebuf = xrealloc(linebuf, linebufsz += GROWBY);
                linebuf[idx++] = (char)ch;
                if ((char)ch == '\n')
                        break;