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