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.
14 #include <sys/types.h>
22 #include <linux/major.h>
23 #include <linux/param.h>
25 #include <sys/param.h>
31 int ppid
, pgrp
, session
;
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
)
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);
64 unsigned long bytes
, sbytes
;
71 float seconds
, start
, total_time
;
72 int utime
, stime
, start_time
;
74 /*extern int _vfprintf_fp_ref, _vfscanf_fp_ref;*/
80 printf(" PID PORT STAT SIZE SHARED %%CPU COMMAND\n"/*, _vfprintf_fp_ref, _vfscanf_fp_ref*/);
82 h
= open("/proc/uptime", O_RDONLY
);
85 perror("Unable to open /proc/uptime\n");
89 l
= read(h
, psbuf
, 255);
95 perror("Unable to read uptime");
106 uptime_secs
= atol(ext
);
111 d
= opendir("/proc");
115 while (de
= readdir(d
)) {
118 for(i
=0;i
<strlen(de
->d_name
);i
++)
119 if (!isdigit(de
->d_name
[i
]))
122 sprintf(psbuf
, "/proc/%s/stat", de
->d_name
);
124 h
= open(psbuf
, O_RDONLY
);
129 l
= read(h
, psbuf
, 255);
131 perror("Unable to read status");
139 ext
= strrchr(psbuf
, ')');
147 ext
= strchr(ext
, ' ')+1;
150 ext
= strchr(ext
, ' ')+1;
153 ext
= strchr(ext
, ' ')+1;
156 ext
= strchr(ext
, ' ')+1;
158 //printf("1|%s\n", ext);
160 ext
= strchr(ext
, ' ')+1;
162 //printf("2|%s\n", ext);
164 ext
= strchr(ext
, ' ')+1;
166 //printf("3|%s\n", ext);
168 ext
= strchr(ext
, ' ')+1;
170 //printf("4|%s\n", ext);
172 ext
= strchr(ext
, ' ')+1;
174 //printf("5|%s\n", ext);
176 ext
= strchr(ext
, ' ')+1;
178 //printf("6|%s\n", ext);
180 ext
= strchr(ext
, ' ')+1;
182 //printf("7|%s\n", ext);
184 ext
= strchr(ext
, ' ')+1;
186 //printf("8|%s\n", ext);
188 ext
= strchr(ext
, ' ')+1;
190 //printf("9|%s\n", ext);
192 ext
= strchr(ext
, ' ')+1;
194 //printf("10|%s\n", ext);
196 ext
= strchr(ext
, ' ')+1;
199 ext
= strchr(ext
, ' ')+1;
202 ext
= strchr(ext
, ' ')+1;
205 ext
= strchr(ext
, ' ')+1;
208 ext
= strchr(ext
, ' ')+1;
210 start_time
= atoi(ext
);
212 ext
= strchr(psbuf
, '(');
223 dev_to_name(tty
, tty_name
);
227 sprintf(psbuf
, "/proc/%s/status", de
->d_name
);
229 f
= fopen(psbuf
, "r");
232 while (fgets(psbuf
, 250, f
)) {
233 if (strncmp(psbuf
, "Mem:", 4) == 0) {
234 bytes
= atol(psbuf
+5);
236 } else if (strncmp(psbuf
, "Shared:", 7) == 0) {
237 sbytes
= atol(psbuf
+8);
239 } else if (strncmp(psbuf
, "VmSize:", 7) == 0) {
240 bytes
= atol(psbuf
+8);
247 seconds
= ((uptime_secs
* (long)HZ
) - start_time
) / HZ
;
249 /*printf("seconds=%s\n", gcvt(seconds, 15, psbuf));*/
251 start
= time_now
- seconds
;
256 gcvt(start, 15, psbuf);
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));*/
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
);
279 perror("Unable to open cmdline");
283 l
= read(h
, psbuf
, 255);
285 perror("Unable to read cmdline");
293 * the args are NUL separated, substitute spaces instead
297 while(psbuf
[i
] == '\0')
298 i
--; /* Don't bother with trailing NULs */
300 if (psbuf
[i
] == '\0')
303 printf("%5d %4s %c %4ldK %3ldK %2u.%u %s\n", pid
, tty_name
, state
,
305 pcpu
/ 10, pcpu
% 10,
306 /*(int)seconds / 60, (int)seconds % 60,*/