ps.c revision 1.39 1 1.39 simonb /* $NetBSD: ps.c,v 1.39 2000/06/07 04:58:01 simonb Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.11 cgd * Copyright (c) 1990, 1993, 1994
5 1.11 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.19 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.19 christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
39 1.19 christos The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.13 cgd #if 0
44 1.11 cgd static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
45 1.13 cgd #else
46 1.39 simonb __RCSID("$NetBSD: ps.c,v 1.39 2000/06/07 04:58:01 simonb Exp $");
47 1.13 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/user.h>
52 1.1 cgd #include <sys/time.h>
53 1.1 cgd #include <sys/resource.h>
54 1.1 cgd #include <sys/proc.h>
55 1.1 cgd #include <sys/stat.h>
56 1.1 cgd #include <sys/ioctl.h>
57 1.11 cgd #include <sys/sysctl.h>
58 1.11 cgd
59 1.11 cgd #include <ctype.h>
60 1.11 cgd #include <err.h>
61 1.11 cgd #include <errno.h>
62 1.11 cgd #include <fcntl.h>
63 1.11 cgd #include <kvm.h>
64 1.17 pk #include <limits.h>
65 1.1 cgd #include <nlist.h>
66 1.11 cgd #include <paths.h>
67 1.25 mycroft #include <pwd.h>
68 1.1 cgd #include <stdio.h>
69 1.1 cgd #include <stdlib.h>
70 1.1 cgd #include <string.h>
71 1.11 cgd #include <unistd.h>
72 1.11 cgd
73 1.1 cgd #include "ps.h"
74 1.1 cgd
75 1.38 simonb struct kinfo_proc2 *kinfo;
76 1.1 cgd struct varent *vhead, *vtail;
77 1.1 cgd
78 1.1 cgd int eval; /* exit value */
79 1.1 cgd int rawcpu; /* -C */
80 1.1 cgd int sumrusage; /* -S */
81 1.39 simonb int dontuseprocfs; /* -K */
82 1.1 cgd int termwidth; /* width of screen (0 == infinity) */
83 1.1 cgd int totwidth; /* calculated width of requested variables */
84 1.1 cgd
85 1.38 simonb int needcomm, needenv, commandonly, use_procfs;
86 1.33 simonb uid_t myuid;
87 1.1 cgd
88 1.1 cgd enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
89 1.1 cgd
90 1.38 simonb static struct kinfo_proc2
91 1.38 simonb *getkinfo_kvm __P((kvm_t *, int, int, int *));
92 1.11 cgd static char *kludge_oldps_options __P((char *));
93 1.11 cgd static int pscomp __P((const void *, const void *));
94 1.11 cgd static void scanvars __P((void));
95 1.11 cgd static void usage __P((void));
96 1.19 christos int main __P((int, char *[]));
97 1.1 cgd
98 1.1 cgd char dfmt[] = "pid tt state time command";
99 1.1 cgd char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
100 1.1 cgd char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
101 1.1 cgd char o1[] = "pid";
102 1.1 cgd char o2[] = "tt state time command";
103 1.1 cgd char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
104 1.11 cgd char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
105 1.11 cgd
106 1.11 cgd kvm_t *kd;
107 1.1 cgd
108 1.11 cgd int
109 1.1 cgd main(argc, argv)
110 1.1 cgd int argc;
111 1.11 cgd char *argv[];
112 1.1 cgd {
113 1.11 cgd struct varent *vent;
114 1.1 cgd struct winsize ws;
115 1.25 mycroft int ch, flag, i, fmt, lineno, nentries;
116 1.39 simonb int prtheader, wflag, what, xflg, mode;
117 1.17 pk char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
118 1.26 kim char *ttname;
119 1.1 cgd
120 1.38 simonb if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
121 1.38 simonb ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
122 1.38 simonb ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
123 1.1 cgd ws.ws_col == 0)
124 1.1 cgd termwidth = 79;
125 1.1 cgd else
126 1.1 cgd termwidth = ws.ws_col - 1;
127 1.1 cgd
128 1.1 cgd if (argc > 1)
129 1.1 cgd argv[1] = kludge_oldps_options(argv[1]);
130 1.1 cgd
131 1.25 mycroft fmt = prtheader = wflag = xflg = 0;
132 1.25 mycroft what = KERN_PROC_UID;
133 1.33 simonb flag = myuid = getuid();
134 1.1 cgd memf = nlistf = swapf = NULL;
135 1.39 simonb mode = PRINTMODE;
136 1.1 cgd while ((ch = getopt(argc, argv,
137 1.28 bgrayson "acCeghjKLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1)
138 1.1 cgd switch((char)ch) {
139 1.1 cgd case 'a':
140 1.25 mycroft what = KERN_PROC_ALL;
141 1.25 mycroft flag = 0;
142 1.1 cgd break;
143 1.12 mycroft case 'c':
144 1.12 mycroft commandonly = 1;
145 1.12 mycroft break;
146 1.11 cgd case 'e': /* XXX set ufmt */
147 1.11 cgd needenv = 1;
148 1.11 cgd break;
149 1.1 cgd case 'C':
150 1.1 cgd rawcpu = 1;
151 1.1 cgd break;
152 1.1 cgd case 'g':
153 1.11 cgd break; /* no-op */
154 1.1 cgd case 'h':
155 1.1 cgd prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
156 1.1 cgd break;
157 1.1 cgd case 'j':
158 1.1 cgd parsefmt(jfmt);
159 1.15 mycroft fmt = 1;
160 1.1 cgd jfmt[0] = '\0';
161 1.1 cgd break;
162 1.28 bgrayson case 'K':
163 1.28 bgrayson dontuseprocfs=1;
164 1.28 bgrayson break;
165 1.11 cgd case 'L':
166 1.1 cgd showkey();
167 1.1 cgd exit(0);
168 1.23 mycroft /* NOTREACHED */
169 1.1 cgd case 'l':
170 1.1 cgd parsefmt(lfmt);
171 1.15 mycroft fmt = 1;
172 1.1 cgd lfmt[0] = '\0';
173 1.1 cgd break;
174 1.1 cgd case 'M':
175 1.1 cgd memf = optarg;
176 1.35 chs dontuseprocfs = 1;
177 1.1 cgd break;
178 1.1 cgd case 'm':
179 1.1 cgd sortby = SORTMEM;
180 1.1 cgd break;
181 1.1 cgd case 'N':
182 1.1 cgd nlistf = optarg;
183 1.1 cgd break;
184 1.1 cgd case 'O':
185 1.1 cgd parsefmt(o1);
186 1.1 cgd parsefmt(optarg);
187 1.1 cgd parsefmt(o2);
188 1.1 cgd o1[0] = o2[0] = '\0';
189 1.15 mycroft fmt = 1;
190 1.1 cgd break;
191 1.1 cgd case 'o':
192 1.1 cgd parsefmt(optarg);
193 1.15 mycroft fmt = 1;
194 1.1 cgd break;
195 1.1 cgd case 'p':
196 1.25 mycroft what = KERN_PROC_PID;
197 1.25 mycroft flag = atol(optarg);
198 1.1 cgd xflg = 1;
199 1.1 cgd break;
200 1.1 cgd case 'r':
201 1.1 cgd sortby = SORTCPU;
202 1.1 cgd break;
203 1.1 cgd case 'S':
204 1.1 cgd sumrusage = 1;
205 1.1 cgd break;
206 1.1 cgd case 'T':
207 1.22 mycroft if ((ttname = ttyname(STDIN_FILENO)) == NULL)
208 1.11 cgd errx(1, "stdin: not a terminal");
209 1.22 mycroft goto tty;
210 1.22 mycroft case 't':
211 1.22 mycroft ttname = optarg;
212 1.22 mycroft tty: {
213 1.11 cgd struct stat sb;
214 1.11 cgd char *ttypath, pathbuf[MAXPATHLEN];
215 1.1 cgd
216 1.36 simonb flag = 0;
217 1.36 simonb if (strcmp(ttname, "?") == 0)
218 1.36 simonb flag = KERN_PROC_TTY_NODEV;
219 1.36 simonb else if (strcmp(ttname, "-") == 0)
220 1.36 simonb flag = KERN_PROC_TTY_REVOKE;
221 1.36 simonb else if (strcmp(ttname, "co") == 0)
222 1.1 cgd ttypath = _PATH_CONSOLE;
223 1.26 kim else if (*ttname != '/')
224 1.11 cgd (void)snprintf(ttypath = pathbuf,
225 1.26 kim sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
226 1.1 cgd else
227 1.26 kim ttypath = ttname;
228 1.25 mycroft what = KERN_PROC_TTY;
229 1.36 simonb if (flag == 0) {
230 1.36 simonb if (stat(ttypath, &sb) == -1)
231 1.36 simonb err(1, "%s", ttypath);
232 1.36 simonb if (!S_ISCHR(sb.st_mode))
233 1.36 simonb errx(1, "%s: not a terminal", ttypath);
234 1.36 simonb flag = sb.st_rdev;
235 1.36 simonb }
236 1.1 cgd break;
237 1.1 cgd }
238 1.25 mycroft case 'U':
239 1.25 mycroft if (*optarg != '\0') {
240 1.25 mycroft struct passwd *pw;
241 1.25 mycroft char *ep;
242 1.25 mycroft
243 1.25 mycroft what = KERN_PROC_UID;
244 1.25 mycroft pw = getpwnam(optarg);
245 1.25 mycroft if (pw == NULL) {
246 1.25 mycroft errno = 0;
247 1.25 mycroft flag = strtoul(optarg, &ep, 10);
248 1.25 mycroft if (errno)
249 1.25 mycroft err(1, "%s", optarg);
250 1.25 mycroft if (*ep != '\0')
251 1.25 mycroft errx(1, "%s: illegal user name",
252 1.25 mycroft optarg);
253 1.25 mycroft } else
254 1.25 mycroft flag = pw->pw_uid;
255 1.25 mycroft }
256 1.25 mycroft break;
257 1.1 cgd case 'u':
258 1.1 cgd parsefmt(ufmt);
259 1.1 cgd sortby = SORTCPU;
260 1.15 mycroft fmt = 1;
261 1.1 cgd ufmt[0] = '\0';
262 1.1 cgd break;
263 1.1 cgd case 'v':
264 1.1 cgd parsefmt(vfmt);
265 1.1 cgd sortby = SORTMEM;
266 1.15 mycroft fmt = 1;
267 1.1 cgd vfmt[0] = '\0';
268 1.1 cgd break;
269 1.1 cgd case 'W':
270 1.1 cgd swapf = optarg;
271 1.1 cgd break;
272 1.1 cgd case 'w':
273 1.6 cgd if (wflag)
274 1.6 cgd termwidth = UNLIMITED;
275 1.6 cgd else if (termwidth < 131)
276 1.1 cgd termwidth = 131;
277 1.11 cgd wflag++;
278 1.1 cgd break;
279 1.1 cgd case 'x':
280 1.1 cgd xflg = 1;
281 1.1 cgd break;
282 1.1 cgd case '?':
283 1.1 cgd default:
284 1.1 cgd usage();
285 1.1 cgd }
286 1.1 cgd argc -= optind;
287 1.1 cgd argv += optind;
288 1.1 cgd
289 1.1 cgd #define BACKWARD_COMPATIBILITY
290 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
291 1.1 cgd if (*argv) {
292 1.1 cgd nlistf = *argv;
293 1.1 cgd if (*++argv) {
294 1.1 cgd memf = *argv;
295 1.1 cgd if (*++argv)
296 1.1 cgd swapf = *argv;
297 1.1 cgd }
298 1.1 cgd }
299 1.1 cgd #endif
300 1.11 cgd
301 1.38 simonb if (nlistf == NULL && memf == NULL && swapf == NULL) {
302 1.38 simonb kd = kvm_openfiles(nlistf, memf, swapf, KVM_NO_FILES, errbuf);
303 1.38 simonb donlist_sysctl();
304 1.38 simonb } else
305 1.38 simonb kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
306 1.38 simonb
307 1.29 jdolecek if (kd == 0) {
308 1.29 jdolecek if (dontuseprocfs)
309 1.29 jdolecek errx(1, "%s", errbuf);
310 1.29 jdolecek else {
311 1.29 jdolecek warnx("kvm_openfiles: %s", errbuf);
312 1.29 jdolecek fprintf(stderr, "ps: falling back to /proc-based lookup\n");
313 1.29 jdolecek }
314 1.29 jdolecek }
315 1.21 mrg
316 1.15 mycroft if (!fmt)
317 1.1 cgd parsefmt(dfmt);
318 1.1 cgd
319 1.1 cgd /*
320 1.39 simonb * scan requested variables, noting what structures are needed.
321 1.1 cgd */
322 1.1 cgd scanvars();
323 1.29 jdolecek
324 1.1 cgd /*
325 1.1 cgd * select procs
326 1.1 cgd */
327 1.38 simonb if (!kd || !(kinfo = getkinfo_kvm(kd, what, flag, &nentries)))
328 1.27 bgrayson {
329 1.28 bgrayson /* If/when the /proc-based code is ripped out
330 1.28 bgrayson * again, make sure all references to the -K
331 1.28 bgrayson * option are also pulled (getopt(), usage(),
332 1.28 bgrayson * man page). See the man page comments about
333 1.28 bgrayson * this for more details. */
334 1.29 jdolecek if (kd)
335 1.29 jdolecek warnx("%s.", kvm_geterr(kd));
336 1.38 simonb if (dontuseprocfs)
337 1.28 bgrayson exit(1);
338 1.38 simonb
339 1.27 bgrayson /* procfs_getprocs supports all but the
340 1.27 bgrayson * KERN_PROC_RUID flag. */
341 1.29 jdolecek kinfo = getkinfo_procfs(what, flag, &nentries);
342 1.38 simonb if (kinfo == 0)
343 1.38 simonb errx(1, "fallback /proc-based lookup also failed. %s",
344 1.38 simonb "Giving up...");
345 1.28 bgrayson fprintf(stderr, "%s%s",
346 1.28 bgrayson "Warning: /proc does not provide ",
347 1.28 bgrayson "valid data for all fields.\n");
348 1.31 jdolecek use_procfs = 1;
349 1.27 bgrayson }
350 1.39 simonb if (nentries == 0) {
351 1.39 simonb printheader();
352 1.1 cgd exit(0);
353 1.39 simonb }
354 1.1 cgd /*
355 1.1 cgd * sort proc list
356 1.1 cgd */
357 1.38 simonb qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp);
358 1.1 cgd /*
359 1.39 simonb * For each proc, call each variable output function in
360 1.39 simonb * "setwidth" mode to determine the widest element of
361 1.39 simonb * the column.
362 1.39 simonb */
363 1.39 simonb if (mode == PRINTMODE)
364 1.39 simonb for (i = 0; i < nentries; i++) {
365 1.39 simonb struct kinfo_proc2 *ki = &kinfo[i];
366 1.39 simonb
367 1.39 simonb if (xflg == 0 && (ki->p_tdev == NODEV ||
368 1.39 simonb (ki->p_flag & P_CONTROLT) == 0))
369 1.39 simonb continue;
370 1.39 simonb for (vent = vhead; vent; vent = vent->next)
371 1.39 simonb (vent->var->oproc)(ki, vent, WIDTHMODE);
372 1.39 simonb }
373 1.39 simonb /*
374 1.39 simonb * Print header - AFTER determining process field widths.
375 1.39 simonb * printheader() also adds up the total width of all
376 1.39 simonb * fields the first time it's called.
377 1.39 simonb */
378 1.39 simonb printheader();
379 1.39 simonb /*
380 1.39 simonb * For each proc, call each variable output function in
381 1.39 simonb * print mode.
382 1.1 cgd */
383 1.1 cgd for (i = lineno = 0; i < nentries; i++) {
384 1.38 simonb struct kinfo_proc2 *ki = &kinfo[i];
385 1.14 mycroft
386 1.38 simonb if (xflg == 0 && (ki->p_tdev == NODEV ||
387 1.38 simonb (ki->p_flag & P_CONTROLT ) == 0))
388 1.1 cgd continue;
389 1.1 cgd for (vent = vhead; vent; vent = vent->next) {
390 1.39 simonb (vent->var->oproc)(ki, vent, mode);
391 1.1 cgd if (vent->next != NULL)
392 1.11 cgd (void)putchar(' ');
393 1.1 cgd }
394 1.11 cgd (void)putchar('\n');
395 1.11 cgd if (prtheader && lineno++ == prtheader - 4) {
396 1.11 cgd (void)putchar('\n');
397 1.1 cgd printheader();
398 1.1 cgd lineno = 0;
399 1.1 cgd }
400 1.1 cgd }
401 1.1 cgd exit(eval);
402 1.23 mycroft /* NOTREACHED */
403 1.1 cgd }
404 1.1 cgd
405 1.38 simonb static struct kinfo_proc2 *
406 1.38 simonb getkinfo_kvm(kd, what, flag, nentriesp)
407 1.29 jdolecek kvm_t *kd;
408 1.38 simonb int what, flag, *nentriesp;
409 1.29 jdolecek {
410 1.38 simonb return (kvm_getproc2(kd, what, flag, sizeof(struct kinfo_proc2),
411 1.38 simonb nentriesp));
412 1.29 jdolecek }
413 1.29 jdolecek
414 1.11 cgd static void
415 1.1 cgd scanvars()
416 1.1 cgd {
417 1.11 cgd struct varent *vent;
418 1.11 cgd VAR *v;
419 1.1 cgd
420 1.1 cgd for (vent = vhead; vent; vent = vent->next) {
421 1.1 cgd v = vent->var;
422 1.1 cgd if (v->flag & COMM)
423 1.1 cgd needcomm = 1;
424 1.1 cgd }
425 1.11 cgd }
426 1.1 cgd
427 1.11 cgd static int
428 1.11 cgd pscomp(a, b)
429 1.11 cgd const void *a, *b;
430 1.1 cgd {
431 1.38 simonb struct kinfo_proc2 *ka = (struct kinfo_proc2 *)a;
432 1.38 simonb struct kinfo_proc2 *kb = (struct kinfo_proc2 *)b;
433 1.38 simonb
434 1.1 cgd int i;
435 1.38 simonb #define VSIZE(k) (k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize)
436 1.1 cgd
437 1.1 cgd if (sortby == SORTCPU)
438 1.38 simonb return (getpcpu(kb) - getpcpu(ka));
439 1.1 cgd if (sortby == SORTMEM)
440 1.38 simonb return (VSIZE(kb) - VSIZE(ka));
441 1.38 simonb i = ka->p_tdev - kb->p_tdev;
442 1.38 simonb if (i == 0)
443 1.38 simonb i = ka->p_pid - kb->p_pid;
444 1.38 simonb
445 1.1 cgd if (i == 0)
446 1.38 simonb i = ka->p_pid - kb->p_pid;
447 1.1 cgd return (i);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd /*
451 1.1 cgd * ICK (all for getopt), would rather hide the ugliness
452 1.1 cgd * here than taint the main code.
453 1.1 cgd *
454 1.1 cgd * ps foo -> ps -foo
455 1.1 cgd * ps 34 -> ps -p34
456 1.1 cgd *
457 1.25 mycroft * The old convention that 't' with no trailing tty arg means the user's
458 1.1 cgd * tty, is only supported if argv[1] doesn't begin with a '-'. This same
459 1.1 cgd * feature is available with the option 'T', which takes no argument.
460 1.1 cgd */
461 1.11 cgd static char *
462 1.1 cgd kludge_oldps_options(s)
463 1.1 cgd char *s;
464 1.1 cgd {
465 1.1 cgd size_t len;
466 1.1 cgd char *newopts, *ns, *cp;
467 1.1 cgd
468 1.1 cgd len = strlen(s);
469 1.16 thorpej if ((newopts = ns = malloc(len + 3)) == NULL)
470 1.32 drochner err(1, NULL);
471 1.1 cgd /*
472 1.1 cgd * options begin with '-'
473 1.1 cgd */
474 1.1 cgd if (*s != '-')
475 1.1 cgd *ns++ = '-'; /* add option flag */
476 1.1 cgd /*
477 1.1 cgd * gaze to end of argv[1]
478 1.1 cgd */
479 1.1 cgd cp = s + len - 1;
480 1.1 cgd /*
481 1.1 cgd * if last letter is a 't' flag with no argument (in the context
482 1.1 cgd * of the oldps options -- option string NOT starting with a '-' --
483 1.1 cgd * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
484 1.1 cgd */
485 1.1 cgd if (*cp == 't' && *s != '-')
486 1.1 cgd *cp = 'T';
487 1.1 cgd else {
488 1.1 cgd /*
489 1.1 cgd * otherwise check for trailing number, which *may* be a
490 1.1 cgd * pid.
491 1.1 cgd */
492 1.1 cgd while (cp >= s && isdigit(*cp))
493 1.1 cgd --cp;
494 1.1 cgd }
495 1.1 cgd cp++;
496 1.11 cgd memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
497 1.1 cgd ns += cp - s;
498 1.1 cgd /*
499 1.1 cgd * if there's a trailing number, and not a preceding 'p' (pid) or
500 1.1 cgd * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
501 1.1 cgd */
502 1.25 mycroft if (isdigit(*cp) &&
503 1.25 mycroft (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
504 1.25 mycroft (cp - 1 == s || cp[-2] != 't'))))
505 1.1 cgd *ns++ = 'p';
506 1.18 mrg /* and append the number */
507 1.18 mrg (void)strcpy(ns, cp); /* XXX strcpy is safe */
508 1.1 cgd
509 1.1 cgd return (newopts);
510 1.1 cgd }
511 1.1 cgd
512 1.11 cgd static void
513 1.11 cgd usage()
514 1.1 cgd {
515 1.1 cgd
516 1.11 cgd (void)fprintf(stderr,
517 1.11 cgd "usage:\t%s\n\t %s\n\t%s\n",
518 1.28 bgrayson "ps [-aChjKlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
519 1.34 hubertf "[-M core] [-N system] [-W swap] [-U username]",
520 1.11 cgd "ps [-L]");
521 1.1 cgd exit(1);
522 1.23 mycroft /* NOTREACHED */
523 1.1 cgd }
524