diff options
Diffstat (limited to 'ps.c')
-rw-r--r-- | ps.c | 53 |
1 files changed, 43 insertions, 10 deletions
@@ -35,6 +35,8 @@ | |||
35 | #include <errno.h> | 35 | #include <errno.h> |
36 | #include <fcntl.h> | 36 | #include <fcntl.h> |
37 | #include <ctype.h> | 37 | #include <ctype.h> |
38 | #include <sys/ioctl.h> | ||
39 | |||
38 | 40 | ||
39 | #if ! defined BB_FEATURE_USE_DEVPS_N_DEVMTAB | 41 | #if ! defined BB_FEATURE_USE_DEVPS_N_DEVMTAB |
40 | 42 | ||
@@ -116,7 +118,15 @@ extern int ps_main(int argc, char **argv) | |||
116 | char path[32], sbuf[512]; | 118 | char path[32], sbuf[512]; |
117 | char uidName[10] = ""; | 119 | char uidName[10] = ""; |
118 | char groupName[10] = ""; | 120 | char groupName[10] = ""; |
119 | int i, c; | 121 | int len, i, c; |
122 | #ifdef BB_FEATURE_AUTOWIDTH | ||
123 | struct winsize win = { 0, 0 }; | ||
124 | int terminal_width = 0; | ||
125 | #else | ||
126 | #define terminal_width 79 | ||
127 | #endif | ||
128 | |||
129 | |||
120 | 130 | ||
121 | if (argc > 1 && **(argv + 1) == '-') | 131 | if (argc > 1 && **(argv + 1) == '-') |
122 | usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n"); | 132 | usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n"); |
@@ -125,6 +135,12 @@ extern int ps_main(int argc, char **argv) | |||
125 | if (!dir) | 135 | if (!dir) |
126 | fatalError("Can't open /proc"); | 136 | fatalError("Can't open /proc"); |
127 | 137 | ||
138 | #ifdef BB_FEATURE_AUTOWIDTH | ||
139 | ioctl(fileno(stdout), TIOCGWINSZ, &win); | ||
140 | if (win.ws_col > 0) | ||
141 | terminal_width = win.ws_col - 1; | ||
142 | #endif | ||
143 | |||
128 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", | 144 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", |
129 | "State", "Command"); | 145 | "State", "Command"); |
130 | while ((entry = readdir(dir)) != NULL) { | 146 | while ((entry = readdir(dir)) != NULL) { |
@@ -146,21 +162,21 @@ extern int ps_main(int argc, char **argv) | |||
146 | if (*groupName == '\0') | 162 | if (*groupName == '\0') |
147 | sprintf(groupName, "%d", p.rgid); | 163 | sprintf(groupName, "%d", p.rgid); |
148 | 164 | ||
149 | fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, | 165 | len = fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, |
150 | p.state); | 166 | p.state); |
151 | sprintf(path, "/proc/%s/cmdline", entry->d_name); | 167 | sprintf(path, "/proc/%s/cmdline", entry->d_name); |
152 | file = fopen(path, "r"); | 168 | file = fopen(path, "r"); |
153 | if (file == NULL) | 169 | if (file == NULL) |
154 | fatalError("Can't open %s: %s\n", path, strerror(errno)); | 170 | fatalError("Can't open %s: %s\n", path, strerror(errno)); |
155 | i = 0; | 171 | i = 0; |
156 | while (((c = getc(file)) != EOF) && (i < 53)) { | 172 | while (((c = getc(file)) != EOF) && (i < (terminal_width-len))) { |
157 | i++; | 173 | i++; |
158 | if (c == '\0') | 174 | if (c == '\0') |
159 | c = ' '; | 175 | c = ' '; |
160 | putc(c, stdout); | 176 | putc(c, stdout); |
161 | } | 177 | } |
162 | if (i == 0) | 178 | if (i == 0) |
163 | fprintf(stdout, "%s", p.cmd); | 179 | fprintf(stdout, "[%s]", p.cmd); |
164 | fprintf(stdout, "\n"); | 180 | fprintf(stdout, "\n"); |
165 | } | 181 | } |
166 | closedir(dir); | 182 | closedir(dir); |
@@ -175,19 +191,24 @@ extern int ps_main(int argc, char **argv) | |||
175 | * this one uses the nifty new devps kernel device. | 191 | * this one uses the nifty new devps kernel device. |
176 | */ | 192 | */ |
177 | 193 | ||
178 | #include <sys/ioctl.h> | ||
179 | #include <linux/devps.h> | 194 | #include <linux/devps.h> |
180 | 195 | ||
181 | 196 | ||
182 | extern int ps_main(int argc, char **argv) | 197 | extern int ps_main(int argc, char **argv) |
183 | { | 198 | { |
184 | char device[] = "/dev/ps"; | 199 | char device[] = "/dev/ps"; |
185 | int i, fd; | 200 | int i, j, len, fd; |
186 | pid_t num_pids; | 201 | pid_t num_pids; |
187 | pid_t* pid_array = NULL; | 202 | pid_t* pid_array = NULL; |
188 | struct pid_info info; | 203 | struct pid_info info; |
189 | char uidName[10] = ""; | 204 | char uidName[10] = ""; |
190 | char groupName[10] = ""; | 205 | char groupName[10] = ""; |
206 | #ifdef BB_FEATURE_AUTOWIDTH | ||
207 | struct winsize win = { 0, 0 }; | ||
208 | int terminal_width = 0; | ||
209 | #else | ||
210 | #define terminal_width 79 | ||
211 | #endif | ||
191 | 212 | ||
192 | if (argc > 1 && **(argv + 1) == '-') | 213 | if (argc > 1 && **(argv + 1) == '-') |
193 | usage("ps-devps\n\nReport process status\n\nThis version of ps accepts no options.\n\n"); | 214 | usage("ps-devps\n\nReport process status\n\nThis version of ps accepts no options.\n\n"); |
@@ -212,6 +233,12 @@ extern int ps_main(int argc, char **argv) | |||
212 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) | 233 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) |
213 | fatalError("\nDEVPS_GET_PID_LIST: %s\n", strerror (errno)); | 234 | fatalError("\nDEVPS_GET_PID_LIST: %s\n", strerror (errno)); |
214 | 235 | ||
236 | #ifdef BB_FEATURE_AUTOWIDTH | ||
237 | ioctl(fileno(stdout), TIOCGWINSZ, &win); | ||
238 | if (win.ws_col > 0) | ||
239 | terminal_width = win.ws_col - 1; | ||
240 | #endif | ||
241 | |||
215 | /* Print up a ps listing */ | 242 | /* Print up a ps listing */ |
216 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", | 243 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", |
217 | "State", "Command"); | 244 | "State", "Command"); |
@@ -232,13 +259,19 @@ extern int ps_main(int argc, char **argv) | |||
232 | if (*groupName == '\0') | 259 | if (*groupName == '\0') |
233 | sprintf(groupName, "%ld", info.egid); | 260 | sprintf(groupName, "%ld", info.egid); |
234 | 261 | ||
235 | fprintf(stdout, "%5d %-8s %-8s %c ", info.pid, uidName, groupName, info.state); | 262 | len = fprintf(stdout, "%5d %-8s %-8s %c ", info.pid, uidName, groupName, info.state); |
236 | 263 | ||
237 | if (strlen(info.command_line) > 1) | 264 | if (strlen(info.command_line) > 1) { |
265 | for( j=0; j<(sizeof(info.command_line)-1) && j < (terminal_width-len); j++) { | ||
266 | if (*(info.command_line+j) == '\0' && *(info.command_line+j+1) != '\0') { | ||
267 | *(info.command_line+j) = ' '; | ||
268 | } | ||
269 | } | ||
270 | *(info.command_line+j) = '\0'; | ||
238 | fprintf(stdout, "%s\n", info.command_line); | 271 | fprintf(stdout, "%s\n", info.command_line); |
239 | else | 272 | } else { |
240 | fprintf(stdout, "[%s]\n", info.name); | 273 | fprintf(stdout, "[%s]\n", info.name); |
241 | 274 | } | |
242 | } | 275 | } |
243 | 276 | ||
244 | /* Free memory */ | 277 | /* Free memory */ |