whitespace
[oweals/busybox.git] / libbb / find_pid_by_name.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL v2, see the file LICENSE in this tarball.
8  */
9
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include "libbb.h"
15
16 /* find_pid_by_name()
17  *
18  *  Modified by Vladimir Oleynik for use with libbb/procps.c
19  *  This finds the pid of the specified process.
20  *  Currently, it's implemented by rummaging through
21  *  the proc filesystem.
22  *
23  *  Returns a list of all matching PIDs
24  *  It is the caller's duty to free the returned pidlist.
25  */
26 long* find_pid_by_name( const char* pidName)
27 {
28         long* pidList;
29         int i=0;
30         procps_status_t * p;
31
32         pidList = xmalloc(sizeof(long));
33         while ((p = procps_scan(0)) != 0)
34         {
35                 if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) {
36                         pidList=xrealloc( pidList, sizeof(long) * (i+2));
37                         pidList[i++]=p->pid;
38                 }
39         }
40
41         pidList[i] = i==0 ? -1 : 0;
42         return pidList;
43 }
44
45 long *pidlist_reverse(long *pidList)
46 {
47         int i=0;
48         while (pidList[i] > 0 && ++i);
49         if ( i-- > 0) {
50                 long k;
51                 int j;
52                 for (j = 0; i > j; i--, j++) {
53                         k = pidList[i];
54                         pidList[i] = pidList[j];
55                         pidList[j] = k;
56                 }
57         }
58         return pidList;
59 }