1 /*
2 * ps related stuff
3 *
4 * RCS:
5 * $Revision$
6 * $Date$
7 *
8 * Security:
9 * Unclassified
10 *
11 * Description:
12 * text
13 *
14 * Input Parameters:
15 * type identifier description
16 *
17 * text
18 *
19 * Output Parameters:
20 * type identifier description
21 *
22 * text
23 *
24 * Return Values:
25 * value description
26 *
27 * Side Effects:
28 * text
29 *
30 * Limitations and Comments:
31 * text
32 *
33 * Development History:
34 * who when why
35 * muquit@semcor.com 04-Sep-95 first cut
36 * 02-Mar-97 on my home linux box
37 * - found out, no header files included
38 * - freeing without checking
39 */
40
41 #include "xhead.h"
42 #include "mxkill.h"
43
44 #define MaxItems 1024
45 char
46 *lines[MaxItems];
47
48 char
49 **processes = &lines[0];
50
51 char
52 *psheader=(char *) NULL;
53
54 int
55 Gmaxline;
56
57 int
58 pidcol=(-1);
59 #include "xhead.h"
60 #include "mxkill.h"
61
GetpsInfo(commandbuf)62 unsigned int GetpsInfo(commandbuf)
63 char
64 *commandbuf;
65 {
66 int
67 i=0;
68
69 FILE
70 *pp;
71
72 char
73 buf[BUFSIZ];
74
75 int
76 newpidcol=(-1);
77
78 *buf='\0';
79
80
81 pp=popen(commandbuf,"r");
82 if (pp == (FILE *) NULL)
83 Error("Unable to open pipe to ps",(char *) NULL);
84
85 while ((fgets(buf,sizeof(buf),pp) != NULL))
86 {
87 buf[(int)strlen(buf)-1]='\0';
88
89 if (newpidcol == (-1))
90 {
91 newpidcol=StrIndex(buf," PID");
92 if (newpidcol != (-1))
93 {
94 if (psheader != (char *) NULL)
95 XtFree(psheader);
96 psheader=XtNewString(buf);
97 continue;
98 }
99 }
100 if (lines[i] != (char *) NULL)
101 XtFree(lines[i]);
102 lines[i]=XtNewString(buf);
103 if (++i == MaxItems)
104 break;
105 }
106
107 (void) pclose(pp);
108
109 if (newpidcol != (-1))
110 pidcol=newpidcol;
111
112 if (i > 0 || (newpidcol != (-1)))
113 {
114 Gmaxline=i;
115 return (True);
116 }
117 else
118 return (False);
119 }
120
121 /*
122 ** from xzap
123 */
StrIndex(s,t)124 int StrIndex(s,t)
125 char
126 *s,
127 *t;
128 {
129 int
130 i,
131 n;
132
133 n=(int) strlen(t);
134
135 for (i=0; s[i] != '\0'; i++)
136 {
137 if (strncmp(s+i,t,n) == 0)
138 return (i);
139 }
140 return (-1);
141 }
142