1 /* $NetBSD: ps.c,v 1.56 2004/11/29 04:44:10 atatat 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.56 2004/11/29 04:44:10 atatat 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 if (strcmp(ttname, "?") == 0) 267 flag = KERN_PROC_TTY_NODEV; 268 else if (strcmp(ttname, "-") == 0) 269 flag = KERN_PROC_TTY_REVOKE; 270 else if (strcmp(ttname, "co") == 0) 271 ttypath = _PATH_CONSOLE; 272 else if (strncmp(ttname, "pts/", 4) == 0) 273 (void)snprintf(ttypath = pathbuf, 274 sizeof(pathbuf), "%s%s", _PATH_DEV, ttname); 275 else if (*ttname != '/') 276 (void)snprintf(ttypath = pathbuf, 277 sizeof(pathbuf), "%s%s", _PATH_TTY, ttname); 278 else 279 ttypath = ttname; 280 what = KERN_PROC_TTY; 281 if (flag == 0) { 282 if (stat(ttypath, &sb) == -1) 283 err(1, "%s", ttypath); 284 if (!S_ISCHR(sb.st_mode)) 285 errx(1, "%s: not a terminal", ttypath); 286 flag = sb.st_rdev; 287 } 288 break; 289 } 290 case 'U': 291 if (*optarg != '\0') { 292 struct passwd *pw; 293 char *ep; 294 295 what = KERN_PROC_UID; 296 pw = getpwnam(optarg); 297 if (pw == NULL) { 298 errno = 0; 299 flag = strtoul(optarg, &ep, 10); 300 if (errno) 301 err(1, "%s", optarg); 302 if (*ep != '\0') 303 errx(1, "%s: illegal user name", 304 optarg); 305 } else 306 flag = pw->pw_uid; 307 } 308 break; 309 case 'u': 310 parsefmt(ufmt); 311 parsesort("%cpu"); 312 fmt = 1; 313 ufmt[0] = '\0'; 314 break; 315 case 'v': 316 parsefmt(vfmt); 317 parsesort("vsz"); 318 fmt = 1; 319 vfmt[0] = '\0'; 320 break; 321 case 'W': 322 swapf = optarg; 323 break; 324 case 'w': 325 if (wflag) 326 termwidth = UNLIMITED; 327 else if (termwidth < 131) 328 termwidth = 131; 329 wflag++; 330 break; 331 case 'x': 332 xflg = 1; 333 break; 334 case '?': 335 default: 336 usage(); 337 } 338 argc -= optind; 339 argv += optind; 340 341 #define BACKWARD_COMPATIBILITY 342 #ifdef BACKWARD_COMPATIBILITY 343 if (*argv) { 344 nlistf = *argv; 345 if (*++argv) { 346 memf = *argv; 347 if (*++argv) 348 swapf = *argv; 349 } 350 } 351 #endif 352 353 if (memf == NULL) { 354 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); 355 donlist_sysctl(); 356 } else 357 kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf); 358 359 if (kd == 0) 360 errx(1, "%s", errbuf); 361 362 if (!fmt) 363 parsefmt(dfmt); 364 365 /* Add default sort criteria */ 366 parsesort("tdev,pid"); 367 for (vent = sorthead; vent; vent = vent->next) { 368 if (vent->var->flag & LWP || vent->var->type == UNSPECIFIED) 369 warnx("Cannot sort on %s, sort key ignored\n", 370 vent->var->name); 371 } 372 373 /* 374 * scan requested variables, noting what structures are needed. 375 */ 376 scanvars(); 377 378 /* 379 * select procs 380 */ 381 if (!(kinfo = getkinfo_kvm(kd, what, flag, &nentries))) 382 err(1, "%s", kvm_geterr(kd)); 383 if (nentries == 0) { 384 printheader(); 385 exit(1); 386 } 387 /* 388 * sort proc list 389 */ 390 qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp); 391 /* 392 * For each proc, call each variable output function in 393 * "setwidth" mode to determine the widest element of 394 * the column. 395 */ 396 if (mode == PRINTMODE) 397 for (i = 0; i < nentries; i++) { 398 struct kinfo_proc2 *ki = &kinfo[i]; 399 400 if (xflg == 0 && (ki->p_tdev == NODEV || 401 (ki->p_flag & P_CONTROLT) == 0)) 402 continue; 403 404 kl = kvm_getlwps(kd, ki->p_pid, ki->p_paddr, 405 sizeof(struct kinfo_lwp), &nlwps); 406 if (kl == 0) 407 nlwps = 0; 408 if (showlwps == 0) { 409 l = pick_representative_lwp(ki, kl, nlwps); 410 for (vent = vhead; vent; vent = vent->next) 411 OUTPUT(vent, ki, l, WIDTHMODE); 412 } else { 413 /* The printing is done with the loops 414 * reversed, but here we don't need that, 415 * and this improves the code locality a bit. 416 */ 417 for (vent = vhead; vent; vent = vent->next) 418 for (j = 0; j < nlwps; j++) 419 OUTPUT(vent, ki, &kl[j], 420 WIDTHMODE); 421 } 422 } 423 /* 424 * Print header - AFTER determining process field widths. 425 * printheader() also adds up the total width of all 426 * fields the first time it's called. 427 */ 428 printheader(); 429 /* 430 * For each proc, call each variable output function in 431 * print mode. 432 */ 433 for (i = lineno = 0; i < nentries; i++) { 434 struct kinfo_proc2 *ki = &kinfo[i]; 435 436 if (xflg == 0 && (ki->p_tdev == NODEV || 437 (ki->p_flag & P_CONTROLT ) == 0)) 438 continue; 439 kl = kvm_getlwps(kd, ki->p_pid, (u_long)ki->p_paddr, 440 sizeof(struct kinfo_lwp), &nlwps); 441 if (kl == 0) 442 nlwps = 0; 443 if (showlwps == 0) { 444 l = pick_representative_lwp(ki, kl, nlwps); 445 for (vent = vhead; vent; vent = vent->next) { 446 OUTPUT(vent, ki, l, mode); 447 if (vent->next != NULL) 448 (void)putchar(' '); 449 } 450 (void)putchar('\n'); 451 if (prtheader && lineno++ == prtheader - 4) { 452 (void)putchar('\n'); 453 printheader(); 454 lineno = 0; 455 } 456 } else { 457 for (j = 0; j < nlwps; j++) { 458 for (vent = vhead; vent; vent = vent->next) { 459 OUTPUT(vent, ki, &kl[j], mode); 460 if (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 } 470 } 471 } 472 exit(eval); 473 /* NOTREACHED */ 474 } 475 476 static struct kinfo_lwp * 477 pick_representative_lwp(struct kinfo_proc2 *ki, struct kinfo_lwp *kl, int nlwps) 478 { 479 int i, onproc, running, sleeping, stopped, suspended; 480 static struct kinfo_lwp zero_lwp; 481 482 if (kl == 0) 483 return &zero_lwp; 484 485 /* Trivial case: only one LWP */ 486 if (nlwps == 1) 487 return kl; 488 489 switch (ki->p_realstat) { 490 case SSTOP: 491 case SACTIVE: 492 /* Pick the most live LWP */ 493 onproc = running = sleeping = stopped = suspended = -1; 494 for (i = 0; i < nlwps; i++) { 495 switch (kl[i].l_stat) { 496 case LSONPROC: 497 onproc = i; 498 break; 499 case LSRUN: 500 running = i; 501 break; 502 case LSSLEEP: 503 sleeping = i; 504 break; 505 case LSSTOP: 506 stopped = i; 507 break; 508 case LSSUSPENDED: 509 suspended = i; 510 break; 511 } 512 } 513 if (onproc != -1) 514 return &kl[onproc]; 515 if (running != -1) 516 return &kl[running]; 517 if (sleeping != -1) 518 return &kl[sleeping]; 519 if (stopped != -1) 520 return &kl[stopped]; 521 if (suspended != -1) 522 return &kl[suspended]; 523 break; 524 case SZOMB: 525 /* First will do */ 526 return kl; 527 break; 528 } 529 /* Error condition! */ 530 warnx("Inconsistent LWP state for process %d\n", ki->p_pid); 531 return kl; 532 } 533 534 535 static struct kinfo_proc2 * 536 getkinfo_kvm(kvm_t *kdp, int what, int flag, int *nentriesp) 537 { 538 539 return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2), 540 nentriesp)); 541 } 542 543 static void 544 scanvars(void) 545 { 546 struct varent *vent; 547 VAR *v; 548 549 for (vent = vhead; vent; vent = vent->next) { 550 v = vent->var; 551 if (v->flag & COMM) { 552 needcomm = 1; 553 break; 554 } 555 } 556 } 557 558 static int 559 pscomp(const void *a, const void *b) 560 { 561 struct kinfo_proc2 *ka = (struct kinfo_proc2 *)a; 562 struct kinfo_proc2 *kb = (struct kinfo_proc2 *)b; 563 564 int i; 565 int64_t i64; 566 VAR *v; 567 struct varent *ve; 568 sigset_t *sa, *sb; 569 570 #define V_SIZE(k) (k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize) 571 #define RDIFF_N(t, n) \ 572 if (((t *)((char *)ka + v->off))[n] > ((t *)((char *)kb + v->off))[n]) \ 573 return 1; \ 574 if (((t *)((char *)ka + v->off))[n] < ((t *)((char *)kb + v->off))[n]) \ 575 return -1; 576 577 #define RDIFF(type) RDIFF_N(type, 0); continue 578 579 for (ve = sorthead; ve != NULL; ve = ve->next) { 580 v = ve->var; 581 if (v->flag & LWP) 582 /* LWP structure not available (yet) */ 583 continue; 584 /* Sort on pvar() fields, + a few others */ 585 switch (v->type) { 586 case CHAR: 587 RDIFF(char); 588 case UCHAR: 589 RDIFF(u_char); 590 case SHORT: 591 RDIFF(short); 592 case USHORT: 593 RDIFF(ushort); 594 case INT: 595 RDIFF(int); 596 case UINT: 597 RDIFF(uint); 598 case LONG: 599 RDIFF(long); 600 case ULONG: 601 RDIFF(ulong); 602 case INT32: 603 RDIFF(int32_t); 604 case UINT32: 605 RDIFF(uint32_t); 606 case SIGLIST: 607 sa = (void *)((char *)a + v->off); 608 sb = (void *)((char *)b + v->off); 609 i = 0; 610 do { 611 if (sa->__bits[i] > sb->__bits[i]) 612 return 1; 613 if (sa->__bits[i] < sb->__bits[i]) 614 return -1; 615 i++; 616 } while (i < sizeof sa->__bits / sizeof sa->__bits[0]); 617 continue; 618 case INT64: 619 RDIFF(int64_t); 620 case KPTR: 621 case KPTR24: 622 case UINT64: 623 RDIFF(uint64_t); 624 case TIMEVAL: 625 /* compare xxx_sec then xxx_usec */ 626 RDIFF_N(uint32_t, 0); 627 RDIFF_N(uint32_t, 1); 628 continue; 629 case CPUTIME: 630 i64 = ka->p_rtime_sec * 1000000 + ka->p_rtime_usec; 631 i64 -= kb->p_rtime_sec * 1000000 + kb->p_rtime_usec; 632 if (sumrusage) { 633 i64 += ka->p_uctime_sec * 1000000 634 + ka->p_uctime_usec; 635 i64 -= kb->p_uctime_sec * 1000000 636 + kb->p_uctime_usec; 637 } 638 if (i64 != 0) 639 return i64 > 0 ? 1 : -1; 640 continue; 641 case PCPU: 642 i = getpcpu(kb) - getpcpu(ka); 643 if (i != 0) 644 return i; 645 continue; 646 case VSIZE: 647 i = V_SIZE(kb) - V_SIZE(ka); 648 if (i != 0) 649 return i; 650 continue; 651 652 default: 653 /* Ignore everything else */ 654 break; 655 } 656 } 657 return 0; 658 659 #undef VSIZE 660 } 661 662 /* 663 * ICK (all for getopt), would rather hide the ugliness 664 * here than taint the main code. 665 * 666 * ps foo -> ps -foo 667 * ps 34 -> ps -p34 668 * 669 * The old convention that 't' with no trailing tty arg means the user's 670 * tty, is only supported if argv[1] doesn't begin with a '-'. This same 671 * feature is available with the option 'T', which takes no argument. 672 */ 673 static char * 674 kludge_oldps_options(char *s) 675 { 676 size_t len; 677 char *newopts, *ns, *cp; 678 679 len = strlen(s); 680 if ((newopts = ns = malloc(len + 3)) == NULL) 681 err(1, NULL); 682 /* 683 * options begin with '-' 684 */ 685 if (*s != '-') 686 *ns++ = '-'; /* add option flag */ 687 /* 688 * gaze to end of argv[1] 689 */ 690 cp = s + len - 1; 691 /* 692 * if the last letter is a 't' flag and there are no other option 693 * characters that take arguments (eg U, p, o) in the option 694 * string and the option string doesn't start with a '-' then 695 * convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 696 */ 697 if (*cp == 't' && *s != '-' && strpbrk(s, ARGOPTS) == NULL) 698 *cp = 'T'; 699 else { 700 /* 701 * otherwise check for trailing number, which *may* be a 702 * pid. 703 */ 704 while (cp >= s && isdigit((unsigned char)*cp)) 705 --cp; 706 } 707 cp++; 708 memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 709 ns += cp - s; 710 /* 711 * if there's a trailing number, and not a preceding 'p' (pid) or 712 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 713 */ 714 if (isdigit((unsigned char)*cp) && 715 (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' && 716 cp[-1] != '/' && (cp - 1 == s || cp[-2] != 't')))) 717 *ns++ = 'p'; 718 /* and append the number */ 719 (void)strcpy(ns, cp); /* XXX strcpy is safe here */ 720 721 return (newopts); 722 } 723 724 static void 725 usage(void) 726 { 727 728 (void)fprintf(stderr, 729 "usage:\t%s\n\t %s\n\t%s\n", 730 "ps [-acCehjlmrsSTuvwx] [-k key] [-O|o fmt] [-p pid] [-t tty]", 731 "[-M core] [-N system] [-W swap] [-U username]", 732 "ps [-L]"); 733 exit(1); 734 /* NOTREACHED */ 735 } 736