diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /ps.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'ps.c')
-rw-r--r-- | ps.c | 206 |
1 files changed, 106 insertions, 100 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini ps implementation for busybox | 3 | * Mini ps implementation for busybox |
3 | * | 4 | * |
@@ -33,123 +34,128 @@ | |||
33 | #endif | 34 | #endif |
34 | 35 | ||
35 | typedef struct proc_s { | 36 | typedef struct proc_s { |
36 | char | 37 | char |
37 | cmd[16]; /* basename of executable file in call to exec(2) */ | 38 | cmd[16]; /* basename of executable file in call to exec(2) */ |
38 | int | 39 | int |
39 | ruid, rgid, /* real only (sorry) */ | 40 | ruid, rgid, /* real only (sorry) */ |
40 | pid, /* process id */ | 41 | pid, /* process id */ |
41 | ppid; /* pid of parent process */ | 42 | ppid; /* pid of parent process */ |
42 | char | 43 | char |
43 | state; /* single-char code for process state (S=sleeping) */ | 44 | state; /* single-char code for process state (S=sleeping) */ |
44 | } proc_t; | 45 | } proc_t; |
45 | 46 | ||
46 | 47 | ||
47 | 48 | ||
48 | static int file2str(char *filename, char *ret, int cap) | 49 | static int file2str(char *filename, char *ret, int cap) |
49 | { | 50 | { |
50 | int fd, num_read; | 51 | int fd, num_read; |
51 | 52 | ||
52 | if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1; | 53 | if ((fd = open(filename, O_RDONLY, 0)) == -1) |
53 | if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1; | 54 | return -1; |
54 | ret[num_read] = 0; | 55 | if ((num_read = read(fd, ret, cap - 1)) <= 0) |
55 | close(fd); | 56 | return -1; |
56 | return num_read; | 57 | ret[num_read] = 0; |
58 | close(fd); | ||
59 | return num_read; | ||
57 | } | 60 | } |
58 | 61 | ||
59 | 62 | ||
60 | static void parse_proc_status(char* S, proc_t* P) | 63 | static void parse_proc_status(char *S, proc_t * P) |
61 | { | 64 | { |
62 | char* tmp; | 65 | char *tmp; |
63 | memset(P->cmd, 0, sizeof P->cmd); | 66 | |
64 | sscanf (S, "Name:\t%15c", P->cmd); | 67 | memset(P->cmd, 0, sizeof P->cmd); |
65 | tmp = strchr(P->cmd,'\n'); | 68 | sscanf(S, "Name:\t%15c", P->cmd); |
66 | if (tmp) | 69 | tmp = strchr(P->cmd, '\n'); |
67 | *tmp='\0'; | 70 | if (tmp) |
68 | tmp = strstr (S,"State"); | 71 | *tmp = '\0'; |
69 | sscanf (tmp, "State:\t%c", &P->state); | 72 | tmp = strstr(S, "State"); |
70 | 73 | sscanf(tmp, "State:\t%c", &P->state); | |
71 | tmp = strstr (S,"Pid:"); | 74 | |
72 | if(tmp) sscanf (tmp, | 75 | tmp = strstr(S, "Pid:"); |
73 | "Pid:\t%d\n" | 76 | if (tmp) |
74 | "PPid:\t%d\n", | 77 | sscanf(tmp, "Pid:\t%d\n" "PPid:\t%d\n", &P->pid, &P->ppid); |
75 | &P->pid, | 78 | else |
76 | &P->ppid | 79 | fprintf(stderr, "Internal error!\n"); |
77 | ); | 80 | |
78 | else fprintf(stderr, "Internal error!\n"); | 81 | /* For busybox, ignoring effective, saved, etc */ |
79 | 82 | tmp = strstr(S, "Uid:"); | |
80 | /* For busybox, ignoring effective, saved, etc */ | 83 | if (tmp) |
81 | tmp = strstr (S,"Uid:"); | 84 | sscanf(tmp, "Uid:\t%d", &P->ruid); |
82 | if(tmp) sscanf (tmp, | 85 | else |
83 | "Uid:\t%d", &P->ruid); | 86 | fprintf(stderr, "Internal error!\n"); |
84 | else fprintf(stderr, "Internal error!\n"); | 87 | |
85 | 88 | tmp = strstr(S, "Gid:"); | |
86 | tmp = strstr (S,"Gid:"); | 89 | if (tmp) |
87 | if(tmp) sscanf (tmp, | 90 | sscanf(tmp, "Gid:\t%d", &P->rgid); |
88 | "Gid:\t%d", &P->rgid); | 91 | else |
89 | else fprintf(stderr, "Internal error!\n"); | 92 | fprintf(stderr, "Internal error!\n"); |
90 | 93 | ||
91 | } | 94 | } |
92 | 95 | ||
93 | 96 | ||
94 | extern int ps_main(int argc, char **argv) | 97 | extern int ps_main(int argc, char **argv) |
95 | { | 98 | { |
96 | proc_t p; | 99 | proc_t p; |
97 | DIR *dir; | 100 | DIR *dir; |
98 | FILE *file; | 101 | FILE *file; |
99 | struct dirent *entry; | 102 | struct dirent *entry; |
100 | char path[32], sbuf[512]; | 103 | char path[32], sbuf[512]; |
101 | char uidName[10]=""; | 104 | char uidName[10] = ""; |
102 | char groupName[10]=""; | 105 | char groupName[10] = ""; |
103 | int i, c; | 106 | int i, c; |
104 | 107 | ||
105 | if ( argc>1 && **(argv+1) == '-' ) { | 108 | if (argc > 1 && **(argv + 1) == '-') { |
106 | usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n"); | 109 | usage |
107 | } | 110 | ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n"); |
108 | |||
109 | dir = opendir("/proc"); | ||
110 | if (!dir) { | ||
111 | perror("Can't open /proc"); | ||
112 | exit(FALSE); | ||
113 | } | ||
114 | |||
115 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command"); | ||
116 | while ((entry = readdir(dir)) != NULL) { | ||
117 | uidName[0]='\0'; | ||
118 | groupName[0]='\0'; | ||
119 | |||
120 | if (! isdigit(*entry->d_name)) | ||
121 | continue; | ||
122 | sprintf(path, "/proc/%s/status", entry->d_name); | ||
123 | if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) { | ||
124 | parse_proc_status(sbuf, &p); | ||
125 | } | 111 | } |
126 | 112 | ||
127 | /* Make some adjustments as needed */ | 113 | dir = opendir("/proc"); |
128 | my_getpwuid( uidName, p.ruid); | 114 | if (!dir) { |
129 | my_getgrgid( groupName, p.rgid); | 115 | perror("Can't open /proc"); |
130 | if (*uidName == '\0') | 116 | exit(FALSE); |
131 | sprintf( uidName, "%d", p.ruid); | ||
132 | if (*groupName == '\0') | ||
133 | sprintf( groupName, "%d", p.rgid); | ||
134 | |||
135 | fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state); | ||
136 | sprintf(path, "/proc/%s/cmdline", entry->d_name); | ||
137 | file = fopen(path, "r"); | ||
138 | if (file == NULL) { | ||
139 | perror(path); | ||
140 | exit(FALSE); | ||
141 | } | 117 | } |
142 | i=0; | 118 | |
143 | while (((c = getc(file)) != EOF) && (i < 53)) { | 119 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", |
144 | i++; | 120 | "State", "Command"); |
145 | if (c == '\0') | 121 | while ((entry = readdir(dir)) != NULL) { |
146 | c = ' '; | 122 | uidName[0] = '\0'; |
147 | putc(c, stdout); | 123 | groupName[0] = '\0'; |
124 | |||
125 | if (!isdigit(*entry->d_name)) | ||
126 | continue; | ||
127 | sprintf(path, "/proc/%s/status", entry->d_name); | ||
128 | if ((file2str(path, sbuf, sizeof sbuf)) != -1) { | ||
129 | parse_proc_status(sbuf, &p); | ||
130 | } | ||
131 | |||
132 | /* Make some adjustments as needed */ | ||
133 | my_getpwuid(uidName, p.ruid); | ||
134 | my_getgrgid(groupName, p.rgid); | ||
135 | if (*uidName == '\0') | ||
136 | sprintf(uidName, "%d", p.ruid); | ||
137 | if (*groupName == '\0') | ||
138 | sprintf(groupName, "%d", p.rgid); | ||
139 | |||
140 | fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, | ||
141 | p.state); | ||
142 | sprintf(path, "/proc/%s/cmdline", entry->d_name); | ||
143 | file = fopen(path, "r"); | ||
144 | if (file == NULL) { | ||
145 | perror(path); | ||
146 | exit(FALSE); | ||
147 | } | ||
148 | i = 0; | ||
149 | while (((c = getc(file)) != EOF) && (i < 53)) { | ||
150 | i++; | ||
151 | if (c == '\0') | ||
152 | c = ' '; | ||
153 | putc(c, stdout); | ||
154 | } | ||
155 | if (i == 0) | ||
156 | fprintf(stdout, "%s", p.cmd); | ||
157 | fprintf(stdout, "\n"); | ||
148 | } | 158 | } |
149 | if (i==0) | 159 | closedir(dir); |
150 | fprintf(stdout, "%s", p.cmd); | 160 | exit(TRUE); |
151 | fprintf(stdout, "\n"); | ||
152 | } | ||
153 | closedir(dir); | ||
154 | exit(TRUE); | ||
155 | } | 161 | } |