ps.c revision 1.65 1 /* $NetBSD: ps.c,v 1.65 2008/02/10 17:48:00 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 * 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.65 2008/02/10 17:48:00 christos 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 varlist displaylist = SIMPLEQ_HEAD_INITIALIZER(displaylist);
118 struct varlist sortlist = SIMPLEQ_HEAD_INITIALIZER(sortlist);
119
120 int eval; /* exit value */
121 int rawcpu; /* -C */
122 int sumrusage; /* -S */
123 int termwidth; /* width of screen (0 == infinity) */
124 int totwidth; /* calculated width of requested variables */
125
126 int needcomm, needenv, commandonly;
127 uid_t myuid;
128
129 static struct kinfo_lwp
130 *pick_representative_lwp(struct kinfo_proc2 *,
131 struct kinfo_lwp *, int);
132 static struct kinfo_proc2
133 *getkinfo_kvm(kvm_t *, int, int, int *);
134 static char *kludge_oldps_options(char *);
135 static int pscomp(const void *, const void *);
136 static void scanvars(void);
137 static void usage(void);
138 static int parsenum(const char *, const char *);
139 int main(int, char *[]);
140
141 char dfmt[] = "pid tt state time command";
142 char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
143 char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt 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 const char *default_fmt = dfmt;
150
151 struct varent *Opos = NULL; /* -O flag inserts after this point */
152
153 kvm_t *kd;
154
155 int
156 main(int argc, char *argv[])
157 {
158 struct varent *vent;
159 struct winsize ws;
160 struct kinfo_lwp *kl, *l;
161 int ch, flag, i, j, fmt, lineno, nentries, nlwps;
162 int prtheader, wflag, what, xflg, mode, showlwps;
163 char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
164 char *ttname;
165
166 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
167 ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
168 ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
169 ws.ws_col == 0)
170 termwidth = 79;
171 else
172 termwidth = ws.ws_col - 1;
173
174 setncpu();
175
176 if (argc > 1)
177 argv[1] = kludge_oldps_options(argv[1]);
178
179 fmt = prtheader = wflag = xflg = showlwps = 0;
180 what = KERN_PROC_UID;
181 flag = myuid = getuid();
182 memf = nlistf = swapf = NULL;
183 mode = PRINTMODE;
184 while ((ch = getopt(argc, argv, GETOPTSTR)) != -1)
185 switch((char)ch) {
186 case 'a':
187 what = KERN_PROC_ALL;
188 flag = 0;
189 break;
190 case 'c':
191 commandonly = 1;
192 break;
193 case 'e': /* XXX set ufmt */
194 needenv = 1;
195 break;
196 case 'C':
197 rawcpu = 1;
198 break;
199 case 'g':
200 break; /* no-op */
201 case 'h':
202 prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
203 break;
204 case 'j':
205 parsefmt(jfmt);
206 fmt = 1;
207 jfmt[0] = '\0';
208 break;
209 case 'k':
210 parsesort(optarg);
211 break;
212 case 'K':
213 break; /* no-op - was dontuseprocfs */
214 case 'L':
215 showkey();
216 exit(0);
217 /* NOTREACHED */
218 case 'l':
219 parsefmt(lfmt);
220 fmt = 1;
221 lfmt[0] = '\0';
222 break;
223 case 'M':
224 memf = optarg;
225 break;
226 case 'm':
227 parsesort("vsz");
228 break;
229 case 'N':
230 nlistf = optarg;
231 break;
232 case 'O':
233 /*
234 * If this is not the first -O option, insert
235 * just after the previous one.
236 *
237 * If there is no format yet, start with the default
238 * format, and insert after the pid column.
239 *
240 * If there is already a format, insert after
241 * the pid column, or at the end if there's no
242 * pid column.
243 */
244 if (!Opos) {
245 if (!fmt)
246 parsefmt(default_fmt);
247 Opos = varlist_find(&displaylist, "pid");
248 }
249 parsefmt_insert(optarg, &Opos);
250 fmt = 1;
251 break;
252 case 'o':
253 parsefmt(optarg);
254 fmt = 1;
255 break;
256 case 'p':
257 what = KERN_PROC_PID;
258 flag = parsenum(optarg, "process id");
259 xflg = 1;
260 break;
261 case 'r':
262 parsesort("%cpu");
263 break;
264 case 'S':
265 sumrusage = 1;
266 break;
267 case 's':
268 /* -L was already taken... */
269 showlwps = 1;
270 default_fmt = sfmt;
271 break;
272 case 'T':
273 if ((ttname = ttyname(STDIN_FILENO)) == NULL)
274 errx(1, "stdin: not a terminal");
275 goto tty;
276 case 't':
277 ttname = optarg;
278 tty: {
279 struct stat sb;
280 const char *ttypath;
281 char pathbuf[MAXPATHLEN];
282
283 flag = 0;
284 ttypath = NULL;
285 if (strcmp(ttname, "?") == 0)
286 flag = KERN_PROC_TTY_NODEV;
287 else if (strcmp(ttname, "-") == 0)
288 flag = KERN_PROC_TTY_REVOKE;
289 else if (strcmp(ttname, "co") == 0)
290 ttypath = _PATH_CONSOLE;
291 else if (strncmp(ttname, "pts/", 4) == 0 ||
292 strncmp(ttname, "tty", 3) == 0) {
293 (void)snprintf(pathbuf,
294 sizeof(pathbuf), "%s%s", _PATH_DEV, ttname);
295 ttypath = pathbuf;
296 } else if (*ttname != '/') {
297 (void)snprintf(pathbuf,
298 sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
299 ttypath = pathbuf;
300 } else
301 ttypath = ttname;
302 what = KERN_PROC_TTY;
303 if (flag == 0) {
304 if (stat(ttypath, &sb) == -1)
305 err(1, "%s", ttypath);
306 if (!S_ISCHR(sb.st_mode))
307 errx(1, "%s: not a terminal", ttypath);
308 flag = sb.st_rdev;
309 }
310 break;
311 }
312 case 'U':
313 if (*optarg != '\0') {
314 struct passwd *pw;
315
316 what = KERN_PROC_UID;
317 pw = getpwnam(optarg);
318 if (pw == NULL) {
319 flag = parsenum(optarg, "user name");
320 } else
321 flag = pw->pw_uid;
322 }
323 break;
324 case 'u':
325 parsefmt(ufmt);
326 parsesort("%cpu");
327 fmt = 1;
328 ufmt[0] = '\0';
329 break;
330 case 'v':
331 parsefmt(vfmt);
332 parsesort("vsz");
333 fmt = 1;
334 vfmt[0] = '\0';
335 break;
336 case 'W':
337 swapf = optarg;
338 break;
339 case 'w':
340 if (wflag)
341 termwidth = UNLIMITED;
342 else if (termwidth < 131)
343 termwidth = 131;
344 wflag++;
345 break;
346 case 'x':
347 xflg = 1;
348 break;
349 case '?':
350 default:
351 usage();
352 }
353 argc -= optind;
354 argv += optind;
355
356 #define BACKWARD_COMPATIBILITY
357 #ifdef BACKWARD_COMPATIBILITY
358 if (*argv) {
359 nlistf = *argv;
360 if (*++argv) {
361 memf = *argv;
362 if (*++argv)
363 swapf = *argv;
364 }
365 }
366 #endif
367
368 if (memf == NULL) {
369 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
370 donlist_sysctl();
371 } else
372 kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
373
374 if (kd == 0)
375 errx(1, "%s", errbuf);
376
377 if (!fmt)
378 parsefmt(default_fmt);
379
380 /* Add default sort criteria */
381 parsesort("tdev,pid");
382 SIMPLEQ_FOREACH(vent, &sortlist, next) {
383 if (vent->var->flag & LWP || vent->var->type == UNSPECIFIED)
384 warnx("Cannot sort on %s, sort key ignored\n",
385 vent->var->name);
386 }
387
388 /*
389 * scan requested variables, noting what structures are needed.
390 */
391 scanvars();
392
393 /*
394 * select procs
395 */
396 if (!(kinfo = getkinfo_kvm(kd, what, flag, &nentries)))
397 err(1, "%s", kvm_geterr(kd));
398 if (nentries == 0) {
399 printheader();
400 exit(1);
401 }
402 /*
403 * sort proc list
404 */
405 qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp);
406 /*
407 * For each proc, call each variable output function in
408 * "setwidth" mode to determine the widest element of
409 * the column.
410 */
411 if (mode == PRINTMODE)
412 for (i = 0; i < nentries; i++) {
413 struct kinfo_proc2 *ki = &kinfo[i];
414
415 if (xflg == 0 && (ki->p_tdev == NODEV ||
416 (ki->p_flag & P_CONTROLT) == 0))
417 continue;
418
419 kl = kvm_getlwps(kd, ki->p_pid, ki->p_paddr,
420 sizeof(struct kinfo_lwp), &nlwps);
421 if (kl == 0)
422 nlwps = 0;
423 if (showlwps == 0) {
424 l = pick_representative_lwp(ki, kl, nlwps);
425 SIMPLEQ_FOREACH(vent, &displaylist, next)
426 OUTPUT(vent, ki, l, WIDTHMODE);
427 } else {
428 /* The printing is done with the loops
429 * reversed, but here we don't need that,
430 * and this improves the code locality a bit.
431 */
432 SIMPLEQ_FOREACH(vent, &displaylist, next)
433 for (j = 0; j < nlwps; j++)
434 OUTPUT(vent, ki, &kl[j],
435 WIDTHMODE);
436 }
437 }
438 /*
439 * Print header - AFTER determining process field widths.
440 * printheader() also adds up the total width of all
441 * fields the first time it's called.
442 */
443 printheader();
444 /*
445 * For each proc, call each variable output function in
446 * print mode.
447 */
448 for (i = lineno = 0; i < nentries; i++) {
449 struct kinfo_proc2 *ki = &kinfo[i];
450
451 if (xflg == 0 && (ki->p_tdev == NODEV ||
452 (ki->p_flag & P_CONTROLT ) == 0))
453 continue;
454 kl = kvm_getlwps(kd, ki->p_pid, (u_long)ki->p_paddr,
455 sizeof(struct kinfo_lwp), &nlwps);
456 if (kl == 0)
457 nlwps = 0;
458 if (showlwps == 0) {
459 l = pick_representative_lwp(ki, kl, nlwps);
460 SIMPLEQ_FOREACH(vent, &displaylist, next) {
461 OUTPUT(vent, ki, l, mode);
462 if (SIMPLEQ_NEXT(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 } else {
472 for (j = 0; j < nlwps; j++) {
473 SIMPLEQ_FOREACH(vent, &displaylist, next) {
474 OUTPUT(vent, ki, &kl[j], mode);
475 if (SIMPLEQ_NEXT(vent, next) != NULL)
476 (void)putchar(' ');
477 }
478 (void)putchar('\n');
479 if (prtheader && lineno++ == prtheader - 4) {
480 (void)putchar('\n');
481 printheader();
482 lineno = 0;
483 }
484 }
485 }
486 }
487 exit(eval);
488 /* NOTREACHED */
489 }
490
491 static struct kinfo_lwp *
492 pick_representative_lwp(struct kinfo_proc2 *ki, struct kinfo_lwp *kl, int nlwps)
493 {
494 int i, onproc, running, sleeping, stopped, suspended;
495 static struct kinfo_lwp zero_lwp;
496
497 if (kl == 0)
498 return &zero_lwp;
499
500 /* Trivial case: only one LWP */
501 if (nlwps == 1)
502 return kl;
503
504 switch (ki->p_realstat) {
505 case SSTOP:
506 case SACTIVE:
507 /* Pick the most live LWP */
508 onproc = running = sleeping = stopped = suspended = -1;
509 for (i = 0; i < nlwps; i++) {
510 switch (kl[i].l_stat) {
511 case LSONPROC:
512 onproc = i;
513 break;
514 case LSRUN:
515 running = i;
516 break;
517 case LSSLEEP:
518 sleeping = i;
519 break;
520 case LSSTOP:
521 stopped = i;
522 break;
523 case LSSUSPENDED:
524 suspended = i;
525 break;
526 }
527 }
528 if (onproc != -1)
529 return &kl[onproc];
530 if (running != -1)
531 return &kl[running];
532 if (sleeping != -1)
533 return &kl[sleeping];
534 if (stopped != -1)
535 return &kl[stopped];
536 if (suspended != -1)
537 return &kl[suspended];
538 break;
539 case SZOMB:
540 /* First will do */
541 return kl;
542 break;
543 }
544 /* Error condition! */
545 warnx("Inconsistent LWP state for process %d\n", ki->p_pid);
546 return kl;
547 }
548
549
550 static struct kinfo_proc2 *
551 getkinfo_kvm(kvm_t *kdp, int what, int flag, int *nentriesp)
552 {
553
554 return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
555 nentriesp));
556 }
557
558 static void
559 scanvars(void)
560 {
561 struct varent *vent;
562 VAR *v;
563
564 SIMPLEQ_FOREACH(vent, &displaylist, next) {
565 v = vent->var;
566 if (v->flag & COMM) {
567 needcomm = 1;
568 break;
569 }
570 }
571 }
572
573 static int
574 pscomp(const void *a, const void *b)
575 {
576 const struct kinfo_proc2 *ka = (const struct kinfo_proc2 *)a;
577 const struct kinfo_proc2 *kb = (const struct kinfo_proc2 *)b;
578
579 int i;
580 int64_t i64;
581 VAR *v;
582 struct varent *ve;
583 const sigset_t *sa, *sb;
584
585 #define V_SIZE(k) (k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize)
586 #define RDIFF_N(t, n) \
587 if (((const t *)((const char *)ka + v->off))[n] > ((const t *)((const char *)kb + v->off))[n]) \
588 return 1; \
589 if (((const t *)((const char *)ka + v->off))[n] < ((const t *)((const char *)kb + v->off))[n]) \
590 return -1;
591
592 #define RDIFF(type) RDIFF_N(type, 0); continue
593
594 SIMPLEQ_FOREACH(ve, &sortlist, next) {
595 v = ve->var;
596 if (v->flag & LWP)
597 /* LWP structure not available (yet) */
598 continue;
599 /* Sort on pvar() fields, + a few others */
600 switch (v->type) {
601 case CHAR:
602 RDIFF(char);
603 case UCHAR:
604 RDIFF(u_char);
605 case SHORT:
606 RDIFF(short);
607 case USHORT:
608 RDIFF(ushort);
609 case INT:
610 RDIFF(int);
611 case UINT:
612 RDIFF(uint);
613 case LONG:
614 RDIFF(long);
615 case ULONG:
616 RDIFF(ulong);
617 case INT32:
618 RDIFF(int32_t);
619 case UINT32:
620 RDIFF(uint32_t);
621 case SIGLIST:
622 sa = (const void *)((const char *)a + v->off);
623 sb = (const void *)((const char *)b + v->off);
624 i = 0;
625 do {
626 if (sa->__bits[i] > sb->__bits[i])
627 return 1;
628 if (sa->__bits[i] < sb->__bits[i])
629 return -1;
630 i++;
631 } while (i < sizeof sa->__bits / sizeof sa->__bits[0]);
632 continue;
633 case INT64:
634 RDIFF(int64_t);
635 case KPTR:
636 case KPTR24:
637 case UINT64:
638 RDIFF(uint64_t);
639 case TIMEVAL:
640 /* compare xxx_sec then xxx_usec */
641 RDIFF_N(uint32_t, 0);
642 RDIFF_N(uint32_t, 1);
643 continue;
644 case CPUTIME:
645 i64 = ka->p_rtime_sec * 1000000 + ka->p_rtime_usec;
646 i64 -= kb->p_rtime_sec * 1000000 + kb->p_rtime_usec;
647 if (sumrusage) {
648 i64 += ka->p_uctime_sec * 1000000
649 + ka->p_uctime_usec;
650 i64 -= kb->p_uctime_sec * 1000000
651 + kb->p_uctime_usec;
652 }
653 if (i64 != 0)
654 return i64 > 0 ? 1 : -1;
655 continue;
656 case PCPU:
657 i = getpcpu(kb) - getpcpu(ka);
658 if (i != 0)
659 return i;
660 continue;
661 case VSIZE:
662 i = V_SIZE(kb) - V_SIZE(ka);
663 if (i != 0)
664 return i;
665 continue;
666
667 default:
668 /* Ignore everything else */
669 break;
670 }
671 }
672 return 0;
673
674 #undef VSIZE
675 }
676
677 /*
678 * ICK (all for getopt), would rather hide the ugliness
679 * here than taint the main code.
680 *
681 * ps foo -> ps -foo
682 * ps 34 -> ps -p34
683 *
684 * The old convention that 't' with no trailing tty arg means the user's
685 * tty, is only supported if argv[1] doesn't begin with a '-'. This same
686 * feature is available with the option 'T', which takes no argument.
687 */
688 static char *
689 kludge_oldps_options(char *s)
690 {
691 size_t len;
692 char *newopts, *ns, *cp;
693
694 len = strlen(s);
695 if ((newopts = ns = malloc(len + 3)) == NULL)
696 err(1, NULL);
697 /*
698 * options begin with '-'
699 */
700 if (*s != '-')
701 *ns++ = '-'; /* add option flag */
702 /*
703 * gaze to end of argv[1]
704 */
705 cp = s + len - 1;
706 /*
707 * if the last letter is a 't' flag and there are no other option
708 * characters that take arguments (eg U, p, o) in the option
709 * string and the option string doesn't start with a '-' then
710 * convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
711 */
712 if (*cp == 't' && *s != '-' && strpbrk(s, ARGOPTS) == NULL)
713 *cp = 'T';
714 else {
715 /*
716 * otherwise check for trailing number, which *may* be a
717 * pid.
718 */
719 while (cp >= s && isdigit((unsigned char)*cp))
720 --cp;
721 }
722 cp++;
723 memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
724 ns += cp - s;
725 /*
726 * if there's a trailing number, and not a preceding 'p' (pid) or
727 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
728 */
729 if (isdigit((unsigned char)*cp) &&
730 (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
731 cp[-1] != '/' && (cp - 1 == s || cp[-2] != 't'))))
732 *ns++ = 'p';
733 /* and append the number */
734 (void)strcpy(ns, cp); /* XXX strcpy is safe here */
735
736 return (newopts);
737 }
738
739 static int
740 parsenum(const char *str, const char *msg)
741 {
742 char *ep;
743 unsigned long ul;
744
745 ul = strtoul(str, &ep, 0);
746
747 if (*str == '\0' || *ep != '\0')
748 errx(1, "Invalid %s: `%s'", msg, str);
749
750 if (ul > INT_MAX)
751 errx(1, "Out of range %s: `%s'", msg, str);
752
753 return (int)ul;
754 }
755
756 static void
757 usage(void)
758 {
759
760 (void)fprintf(stderr,
761 "usage:\t%s\n\t %s\n\t%s\n",
762 "ps [-acCehjlmrsSTuvwx] [-k key] [-O|o fmt] [-p pid] [-t tty]",
763 "[-M core] [-N system] [-W swap] [-U username]",
764 "ps [-L]");
765 exit(1);
766 /* NOTREACHED */
767 }
768