summaryrefslogtreecommitdiff
path: root/ps.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-20 19:18:15 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-20 19:18:15 +0000
commitd23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7 (patch)
tree0ab285d58cd8cf530ad8acac823545df892c90e9 /ps.c
parentef8b6c757de9684f5d88eff4b014527e87121137 (diff)
downloadbusybox-w32-d23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7.tar.gz
busybox-w32-d23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7.tar.bz2
busybox-w32-d23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7.zip
Made ps work. Fixed some stuff.
Diffstat (limited to 'ps.c')
-rw-r--r--ps.c118
1 files changed, 100 insertions, 18 deletions
diff --git a/ps.c b/ps.c
index b8e4cd3a0..97a5d6ba2 100644
--- a/ps.c
+++ b/ps.c
@@ -23,16 +23,82 @@
23#include <unistd.h> 23#include <unistd.h>
24#include <dirent.h> 24#include <dirent.h>
25#include <stdio.h> 25#include <stdio.h>
26#include <fcntl.h>
27#include <ctype.h>
28
29
30typedef struct proc_s {
31 char
32 cmd[16]; /* basename of executable file in call to exec(2) */
33 int
34 ruid, rgid, /* real only (sorry) */
35 pid, /* process id */
36 ppid; /* pid of parent process */
37 char
38 state; /* single-char code for process state (S=sleeping) */
39} proc_t;
40
41
42
43static int file2str(char *filename, char *ret, int cap)
44{
45 int fd, num_read;
46
47 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
48 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
49 ret[num_read] = 0;
50 close(fd);
51 return num_read;
52}
53
54
55static void parse_proc_status(char* S, proc_t* P)
56{
57 char* tmp;
58 memset(P->cmd, 0, sizeof P->cmd);
59 sscanf (S, "Name:\t%15c", P->cmd);
60 tmp = strchr(P->cmd,'\n');
61 if (tmp)
62 *tmp='\0';
63 tmp = strstr (S,"State");
64 sscanf (tmp, "State:\t%c", &P->state);
65
66 tmp = strstr (S,"Pid:");
67 if(tmp) sscanf (tmp,
68 "Pid:\t%d\n"
69 "PPid:\t%d\n",
70 &P->pid,
71 &P->ppid
72 );
73 else fprintf(stderr, "Internal error!\n");
74
75 /* For busybox, ignoring effecting, saved, etc */
76 tmp = strstr (S,"Uid:");
77 if(tmp) sscanf (tmp,
78 "Uid:\t%d", &P->ruid);
79 else fprintf(stderr, "Internal error!\n");
80
81 tmp = strstr (S,"Gid:");
82 if(tmp) sscanf (tmp,
83 "Gid:\t%d", &P->rgid);
84 else fprintf(stderr, "Internal error!\n");
85
86}
26 87
27 88
28extern int ps_main(int argc, char **argv) 89extern int ps_main(int argc, char **argv)
29{ 90{
91 proc_t p;
30 DIR *dir; 92 DIR *dir;
31 FILE *file; 93 FILE *file;
32 struct dirent *entry; 94 struct dirent *entry;
95 char path[32], sbuf[512];
96 char uidName[10]="";
97 char groupName[10]="";
98 int i, c;
33 99
34 if ( argc>1 && **(argv+1) == '-' ) { 100 if ( argc>1 && **(argv+1) == '-' ) {
35 usage ("ps\n"); 101 usage ("ps - report process status\nThis version of ps accepts no options.\n");
36 } 102 }
37 103
38 dir = opendir("/proc"); 104 dir = opendir("/proc");
@@ -41,28 +107,44 @@ extern int ps_main(int argc, char **argv)
41 exit(FALSE); 107 exit(FALSE);
42 } 108 }
43 109
44 fprintf(stdout, "PID\tUid\tGid\tState\tName\n"); 110 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
45 while ((entry = readdir(dir)) != NULL) { 111 while ((entry = readdir(dir)) != NULL) {
46 char psStatus[NAME_MAX]; 112 uidName[0]='\0';
47 char psName[NAME_MAX]=""; 113 groupName[0]='\0';
48 char psState[NAME_MAX]=""; 114
49 int psPID=0, psPPID=0, psUid=0, psGid=0; 115 if (! isdigit(*entry->d_name))
50 //if (match(entry->d_name, "[0-9]") == FALSE)
51 // continue;
52 sprintf(psStatus, "/proc/%s/status", entry->d_name);
53 file = fopen( psStatus, "r");
54 if (file == NULL) {
55 continue; 116 continue;
56 //perror(psStatus); 117 sprintf(path, "/proc/%s/status", entry->d_name);
57 //exit( FALSE); 118 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
119 parse_proc_status(sbuf, &p);
58 } 120 }
59 fscanf(file, "Name:\t%s\nState:\t%s\nPid:\t%d\nPPid:\t%d\nUid:\t%d\nGid:\t%d",
60 psName, psState, &psPID, &psPPID, &psUid, &psGid);
61 fclose(file);
62 121
63 fprintf(stdout, "%d\t%d\t%d\t%s\t%s\n", psPID, psUid, psGid, psState, psName); 122 /* Make some adjustments as needed */
123 my_getpwuid( uidName, p.ruid);
124 my_getgrgid( groupName, p.rgid);
125 if (*uidName == '\0')
126 sprintf( uidName, "%d", p.ruid);
127 if (*groupName == '\0')
128 sprintf( groupName, "%d", p.rgid);
129
130 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
131 sprintf(path, "/proc/%s/cmdline", entry->d_name);
132 file = fopen(path, "r");
133 if (file == NULL) {
134 perror(path);
135 exit(FALSE);
136 }
137 i=0;
138 while (((c = getc(file)) != EOF) && (i < 53)) {
139 i++;
140 if (c == '\0')
141 c = ' ';
142 putc(c, stdout);
143 }
144 if (i==0)
145 fprintf(stdout, "%s", p.cmd);
146 fprintf(stdout, "\n");
64 } 147 }
65 closedir(dir); 148 closedir(dir);
66 exit(TRUE); 149 exit(TRUE);
67} 150}
68