summaryrefslogtreecommitdiff
path: root/package/sash/src/ps.c
diff options
context:
space:
mode:
Diffstat (limited to 'package/sash/src/ps.c')
-rw-r--r--package/sash/src/ps.c93
1 files changed, 46 insertions, 47 deletions
diff --git a/package/sash/src/ps.c b/package/sash/src/ps.c
index cc31d2398..610650ec6 100644
--- a/package/sash/src/ps.c
+++ b/package/sash/src/ps.c
@@ -75,20 +75,20 @@ do_ps(argc, argv)
#if 0
fclose(stdin);
-#endif
+#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);
+ close(h);
if (l<=0) {
@@ -99,56 +99,56 @@ do_ps(argc, argv)
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;
@@ -158,7 +158,7 @@ do_ps(argc, argv)
//printf("1|%s\n", ext);
//tpgid
ext = strchr(ext, ' ')+1;
-
+
//printf("2|%s\n", ext);
//flags
ext = strchr(ext, ' ')+1;
@@ -186,7 +186,7 @@ do_ps(argc, argv)
//printf("8|%s\n", ext);
stime = atoi(ext);
ext = strchr(ext, ' ')+1;
-
+
//printf("9|%s\n", ext);
//cutime
ext = strchr(ext, ' ')+1;
@@ -194,13 +194,13 @@ do_ps(argc, argv)
//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;
@@ -208,26 +208,26 @@ do_ps(argc, argv)
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) {
@@ -242,53 +242,53 @@ do_ps(argc, argv)
}
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 ?
+ pcpu = seconds ?
(total_time * 10.0f * 100.0f / (float)HZ) / seconds :
- 0;
+ 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
*/
@@ -301,14 +301,13 @@ do_ps(argc, argv)
psbuf[i] = ' ';
printf("%5d %4s %c %4ldK %3ldK %2u.%u %s\n", pid, tty_name, state,
- bytes, sbytes,
- pcpu / 10, pcpu % 10,
+ bytes, sbytes,
+ pcpu / 10, pcpu % 10,
/*(int)seconds / 60, (int)seconds % 60,*/
l ? psbuf : name);
next:
;
}
-
+
closedir(d);
}
-