summaryrefslogtreecommitdiff
path: root/package/sash/src/ps.c
blob: ed4226cc968dfbf24510523c7a834ed528a8fddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* ps.c:
 *
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "sash.h"

#include <fcntl.h>
#include <sys/types.h>

#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <linux/major.h>
#ifdef __UC_LIBC_
#include <linux/types.h>
#endif
#include <sys/time.h>
#include <sys/param.h>
#ifdef __UC_LIBC__
#include <mathf.h>
#endif

char psbuf[256];
char name[40];
int pid, state;
char statec;
int ppid, pgrp, session;
dev_t tty;
char tty_name[10];

char master[] = "pqrstuvwxyzabcde";

#define MAJOR(x) ((x) >> 8)
#define MINOR(x) ((x) & 0xff)

int port_xlate[16] = {1, 3, 5, 7,9 ,11,13,15,
                      2, 4, 6, 8,10,12,14,16};

void dev_to_name(dev_t dev, char * ttyname)
{
	strcpy(ttyname, "");
	if (MAJOR(dev) == 75)
		sprintf(ttyname,"X%d", MINOR(dev));
	else if (MAJOR(dev) == TTY_MAJOR)
		sprintf(ttyname,"S%d", MINOR(dev)-64);
	else if (MAJOR(dev) == PTY_SLAVE_MAJOR)
		sprintf(ttyname,"%c%x", master[MINOR(dev) / 16], MINOR(dev) & 0xf);
}

