libgpg-error: fix riscv64
[openadk.git] / package / sash / src / ps.c
blob610650ec628b0db4318c50d407ecaadcc300ca0c
1 /* ps.c:
3 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
11 #include "sash.h"
13 #include <fcntl.h>
14 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <dirent.h>
18 #include <pwd.h>
19 #include <grp.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <linux/major.h>
23 #include <linux/param.h>
24 #include <sys/time.h>
25 #include <sys/param.h>
27 char psbuf[256];
28 char name[40];
29 int pid, state;
30 char statec;
31 int ppid, pgrp, session;
32 dev_t tty;
33 char tty_name[10];
35 char master[] = "pqrstuvwxyzabcde";
37 #define MAJOR(x) ((x) >> 8)
38 #define MINOR(x) ((x) & 0xff)
40 int port_xlate[16] = {1, 3, 5, 7,9 ,11,13,15,
41 2, 4, 6, 8,10,12,14,16};
43 void dev_to_name(dev_t dev, char * ttyname)
45 strcpy(ttyname, "");
46 if (MAJOR(dev) == 75)
47 sprintf(ttyname,"X%d", MINOR(dev));
48 else if (MAJOR(dev) == TTY_MAJOR)
49 sprintf(ttyname,"S%d", MINOR(dev)-64);
50 else if (MAJOR(dev) == PTY_SLAVE_MAJOR)
51 sprintf(ttyname,"%c%x", master[MINOR(dev) / 16], MINOR(dev) & 0xf);
54 void
55 do_ps(argc, argv)
56 int argc;
57 char **argv;
59 int i;
60 int h;
61 int max;
62 FILE * f;
63 DIR * d;
64 unsigned long bytes, sbytes;
65 struct dirent * de;
66 char *ext;
67 int l;
68 time_t time_now;
69 long uptime_secs;
70 float idle_secs;
71 float seconds, start, total_time;
72 int utime, stime, start_time;
73 int pcpu;
74 /*extern int _vfprintf_fp_ref, _vfscanf_fp_ref;*/
76 #if 0
77 fclose(stdin);
78 #endif
80 printf(" PID PORT STAT SIZE SHARED %%CPU COMMAND\n"/*, _vfprintf_fp_ref, _vfscanf_fp_ref*/);
82 h = open("/proc/uptime", O_RDONLY);
84 if (h==-1) {
85 perror("Unable to open /proc/uptime\n");
86 return;
89 l = read(h, psbuf, 255);
91 close(h);
94 if (l<=0) {
95 perror("Unable to read uptime");
96 return;
100 psbuf[l] = '\0';
101 psbuf[255] = '\0';
103 ext = psbuf;
106 uptime_secs = atol(ext);
109 time_now = time(0);
111 d = opendir("/proc");
112 if (!d)
113 return;
115 while (de = readdir(d)) {
118 for(i=0;i<strlen(de->d_name);i++)
119 if (!isdigit(de->d_name[i]))
120 goto next;
122 sprintf(psbuf, "/proc/%s/stat", de->d_name);
124 h = open(psbuf, O_RDONLY);
126 if (h==-1)
127 continue;
129 l = read(h, psbuf, 255);
130 if (l<=0) {
131 perror("Unable to read status");
132 close(h);
133 continue;
136 psbuf[l] = '\0';
137 psbuf[255] = '\0';
139 ext = strrchr(psbuf, ')');
140 ext[0] = '\0';
142 statec = ext[2];
144 ext += 4;
146 ppid = atoi(ext);
147 ext = strchr(ext, ' ')+1;
149 pgrp = atoi(ext);
150 ext = strchr(ext, ' ')+1;
152 session = atoi(ext);
153 ext = strchr(ext, ' ')+1;
155 tty = atoi(ext);
156 ext = strchr(ext, ' ')+1;
158 //printf("1|%s\n", ext);
159 //tpgid
160 ext = strchr(ext, ' ')+1;
162 //printf("2|%s\n", ext);
163 //flags
164 ext = strchr(ext, ' ')+1;
166 //printf("3|%s\n", ext);
167 //min_flt
168 ext = strchr(ext, ' ')+1;
170 //printf("4|%s\n", ext);
171 //cmin_flt
172 ext = strchr(ext, ' ')+1;
174 //printf("5|%s\n", ext);
175 //maj_flt
176 ext = strchr(ext, ' ')+1;
178 //printf("6|%s\n", ext);
179 //cmaj_flt
180 ext = strchr(ext, ' ')+1;
182 //printf("7|%s\n", ext);
183 utime = atoi(ext);
184 ext = strchr(ext, ' ')+1;
186 //printf("8|%s\n", ext);
187 stime = atoi(ext);
188 ext = strchr(ext, ' ')+1;
190 //printf("9|%s\n", ext);
191 //cutime
192 ext = strchr(ext, ' ')+1;
194 //printf("10|%s\n", ext);
195 //cstime
196 ext = strchr(ext, ' ')+1;
198 //priority
199 ext = strchr(ext, ' ')+1;
201 //nice
202 ext = strchr(ext, ' ')+1;
204 //timeout
205 ext = strchr(ext, ' ')+1;
207 //it_real_value
208 ext = strchr(ext, ' ')+1;
210 start_time = atoi(ext);
212 ext = strchr(psbuf, '(');
213 ext++;
214 strcpy(name, ext);
216 pid = atoi(psbuf);
219 state = statec;
221 close(h);
223 dev_to_name(tty, tty_name);
225 bytes = 0;
226 sbytes = 0;
227 sprintf(psbuf, "/proc/%s/status", de->d_name);
229 f = fopen(psbuf, "r");
231 if (f) {
232 while (fgets(psbuf, 250, f)) {
233 if (strncmp(psbuf, "Mem:", 4) == 0) {
234 bytes = atol(psbuf+5);
235 bytes /= 1024;
236 } else if (strncmp(psbuf, "Shared:", 7) == 0) {
237 sbytes = atol(psbuf+8);
238 sbytes /= 1024;
239 } else if (strncmp(psbuf, "VmSize:", 7) == 0) {
240 bytes = atol(psbuf+8);
243 fclose(f);
247 seconds = ((uptime_secs * (long)HZ) - start_time) / HZ;
249 /*printf("seconds=%s\n", gcvt(seconds, 15, psbuf));*/
251 start = time_now - seconds;
254 printf("1\n");
256 gcvt(start, 15, psbuf);
258 printf("2\n");
260 printf("start=%s\n", psbuf);
262 printf("utime=%d, stime=%d. start_time=%d\n", utime, stime, start_time);
265 total_time = (utime + stime);
267 /*printf("total_time=%s\n", gcvt(total_time, 15, psbuf));*/
269 pcpu = seconds ?
270 (total_time * 10.0f * 100.0f / (float)HZ) / seconds :
272 if (pcpu > 999) pcpu = 999;
275 sprintf(psbuf, "/proc/%s/cmdline", de->d_name);
276 h = open(psbuf, O_RDONLY);
278 if (h == -1) {
279 perror("Unable to open cmdline");
280 continue;
283 l = read(h, psbuf, 255);
284 if (l < 0) {
285 perror("Unable to read cmdline");
286 close(h);
287 continue;
290 close(h);
293 * the args are NUL separated, substitute spaces instead
295 psbuf[l] = '\0';
296 i=l;
297 while(psbuf[i] == '\0')
298 i--; /* Don't bother with trailing NULs */
299 while(--i > 0)
300 if (psbuf[i] == '\0')
301 psbuf[i] = ' ';
303 printf("%5d %4s %c %4ldK %3ldK %2u.%u %s\n", pid, tty_name, state,
304 bytes, sbytes,
305 pcpu / 10, pcpu % 10,
306 /*(int)seconds / 60, (int)seconds % 60,*/
307 l ? psbuf : name);
308 next:
312 closedir(d);