applets: fix compile-time warning
[oweals/busybox.git] / libbb / find_pid_by_name.c
index 73467446f31965d6c7a7447fad329cb3a4e0ca49..e98616940c061ce28b9a62d74203ae7ac13fd590 100644 (file)
@@ -4,13 +4,9 @@
  *
  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  *
- * Licensed under the GPL v2, see the file LICENSE in this tarball.
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  */
 
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdlib.h>
 #include "libbb.h"
 
 /* find_pid_by_name()
  *  Returns a list of all matching PIDs
  *  It is the caller's duty to free the returned pidlist.
  */
-long* find_pid_by_name( const char* pidName)
+pid_t* find_pid_by_name(const char* procName)
 {
-       long* pidList;
-       int i=0;
-       procps_status_t * p;
+       pid_t* pidList;
+       int i = 0;
+       procps_status_t* p = NULL;
 
-       pidList = xmalloc(sizeof(long));
-       while ((p = procps_scan(0)) != 0)
-       {
-               if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) {
-                       pidList=xrealloc( pidList, sizeof(long) * (i+2));
-                       pidList[i++]=p->pid;
+       pidList = xmalloc(sizeof(*pidList));
+       while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) {
+               if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) {
+                       pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
+                       pidList[i++] = p->pid;
                }
        }
 
-       pidList[i] = i==0 ? -1 : 0;
+       pidList[i] = 0;
        return pidList;
 }
 
-long *pidlist_reverse(long *pidList)
+pid_t *pidlist_reverse(pid_t *pidList)
 {
-       int i=0;
-       while (pidList[i] > 0 && ++i);
-       if ( i-- > 0) {
-               long k;
+       int i = 0;
+       while (pidList[i])
+               i++;
+       if (--i >= 0) {
+               pid_t k;
                int j;
                for (j = 0; i > j; i--, j++) {
                        k = pidList[i];