void
do_ps(argc, argv)
	char	**argv;
{
	int i;
	int h;
	int max;
	FILE * f;
	DIR * d;
	unsigned long bytes, sbytes;
	struct dirent * de;
	char *ext;
	int l;
	time_t time_now;
	long uptime_secs;
	float idle_secs;
	float seconds, start, total_time;
	int utime, stime, start_time;
	int pcpu;
	/*extern int _vfprintf_fp_ref, _vfscanf_fp_ref;*/

#if 0
	fclose(stdin);
#endif 

	printf("  PID PORT STAT  SIZE SHARED %%CPU COMMAND\n"/*, _vfprintf_fp_ref, _vfscanf_fp_ref*/);

	h = open("/proc/uptime", O_RDONLY);
		
	if (h==-1) {
		perror("Unable to open /proc/uptime\n");
		return;
	}
	
	l = read(h, psbuf, 255);

	close(h);  


	if (l<=0) {
		perror("Unable to read uptime");
		return;
	}


	psbuf[l] = '\0';
	psbuf[255] = '\0';
		
	ext = psbuf;


	uptime_secs = atol(ext);

	
	time_now = time(0);
	
	d = opendir("/proc");
	if (!d)
		return;
	
	while (de = readdir(d)) {
	
	
		for(i=0;i<strlen(de->d_name);i++)
			if (!isdigit(de->d_name[i]))
				goto next;

		sprintf(psbuf, "/proc/%s/stat", de->d_name);
		
		h = open(psbuf, O_RDONLY);
		
		if (h==-1)
			continue;
			
		l = read(h, psbuf, 255);
		if (l<=0) {
			perror("Unable to read status");
			close(h);
			continue;
		}
		
		psbuf[l] = '\0';
		psbuf[255] = '\0';
		
		ext = strrchr(psbuf, ')');
		ext[0] = '\0';

		statec = ext[2];

		ext += 4;
		
		ppid = atoi(ext);
		ext = strchr(ext, ' ')+1;

		pgrp = atoi(ext);
		ext = strchr(ext, ' ')+1;
		
		session = atoi(ext);
		ext = strchr(ext, ' ')+1;

		tty = atoi(ext);
		ext = strchr(ext, ' ')+1;

		//printf("1|%s\n", ext);
		//tpgid
		ext = strchr(ext, ' ')+1;
		
		//printf("2|%s\n", ext);
		//flags
		ext = strchr(ext, ' ')+1;

		//printf("3|%s\n", ext);
		//min_flt
		ext = strchr(ext, ' ')+1;

		//printf("4|%s\n", ext);
		//cmin_flt
		ext = strchr(ext, ' ')+1;

		//printf("5|%s\n", ext);
		//maj_flt
		ext = strchr(ext, ' ')+1;

		//printf("6|%s\n", ext);
		//cmaj_flt
		ext = strchr(ext, ' ')+1;

		//printf("7|%s\n", ext);
		utime = atoi(ext);
		ext = strchr(ext, ' ')+1;

		//printf("8|%s\n", ext);
		stime = atoi(ext);
		ext = strchr(ext, ' ')+1;
		
		//printf("9|%s\n", ext);
		//cutime
		ext = strchr(ext, ' ')+1;

		//printf("10|%s\n", ext);
		//cstime
		ext = strchr(ext, ' ')+1;
		
		//priority
		ext = strchr(ext, ' ')+1;
		
		//nice
		ext = strchr(ext, ' ')+1;
		
		//timeout
		ext = strchr(ext, ' ')+1;

		//it_real_value
		ext = strchr(ext, ' ')+1;

		start_time = atoi(ext);
		
		ext = strchr(psbuf, '(');
		ext++;
		strcpy(name, ext);
		
		pid = atoi(psbuf);
		
		
		state = statec;
		
		close(h);
		
		dev_to_name(tty, tty_name);
		
		bytes = 0;
		sbytes = 0;
		sprintf(psbuf, "/proc/%s/status", de->d_name);

		f = fopen(psbuf, "r");
		
		if (f) {
			while (fgets(psbuf, 250, f)) {
				if (strncmp(psbuf, "Mem:", 4) == 0) {
					bytes = atol(psbuf+5);
					bytes /= 1024;
				} else if (strncmp(psbuf, "Shared:", 7) == 0) {
					sbytes = atol(psbuf+8);
					sbytes /= 1024;
				} else if (strncmp(psbuf, "VmSize:", 7) == 0) {
					bytes = atol(psbuf+8);
				}
			}
			fclose(f);
		}
		

		seconds = ((uptime_secs * (long)HZ) - start_time) / HZ;
		
		/*printf("seconds=%s\n", gcvt(seconds, 15, psbuf));*/
		
		start = time_now - seconds;
		
		/*
		printf("1\n");

		gcvt(start, 15, psbuf);

		printf("2\n");
		
		printf("start=%s\n", psbuf);
		
		printf("utime=%d, stime=%d. start_time=%d\n", utime, stime, start_time);
		*/
		
		total_time = (utime + stime);

		/*printf("total_time=%s\n", gcvt(total_time, 15, psbuf));*/

		pcpu = 	seconds ? 
			(total_time * 10.0f * 100.0f / (float)HZ) / seconds :
			0; 
		if (pcpu > 999) pcpu = 999;


		sprintf(psbuf, "/proc/%s/cmdline", de->d_name);
		h = open(psbuf, O_RDONLY);
		
		if (h == -1) {
			perror("Unable to open cmdline");
			continue;
		}
		
		l = read(h, psbuf, 255);
		if (l < 0) {
			perror("Unable to read cmdline");
			close(h);
			continue;
		}
		
		close(h);
		
		/*
		 * the args are NUL separated, substitute spaces instead
		 */
		psbuf[l] = '\0';
		i=l;
		while(psbuf[i] == '\0')
			i--;		/* Don't bother with trailing NULs */
		while(--i > 0)
			if (psbuf[i] == '\0')
				psbuf[i] = ' ';

		printf("%5d %4s %c    %4ldK   %3ldK %2u.%u %s\n", pid, tty_name, state,
			bytes, sbytes, 
			 pcpu / 10, pcpu % 10, 
			 /*(int)seconds / 60, (int)seconds % 60,*/
			 l ? psbuf : name);
	next:
		;
	}
	
	closedir(d);
}