ps.c revision 1.58 1 /* $NetBSD: ps.c,v 1.58 2005/06/01 15:30:33 lukem Exp $ */
2
3 /*
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1990, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 #include <sys/cdefs.h>
69 #ifndef lint
70 __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
71 The Regents of the University of California. All rights reserved.\n");
72 #endif /* not lint */
73
74 #ifndef lint
75 #if 0
76 static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
77 #else
78 __RCSID("$NetBSD: ps.c,v 1.58 2005/06/01 15:30:33 lukem Exp $");
79 #endif
80 #endif /* not lint */
81
82 #include <sys/param.h>
83 #include <sys/user.h>
84 #include <sys/time.h>
85 #include <sys/resource.h>
86 #include <sys/lwp.h>
87 #include <sys/proc.h>
88 #include <sys/stat.h>
89 #include <sys/ioctl.h>
90 #include <sys/sysctl.h>
91
92 #include <stddef.h>
93 #include <ctype.h>
94 #include <err.h>
95 #include <errno.h>
96 #include <fcntl.h>
97 #include <kvm.h>
98 #include <limits.h>
99 #include <nlist.h>
100 #include <paths.h>
101 #include <pwd.h>
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <string.h>
105 #include <unistd.h>
106
107 #include "ps.h"
108
109 /*
110 * ARGOPTS must contain all option characters that take arguments
111 * (except for 't'!) - it is used in kludge_oldps_options()
112 */
113 #define GETOPTSTR "acCeghjk:LlM:mN:O:o:p:rSsTt:U:uvW:wx"
114 #define ARGOPTS "kMNOopUW"
115
116 struct kinfo_proc2 *kinfo;
117 struct varent *vhead, *sorthead;
118
119 int eval; /* exit value */
120 int rawcpu; /* -C */
121 int sumrusage; /* -S */
122 int termwidth; /* width of screen (0 == infinity) */
123 int totwidth; /* calculated width of requested variables */
124
125 int needcomm, needenv, commandonly;
126 uid_t myuid;
127
128 static struct kinfo_lwp
129 *pick_representative_lwp(struct kinfo_proc2 *,
130 struct kinfo_lwp *, int);
131 static struct kinfo_proc2
132 *getkinfo_kvm(kvm_t *, int, int, int *);
133 static char *kludge_oldps_options(char *);
134 static int pscomp(const void *, const void *);
135 static void scanvars(void);
136 static void usage(void);
137 int main(int, char *[]);
138
139 char dfmt[] = "pid tt state time command";
140 char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
141 char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
142 char o1[] = "pid";
143 char o2[] = "tt state time command";
144 char sfmt[] = "uid pid ppid cpu lid nlwp pri nice vsz rss wchan lstate tt "
145 "time command";
146 char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
147 char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
148
149 kvm_t *kd;
150
151 int
152 main(int argc, char *argv[])
153 {
154 struct varent *vent;
155 struct winsize ws;
156 struct kinfo_lwp *kl, *l;
157 int ch, flag, i, j, fmt, lineno, nentries, nlwps;
158 int prtheader, wflag, what, xflg, mode, showlwps;
159 char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
160 char *ttname;
161
162 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
163 ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
164 ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
165 ws.ws_col == 0)
166 termwidth = 79;
167 else
168 termwidth = ws.ws_col - 1;
169
170 if (argc > 1)
171 argv[1] = kludge_oldps_options(argv[1]);
172
173 fmt = prtheader = wflag = xflg = showlwps = 0;
174 what = KERN_PROC_UID;
175 flag = myuid = getuid();
176 memf = nlistf = swapf = NULL;
177 mode = PRINTMODE;
178 while ((ch = getopt(argc, argv, GETOPTSTR)) != -1)
179 switch((char)ch) {
180 case 'a':
181 what = KERN_PROC_ALL;
182 flag = 0;
183 break;
184 case 'c':
185 commandonly = 1;
186 break;
187 case 'e': /* XXX set ufmt */
188 needenv = 1;
189 break;
190 case 'C':
191 rawcpu = 1;
192 break;
193 case 'g':
194 break; /* no-op */
195 case 'h':
196 prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
197 break;
198 case 'j':
199 parsefmt(jfmt);
200 fmt = 1;
201 jfmt[0] = '\0';
202 break;
203 case 'k':
204 parsesort(optarg);
205 break;
206 case 'K':
207 break; /* no-op - was dontuseprocfs */
208 case 'L':
209 showkey();
210 exit(0);
211 /* NOTREACHED */
212 case 'l':
213 parsefmt(lfmt);
214 fmt = 1;
215 lfmt[0] = '\0';
216 break;
217 case 'M':
218 memf = optarg;
219 break;
220 case 'm':
221 parsesort("vsz");
222 break;
223 case 'N':
224 nlistf = optarg;
225 break;
226 case 'O':
227 parsefmt(o1);
228 parsefmt(optarg);
229 parsefmt(o2);
230 o1[0] = o2[0] = '\0';
231 fmt = 1;
232 break;
233 case 'o':
234 parsefmt(optarg);
235 fmt = 1;
236 break;
237 case 'p':
238 what = KERN_PROC_PID;
239 flag = atol(optarg);
240 xflg = 1;
241 break;
242 case 'r':
243 parsesort("%cpu");
244 break;
245 case 'S':
246 sumrusage = 1;
247 break;
248 case 's':
249 /* -L was already taken... */
250 showlwps = 1;
251 parsefmt(sfmt);
252 fmt = 1;
253 sfmt[0] = '\0';
254 break;
255 case 'T':
256 if ((ttname = ttyname(STDIN_FILENO)) == NULL)
257 errx(1, "stdin: not a terminal");
258 goto tty;
259 case 't':
260 ttname = optarg;
261 tty: {
262 struct stat sb;
263 char *ttypath, pathbuf[MAXPATHLEN];
264
265 flag = 0;
266 ttypath = NULL;
267 if (strcmp(ttname, "?") == 0)
268 flag = KERN_PROC_TTY_NODEV;
269 else if (strcmp(ttname, "-") == 0)
270 flag = KERN_PROC_TTY_REVOKE;
271 else if (strcmp(ttname, "co") == 0)
272 ttypath = _PATH_CONSOLE;
273 else if (strncmp(ttname, "pts/", 4) == 0 ||
274 strncmp(ttname, "tty", 3) == 0)
275 (void)snprintf(ttypath = pathbuf,
276 sizeof(pathbuf), "%s%s", _PATH_DEV, ttname);
277 else if (*ttname != '/')
278 (void)snprintf(ttypath = pathbuf,
279 sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
280 else
281 ttypath = ttname;
282 what = KERN_PROC_TTY;
283 if (flag == 0) {
284 if (stat(ttypath, &sb) == -1)
285 err(1, "%s", ttypath);
286 if (!S_ISCHR(sb.st_mode))
287 errx(1, "%s: not a terminal", ttypath);
288 flag = sb.st_rdev;
289 }
290 break;
291 }
292 case 'U':
293 if (*optarg != '\0') {
294 struct passwd *pw;
295 char *ep;
296
297 what = KERN_PROC_UID;
298 pw = getpwnam(optarg);
299 if (pw == NULL) {
300 errno = 0;
301 flag = strtoul(optarg, &ep, 10);
302 if (errno)
303 err(1, "%s", optarg);
304 if (*ep != '\0')
305 errx(1, "%s: illegal user name",
306 optarg);
307 } else
308 flag = pw->pw_uid;
309 }
310 break;
311 case 'u':
312 parsefmt(ufmt);
313 parsesort("%cpu");
314 fmt = 1;
315 ufmt[0] = '\0';
316 break;
317 case 'v':
318 parsefmt(vfmt);
319 parsesort("vsz");
320 fmt = 1;
321 vfmt[0] = '\0';
322 break;
323 case 'W':
324 swapf = optarg;
325 break;
326 case 'w':
327 if (wflag)
328 termwidth = UNLIMITED;
329 else if (termwidth < 131)
330 termwidth = 131;
331 wflag++;
332 break;
333 case 'x':
334 xflg = 1;
335 break;
336 case '?':
337 default:
338 usage();
339 }
340 argc -= optind;
341 argv += optind;
342
343 #define BACKWARD_COMPATIBILITY
344 #ifdef BACKWARD_COMPATIBILITY
345 if (*argv) {
346 nlistf = *argv;
347 if (*++argv) {
348 memf = *argv;
349 if (*++argv)
350 swapf = *argv;
351 }
352 }
353 #endif
354
355 if (memf == NULL) {
356 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
357 donlist_sysctl();
358 } else
359 kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
360
361 if (kd == 0)
362 errx(1, "%s", errbuf);
363
364 if (!fmt)
365 parsefmt(dfmt);
366
367 /* Add default sort criteria */
368 parsesort("tdev,pid");
369 for (vent = sorthead; vent; vent = vent->next) {
370 if (vent->var->flag & LWP || vent->var->type == UNSPECIFIED)
371 warnx("Cannot sort on %s, sort key ignored\n",
372 vent->var->name);
373 }
374
375 /*
376 * scan requested variables, noting what structures are needed.
377 */
378 scanvars();
379
380 /*
381 * select procs
382 */
383 if (!(kinfo = getkinfo_kvm(kd, what, flag, &nentries)))
384 err(1, "%s", kvm_geterr(kd));
385 if (nentries == 0) {
386 printheader();
387 exit(1);
388 }
389 /*
390 * sort proc list
391 */
392 qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp);
393 /*
394 * For each proc, call each variable output function in
395 * "setwidth" mode to determine the widest element of
396 * the column.
397 */
398 if (mode == PRINTMODE)
399 for (i = 0; i < nentries; i++) {
400 struct kinfo_proc2 *ki = &kinfo[i];
401
402 if (xflg == 0 && (ki->p_tdev == NODEV ||
403 (ki->p_flag & P_CONTROLT) == 0))
404 continue;
405
406 kl = kvm_getlwps(kd, ki->p_pid, ki->p_paddr,
407 sizeof(struct kinfo_lwp), &nlwps);
408 if (kl == 0)
409 nlwps = 0;
410 if (showlwps == 0) {
411 l = pick_representative_lwp(ki, kl, nlwps);
412 for (vent = vhead; vent; vent = vent->next)
413 OUTPUT(vent, ki, l, WIDTHMODE);
414 } else {
415 /* The printing is done with the loops
416 * reversed, but here we don't need that,
417 * and this improves the code locality a bit.
418 */
419 for (vent = vhead; vent; vent = vent->next)
420 for (j = 0; j < nlwps; j++)
421 OUTPUT(vent, ki, &kl[j],
422 WIDTHMODE);
423 }
424 }
425 /*
426 * Print header - AFTER determining process field widths.
427 * printheader() also adds up the total width of all
428 * fields the first time it's called.
429 */
430 printheader();
431 /*
432 * For each proc, call each variable output function in
433 * print mode.
434 */
435 for (i = lineno = 0; i < nentries; i++) {
436 struct kinfo_proc2 *ki = &kinfo[i];
437
438 if (xflg == 0 && (ki->p_tdev == NODEV ||
439 (ki->p_flag & P_CONTROLT ) == 0))
440 continue;
441 kl = kvm_getlwps(kd, ki->p_pid, (u_long)ki->p_paddr,
442 sizeof(struct kinfo_lwp), &nlwps);
443 if (kl == 0)
444 nlwps = 0;
445 if (showlwps == 0) {
446 l = pick_representative_lwp(ki, kl, nlwps);
447 for (vent = vhead; vent; vent = vent->next) {
448 OUTPUT(vent, ki, l, mode);
449 if (vent->next != NULL)
450 (void)putchar(' ');
451 }
452 (void)putchar('\n');
453 if (prtheader && lineno++ == prtheader - 4) {
454 (void)putchar('\n');
455 printheader();
456 lineno = 0;
457 }
458 } else {
459 for (j = 0; j < nlwps; j++) {
460 for (vent = vhead; vent; vent = vent->next) {
461 OUTPUT(vent, ki, &kl[j], mode);
462 if (vent->next != NULL)
463 (void)putchar(' ');
464 }
465 (void)putchar('\n');
466 if (prtheader && lineno++ == prtheader - 4) {
467 (void)putchar('\n');
468 printheader();
469 lineno = 0;
470 }
471 }
472 }
473 }
474 exit(eval);
475 /* NOTREACHED */
476 }
477
478 static struct kinfo_lwp *
479 pick_representative_lwp(struct kinfo_proc2 *ki, struct kinfo_lwp *kl, int nlwps)
480 {
481 int i, onproc, running, sleeping, stopped, suspended;
482 static struct kinfo_lwp zero_lwp;
483
484 if (kl == 0)
485 return &zero_lwp;
486
487 /* Trivial case: only one LWP */
488 if (nlwps == 1)
489 return kl;
490
491 switch (ki->p_realstat) {
492 case SSTOP:
493 case SACTIVE:
494 /* Pick the most live LWP */
495 onproc = running = sleeping = stopped = suspended = -1;
496 for (i = 0; i < nlwps; i++) {
497 switch (kl[i].l_stat) {
498 case LSONPROC:
499 onproc = i;
500 break;
501 case LSRUN:
502 running = i;
503 break;
504 case LSSLEEP:
505 sleeping = i;
506 break;
507 case LSSTOP:
508 stopped = i;
509 break;
510 case LSSUSPENDED:
511 suspended = i;
512 break;
513 }
514 }
515 if (onproc != -1)
516 return &kl[onproc];
517 if (running != -1)
518 return &kl[running];
519 if (sleeping != -1)
520 return &kl[sleeping];
521 if (stopped != -1)
522 return &kl[stopped];
523 if (suspended != -1)
524 return &kl[suspended];
525 break;
526 case SZOMB:
527 /* First will do */
528 return kl;
529 break;
530 }
531 /* Error condition! */
532 warnx("Inconsistent LWP state for process %d\n", ki->p_pid);
533 return kl;
534 }
535
536
537 static struct kinfo_proc2 *
538 getkinfo_kvm(kvm_t *kdp, int what, int flag, int *nentriesp)
539 {
540
541 return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
542 nentriesp));
543 }
544
545 static void
546 scanvars(void)
547 {
548 struct varent *vent;
549 VAR *v;
550
551 for (vent = vhead; vent; vent = vent->next) {
552 v = vent->var;
553 if (v->flag & COMM) {
554 needcomm = 1;
555 break;
556 }
557 }
558 }
559
560 static int
561 pscomp(const void *a, const void *b)
562 {
563 struct kinfo_proc2 *ka = (struct kinfo_proc2 *)a;
564 struct kinfo_proc2 *kb = (struct kinfo_proc2 *)b;
565
566 int i;
567 int64_t i64;
568 VAR *v;
569 struct varent *ve;
570 sigset_t *sa, *sb;
571
572 #define V_SIZE(k) (k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize)
573 #define RDIFF_N(t, n) \
574 if (((t *)((char *)ka + v->off))[n] > ((t *)((char *)kb + v->off))[n]) \
575 return 1; \
576 if (((t *)((char *)ka + v->off))[n] < ((t *)((char *)kb + v->off))[n]) \
577 return -1;
578
579 #define RDIFF(type) RDIFF_N(type, 0); continue
580
581 for (ve = sorthead; ve != NULL; ve = ve->next) {
582 v = ve->var;
583 if (v->flag & LWP)
584 /* LWP structure not available (yet) */
585 continue;
586 /* Sort on pvar() fields, + a few others */
587 switch (v->type) {
588 case CHAR:
589 RDIFF(char);
590 case UCHAR:
591 RDIFF(u_char);
592 case SHORT:
593 RDIFF(short);
594 case USHORT:
595 RDIFF(ushort);
596 case INT:
597 RDIFF(int);
598 case UINT:
599 RDIFF(uint);
600 case LONG:
601 RDIFF(long);
602 case ULONG:
603 RDIFF(ulong);
604 case INT32:
605 RDIFF(int32_t);
606 case UINT32:
607 RDIFF(uint32_t);
608 case SIGLIST:
609 sa = (void *)((char *)a + v->off);
610 sb = (void *)((char *)b + v->off);
611 i = 0;
612 do {
613 if (sa->__bits[i] > sb->__bits[i])
614 return 1;
615 if (sa->__bits[i] < sb->__bits[i])
616 return -1;
617 i++;
618 } while (i < sizeof sa->__bits / sizeof sa->__bits[0]);
619 continue;
620 case INT64:
621 RDIFF(int64_t);
622 case KPTR:
623 case KPTR24:
624 case UINT64:
625 RDIFF(uint64_t);
626 case TIMEVAL:
627 /* compare xxx_sec then xxx_usec */
628 RDIFF_N(uint32_t, 0);
629 RDIFF_N(uint32_t, 1);
630 continue;
631 case CPUTIME:
632 i64 = ka->p_rtime_sec * 1000000 + ka->p_rtime_usec;
633 i64 -= kb->p_rtime_sec * 1000000 + kb->p_rtime_usec;
634 if (sumrusage) {
635 i64 += ka->p_uctime_sec * 1000000
636 + ka->p_uctime_usec;
637 i64 -= kb->p_uctime_sec * 1000000
638 + kb->p_uctime_usec;
639 }
640 if (i64 != 0)
641 return i64 > 0 ? 1 : -1;
642 continue;
643 case PCPU:
644 i = getpcpu(kb) - getpcpu(ka);
645 if (i != 0)
646 return i;
647 continue;
648 case VSIZE:
649 i = V_SIZE(kb) - V_SIZE(ka);
650 if (i != 0)
651 return i;
652 continue;
653
654 default:
655 /* Ignore everything else */
656 break;
657 }
658 }
659 return 0;
660
661 #undef VSIZE
662 }
663
664 /*
665 * ICK (all for getopt), would rather hide the ugliness
666 * here than taint the main code.
667 *
668 * ps foo -> ps -foo
669 * ps 34 -> ps -p34
670 *
671 * The old convention that 't' with no trailing tty arg means the user's
672 * tty, is only supported if argv[1] doesn't begin with a '-'. This same
673 * feature is available with the option 'T', which takes no argument.
674 */
675 static char *
676 kludge_oldps_options(char *s)
677 {
678 size_t len;
679 char *newopts, *ns, *cp;
680
681 len = strlen(s);
682 if ((newopts = ns = malloc(len + 3)) == NULL)
683 err(1, NULL);
684 /*
685 * options begin with '-'
686 */
687 if (*s != '-')
688 *ns++ = '-'; /* add option flag */
689 /*
690 * gaze to end of argv[1]
691 */
692 cp = s + len - 1;
693 /*
694 * if the last letter is a 't' flag and there are no other option
695 * characters that take arguments (eg U, p, o) in the option
696 * string and the option string doesn't start with a '-' then
697 * convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
698 */
699 if (*cp == 't' && *s != '-' && strpbrk(s, ARGOPTS) == NULL)
700 *cp = 'T';
701 else {
702 /*
703 * otherwise check for trailing number, which *may* be a
704 * pid.
705 */
706 while (cp >= s && isdigit((unsigned char)*cp))
707 --cp;
708 }
709 cp++;
710 memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
711 ns += cp - s;
712 /*
713 * if there's a trailing number, and not a preceding 'p' (pid) or
714 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
715 */
716 if (isdigit((unsigned char)*cp) &&
717 (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
718 cp[-1] != '/' && (cp - 1 == s || cp[-2] != 't'))))
719 *ns++ = 'p';
720 /* and append the number */
721 (void)strcpy(ns, cp); /* XXX strcpy is safe here */
722
723 return (newopts);
724 }
725
726 static void
727 usage(void)
728 {
729
730 (void)fprintf(stderr,
731 "usage:\t%s\n\t %s\n\t%s\n",
732 "ps [-acCehjlmrsSTuvwx] [-k key] [-O|o fmt] [-p pid] [-t tty]",
733 "[-M core] [-N system] [-W swap] [-U username]",
734 "ps [-L]");
735 exit(1);
736 /* NOTREACHED */
737 }
738