ps.c revision 19068
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1990, 1993, 1994 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 323044Sdg * 3319068Speter * $Id: ps.c,v 1.12 1996/01/20 10:43:54 mpp Exp $ 341556Srgrimes */ 351556Srgrimes 361556Srgrimes#ifndef lint 371556Srgrimesstatic char copyright[] = 381556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\ 391556Srgrimes The Regents of the University of California. All rights reserved.\n"; 401556Srgrimes#endif /* not lint */ 411556Srgrimes 421556Srgrimes#ifndef lint 431556Srgrimesstatic char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 441556Srgrimes#endif /* not lint */ 451556Srgrimes 461556Srgrimes#include <sys/param.h> 473296Sdg#include <sys/user.h> 481556Srgrimes#include <sys/time.h> 491556Srgrimes#include <sys/resource.h> 501556Srgrimes#include <sys/proc.h> 511556Srgrimes#include <sys/stat.h> 521556Srgrimes#include <sys/ioctl.h> 531556Srgrimes#include <sys/sysctl.h> 541556Srgrimes 551556Srgrimes#include <ctype.h> 561556Srgrimes#include <err.h> 571556Srgrimes#include <errno.h> 581556Srgrimes#include <fcntl.h> 591556Srgrimes#include <kvm.h> 6013514Smpp#include <limits.h> 611556Srgrimes#include <nlist.h> 621556Srgrimes#include <paths.h> 631556Srgrimes#include <stdio.h> 641556Srgrimes#include <stdlib.h> 651556Srgrimes#include <string.h> 661556Srgrimes#include <unistd.h> 6711744Sache#include <locale.h> 6813020Speter#include <pwd.h> 691556Srgrimes 701556Srgrimes#include "ps.h" 711556Srgrimes 721556Srgrimes#ifdef P_PPWAIT 731556Srgrimes#define NEWVM 741556Srgrimes#endif 751556Srgrimes 761556SrgrimesKINFO *kinfo; 771556Srgrimesstruct varent *vhead, *vtail; 781556Srgrimes 791556Srgrimesint eval; /* exit value */ 8019068Speterint cflag; /* -c */ 811556Srgrimesint rawcpu; /* -C */ 821556Srgrimesint sumrusage; /* -S */ 831556Srgrimesint termwidth; /* width of screen (0 == infinity) */ 841556Srgrimesint totwidth; /* calculated width of requested variables */ 851556Srgrimes 861556Srgrimesstatic int needuser, needcomm, needenv; 871556Srgrimes 881556Srgrimesenum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 891556Srgrimes 901556Srgrimesstatic char *fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int), 911556Srgrimes KINFO *, char *, int)); 921556Srgrimesstatic char *kludge_oldps_options __P((char *)); 931556Srgrimesstatic int pscomp __P((const void *, const void *)); 941556Srgrimesstatic void saveuser __P((KINFO *)); 951556Srgrimesstatic void scanvars __P((void)); 961556Srgrimesstatic void usage __P((void)); 971556Srgrimes 981556Srgrimeschar dfmt[] = "pid tt state time command"; 991556Srgrimeschar jfmt[] = "user pid ppid pgid sess jobc state tt time command"; 1001556Srgrimeschar lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command"; 1011556Srgrimeschar o1[] = "pid"; 1021556Srgrimeschar o2[] = "tt state time command"; 1031556Srgrimeschar ufmt[] = "user pid %cpu %mem vsz rss tt state start time command"; 1041556Srgrimeschar vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command"; 1051556Srgrimes 1061556Srgrimeskvm_t *kd; 1071556Srgrimes 1081556Srgrimesint 1091556Srgrimesmain(argc, argv) 1101556Srgrimes int argc; 1111556Srgrimes char *argv[]; 1121556Srgrimes{ 1131556Srgrimes struct kinfo_proc *kp; 1141556Srgrimes struct varent *vent; 1151556Srgrimes struct winsize ws; 11613020Speter struct passwd *pwd; 1171556Srgrimes dev_t ttydev; 1181556Srgrimes pid_t pid; 1191556Srgrimes uid_t uid; 1201556Srgrimes int all, ch, flag, i, fmt, lineno, nentries; 1211556Srgrimes int prtheader, wflag, what, xflg; 12213514Smpp char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX]; 1231556Srgrimes 12411809Sache (void) setlocale(LC_ALL, ""); 12511809Sache 1261556Srgrimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1271556Srgrimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1281556Srgrimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1291556Srgrimes ws.ws_col == 0) 1301556Srgrimes termwidth = 79; 1311556Srgrimes else 1321556Srgrimes termwidth = ws.ws_col - 1; 1331556Srgrimes 1341556Srgrimes if (argc > 1) 1351556Srgrimes argv[1] = kludge_oldps_options(argv[1]); 1361556Srgrimes 1371556Srgrimes all = fmt = prtheader = wflag = xflg = 0; 1381556Srgrimes pid = -1; 1391556Srgrimes uid = (uid_t) -1; 1401556Srgrimes ttydev = NODEV; 1411556Srgrimes memf = nlistf = swapf = NULL; 1421556Srgrimes while ((ch = getopt(argc, argv, 14319068Speter "aCceghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != EOF) 1441556Srgrimes switch((char)ch) { 1451556Srgrimes case 'a': 1461556Srgrimes all = 1; 1471556Srgrimes break; 14819068Speter case 'C': 14919068Speter rawcpu = 1; 15019068Speter break; 15119068Speter case 'c': 15219068Speter cflag = 1; 15319068Speter break; 1541556Srgrimes case 'e': /* XXX set ufmt */ 1551556Srgrimes needenv = 1; 1561556Srgrimes break; 1571556Srgrimes case 'g': 1581556Srgrimes break; /* no-op */ 1591556Srgrimes case 'h': 1601556Srgrimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 1611556Srgrimes break; 1621556Srgrimes case 'j': 1631556Srgrimes parsefmt(jfmt); 1641556Srgrimes fmt = 1; 1651556Srgrimes jfmt[0] = '\0'; 1661556Srgrimes break; 1671556Srgrimes case 'L': 1681556Srgrimes showkey(); 1691556Srgrimes exit(0); 1701556Srgrimes case 'l': 1711556Srgrimes parsefmt(lfmt); 1721556Srgrimes fmt = 1; 1731556Srgrimes lfmt[0] = '\0'; 1741556Srgrimes break; 1751556Srgrimes case 'M': 1761556Srgrimes memf = optarg; 1771556Srgrimes break; 1781556Srgrimes case 'm': 1791556Srgrimes sortby = SORTMEM; 1801556Srgrimes break; 1811556Srgrimes case 'N': 1821556Srgrimes nlistf = optarg; 1831556Srgrimes break; 1841556Srgrimes case 'O': 1851556Srgrimes parsefmt(o1); 1861556Srgrimes parsefmt(optarg); 1871556Srgrimes parsefmt(o2); 1881556Srgrimes o1[0] = o2[0] = '\0'; 1891556Srgrimes fmt = 1; 1901556Srgrimes break; 1911556Srgrimes case 'o': 1921556Srgrimes parsefmt(optarg); 1931556Srgrimes fmt = 1; 1941556Srgrimes break; 1951556Srgrimes case 'p': 1961556Srgrimes pid = atol(optarg); 1971556Srgrimes xflg = 1; 1981556Srgrimes break; 1991556Srgrimes case 'r': 2001556Srgrimes sortby = SORTCPU; 2011556Srgrimes break; 2021556Srgrimes case 'S': 2031556Srgrimes sumrusage = 1; 2041556Srgrimes break; 2051556Srgrimes case 'T': 2061556Srgrimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 2071556Srgrimes errx(1, "stdin: not a terminal"); 2081556Srgrimes /* FALLTHROUGH */ 2091556Srgrimes case 't': { 2101556Srgrimes struct stat sb; 2111556Srgrimes char *ttypath, pathbuf[MAXPATHLEN]; 2121556Srgrimes 2131556Srgrimes if (strcmp(optarg, "co") == 0) 2141556Srgrimes ttypath = _PATH_CONSOLE; 2151556Srgrimes else if (*optarg != '/') 2161556Srgrimes (void)snprintf(ttypath = pathbuf, 2171556Srgrimes sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); 2181556Srgrimes else 2191556Srgrimes ttypath = optarg; 2201556Srgrimes if (stat(ttypath, &sb) == -1) 2211556Srgrimes err(1, "%s", ttypath); 2221556Srgrimes if (!S_ISCHR(sb.st_mode)) 2231556Srgrimes errx(1, "%s: not a terminal", ttypath); 2241556Srgrimes ttydev = sb.st_rdev; 2251556Srgrimes break; 2261556Srgrimes } 22713020Speter case 'U': 22813020Speter pwd = getpwnam(optarg); 22913020Speter if (pwd == NULL) 23013020Speter errx(1, "%s: no such user", optarg); 23113020Speter uid = pwd->pw_uid; 23213020Speter endpwent(); 23313020Speter xflg++; /* XXX: intuitive? */ 23413020Speter break; 2351556Srgrimes case 'u': 2361556Srgrimes parsefmt(ufmt); 2371556Srgrimes sortby = SORTCPU; 2381556Srgrimes fmt = 1; 2391556Srgrimes ufmt[0] = '\0'; 2401556Srgrimes break; 2411556Srgrimes case 'v': 2421556Srgrimes parsefmt(vfmt); 2431556Srgrimes sortby = SORTMEM; 2441556Srgrimes fmt = 1; 2451556Srgrimes vfmt[0] = '\0'; 2461556Srgrimes break; 2471556Srgrimes case 'W': 2481556Srgrimes swapf = optarg; 2491556Srgrimes break; 2501556Srgrimes case 'w': 2511556Srgrimes if (wflag) 2521556Srgrimes termwidth = UNLIMITED; 2531556Srgrimes else if (termwidth < 131) 2541556Srgrimes termwidth = 131; 2551556Srgrimes wflag++; 2561556Srgrimes break; 2571556Srgrimes case 'x': 2581556Srgrimes xflg = 1; 2591556Srgrimes break; 2601556Srgrimes case '?': 2611556Srgrimes default: 2621556Srgrimes usage(); 2631556Srgrimes } 2641556Srgrimes argc -= optind; 2651556Srgrimes argv += optind; 2661556Srgrimes 2671556Srgrimes#define BACKWARD_COMPATIBILITY 2681556Srgrimes#ifdef BACKWARD_COMPATIBILITY 2691556Srgrimes if (*argv) { 2701556Srgrimes nlistf = *argv; 2711556Srgrimes if (*++argv) { 2721556Srgrimes memf = *argv; 2731556Srgrimes if (*++argv) 2741556Srgrimes swapf = *argv; 2751556Srgrimes } 2761556Srgrimes } 2771556Srgrimes#endif 2781556Srgrimes /* 2791556Srgrimes * Discard setgid privileges if not the running kernel so that bad 2801556Srgrimes * guys can't print interesting stuff from kernel memory. 2811556Srgrimes */ 2821556Srgrimes if (nlistf != NULL || memf != NULL || swapf != NULL) 2831556Srgrimes setgid(getgid()); 2841556Srgrimes 2851556Srgrimes kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf); 2861556Srgrimes if (kd == 0) 2871556Srgrimes errx(1, "%s", errbuf); 2881556Srgrimes 2891556Srgrimes if (!fmt) 2901556Srgrimes parsefmt(dfmt); 2911556Srgrimes 29213020Speter /* XXX - should be cleaner */ 29313020Speter if (!all && ttydev == NODEV && pid == -1 && uid == (uid_t)-1) 2941556Srgrimes uid = getuid(); 2951556Srgrimes 2961556Srgrimes /* 2971556Srgrimes * scan requested variables, noting what structures are needed, 2981556Srgrimes * and adjusting header widths as appropiate. 2991556Srgrimes */ 3001556Srgrimes scanvars(); 3011556Srgrimes /* 3021556Srgrimes * get proc list 3031556Srgrimes */ 3041556Srgrimes if (uid != (uid_t) -1) { 3051556Srgrimes what = KERN_PROC_UID; 3061556Srgrimes flag = uid; 3071556Srgrimes } else if (ttydev != NODEV) { 3081556Srgrimes what = KERN_PROC_TTY; 3091556Srgrimes flag = ttydev; 3101556Srgrimes } else if (pid != -1) { 3111556Srgrimes what = KERN_PROC_PID; 3121556Srgrimes flag = pid; 3131556Srgrimes } else { 3141556Srgrimes what = KERN_PROC_ALL; 3151556Srgrimes flag = 0; 3161556Srgrimes } 3171556Srgrimes /* 3181556Srgrimes * select procs 3191556Srgrimes */ 3201556Srgrimes if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0) 3211556Srgrimes errx(1, "%s", kvm_geterr(kd)); 3221556Srgrimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 3231556Srgrimes err(1, NULL); 3241556Srgrimes for (i = nentries; --i >= 0; ++kp) { 3251556Srgrimes kinfo[i].ki_p = kp; 3261556Srgrimes if (needuser) 3271556Srgrimes saveuser(&kinfo[i]); 3281556Srgrimes } 3291556Srgrimes /* 3301556Srgrimes * print header 3311556Srgrimes */ 3321556Srgrimes printheader(); 3331556Srgrimes if (nentries == 0) 3341556Srgrimes exit(0); 3351556Srgrimes /* 3361556Srgrimes * sort proc list 3371556Srgrimes */ 3381556Srgrimes qsort(kinfo, nentries, sizeof(KINFO), pscomp); 3391556Srgrimes /* 3401556Srgrimes * for each proc, call each variable output function. 3411556Srgrimes */ 3421556Srgrimes for (i = lineno = 0; i < nentries; i++) { 3431556Srgrimes if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV || 3441556Srgrimes (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0)) 3451556Srgrimes continue; 3461556Srgrimes for (vent = vhead; vent; vent = vent->next) { 3471556Srgrimes (vent->var->oproc)(&kinfo[i], vent); 3481556Srgrimes if (vent->next != NULL) 3491556Srgrimes (void)putchar(' '); 3501556Srgrimes } 3511556Srgrimes (void)putchar('\n'); 3521556Srgrimes if (prtheader && lineno++ == prtheader - 4) { 3531556Srgrimes (void)putchar('\n'); 3541556Srgrimes printheader(); 3551556Srgrimes lineno = 0; 3561556Srgrimes } 3571556Srgrimes } 3581556Srgrimes exit(eval); 3591556Srgrimes} 3601556Srgrimes 3611556Srgrimesstatic void 3621556Srgrimesscanvars() 3631556Srgrimes{ 3641556Srgrimes struct varent *vent; 3651556Srgrimes VAR *v; 3661556Srgrimes int i; 3671556Srgrimes 3681556Srgrimes for (vent = vhead; vent; vent = vent->next) { 3691556Srgrimes v = vent->var; 3701556Srgrimes i = strlen(v->header); 3711556Srgrimes if (v->width < i) 3721556Srgrimes v->width = i; 3731556Srgrimes totwidth += v->width + 1; /* +1 for space */ 3741556Srgrimes if (v->flag & USER) 3751556Srgrimes needuser = 1; 3761556Srgrimes if (v->flag & COMM) 3771556Srgrimes needcomm = 1; 3781556Srgrimes } 3791556Srgrimes totwidth--; 3801556Srgrimes} 3811556Srgrimes 3821556Srgrimesstatic char * 3831556Srgrimesfmt(fn, ki, comm, maxlen) 3841556Srgrimes char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int)); 3851556Srgrimes KINFO *ki; 3861556Srgrimes char *comm; 3871556Srgrimes int maxlen; 3881556Srgrimes{ 3891556Srgrimes char *s; 3901556Srgrimes 3911556Srgrimes if ((s = 3921556Srgrimes fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL) 3931556Srgrimes err(1, NULL); 3941556Srgrimes return (s); 3951556Srgrimes} 3961556Srgrimes 3971556Srgrimesstatic void 3981556Srgrimessaveuser(ki) 3991556Srgrimes KINFO *ki; 4001556Srgrimes{ 4011556Srgrimes struct pstats pstats; 4021556Srgrimes struct usave *usp; 4033686Sdg struct user *u_addr = (struct user *)USRSTACK; 4041556Srgrimes 4051556Srgrimes usp = &ki->ki_u; 4067165Sjoerg if (kvm_uread(kd, KI_PROC(ki), (unsigned long)&u_addr->u_stats, 4071556Srgrimes (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) { 4081556Srgrimes /* 4091556Srgrimes * The u-area might be swapped out, and we can't get 4101556Srgrimes * at it because we have a crashdump and no swap. 4111556Srgrimes * If it's here fill in these fields, otherwise, just 4121556Srgrimes * leave them 0. 4131556Srgrimes */ 4141556Srgrimes usp->u_start = pstats.p_start; 4151556Srgrimes usp->u_ru = pstats.p_ru; 4161556Srgrimes usp->u_cru = pstats.p_cru; 4171556Srgrimes usp->u_valid = 1; 4181556Srgrimes } else 4191556Srgrimes usp->u_valid = 0; 4201556Srgrimes /* 4211556Srgrimes * save arguments if needed 4221556Srgrimes */ 4231556Srgrimes if (needcomm) 4241556Srgrimes ki->ki_args = fmt(kvm_getargv, ki, KI_PROC(ki)->p_comm, 4251556Srgrimes MAXCOMLEN); 4261556Srgrimes else 4271556Srgrimes ki->ki_args = NULL; 4281556Srgrimes if (needenv) 4291556Srgrimes ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0); 4301556Srgrimes else 4311556Srgrimes ki->ki_env = NULL; 4321556Srgrimes} 4331556Srgrimes 4341556Srgrimesstatic int 4351556Srgrimespscomp(a, b) 4361556Srgrimes const void *a, *b; 4371556Srgrimes{ 4381556Srgrimes int i; 4391556Srgrimes#ifdef NEWVM 4401556Srgrimes#define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \ 4411556Srgrimes KI_EPROC(k)->e_vm.vm_tsize) 4421556Srgrimes#else 4431556Srgrimes#define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize) 4441556Srgrimes#endif 4451556Srgrimes 4461556Srgrimes if (sortby == SORTCPU) 4471556Srgrimes return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a)); 4481556Srgrimes if (sortby == SORTMEM) 4491556Srgrimes return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a)); 4501556Srgrimes i = KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev; 4511556Srgrimes if (i == 0) 4521556Srgrimes i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid; 4531556Srgrimes return (i); 4541556Srgrimes} 4551556Srgrimes 4561556Srgrimes/* 4571556Srgrimes * ICK (all for getopt), would rather hide the ugliness 4581556Srgrimes * here than taint the main code. 4591556Srgrimes * 4601556Srgrimes * ps foo -> ps -foo 4611556Srgrimes * ps 34 -> ps -p34 4621556Srgrimes * 4631556Srgrimes * The old convention that 't' with no trailing tty arg means the users 4641556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 4651556Srgrimes * feature is available with the option 'T', which takes no argument. 4661556Srgrimes */ 4671556Srgrimesstatic char * 4681556Srgrimeskludge_oldps_options(s) 4691556Srgrimes char *s; 4701556Srgrimes{ 4711556Srgrimes size_t len; 4721556Srgrimes char *newopts, *ns, *cp; 4731556Srgrimes 4741556Srgrimes len = strlen(s); 4751556Srgrimes if ((newopts = ns = malloc(len + 2)) == NULL) 4761556Srgrimes err(1, NULL); 4771556Srgrimes /* 4781556Srgrimes * options begin with '-' 4791556Srgrimes */ 4801556Srgrimes if (*s != '-') 4811556Srgrimes *ns++ = '-'; /* add option flag */ 4821556Srgrimes /* 4831556Srgrimes * gaze to end of argv[1] 4841556Srgrimes */ 4851556Srgrimes cp = s + len - 1; 4861556Srgrimes /* 4871556Srgrimes * if last letter is a 't' flag with no argument (in the context 4881556Srgrimes * of the oldps options -- option string NOT starting with a '-' -- 4891556Srgrimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 4901556Srgrimes */ 4911556Srgrimes if (*cp == 't' && *s != '-') 4921556Srgrimes *cp = 'T'; 4931556Srgrimes else { 4941556Srgrimes /* 4951556Srgrimes * otherwise check for trailing number, which *may* be a 4961556Srgrimes * pid. 4971556Srgrimes */ 4981556Srgrimes while (cp >= s && isdigit(*cp)) 4991556Srgrimes --cp; 5001556Srgrimes } 5011556Srgrimes cp++; 5021556Srgrimes memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 5031556Srgrimes ns += cp - s; 5041556Srgrimes /* 5051556Srgrimes * if there's a trailing number, and not a preceding 'p' (pid) or 5061556Srgrimes * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 5071556Srgrimes */ 5088855Srgrimes if (isdigit(*cp) && 5097165Sjoerg (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) && 5107165Sjoerg (cp - 1 == s || cp[-2] != 't')) 5111556Srgrimes *ns++ = 'p'; 5121556Srgrimes (void)strcpy(ns, cp); /* and append the number */ 5131556Srgrimes 5141556Srgrimes return (newopts); 5151556Srgrimes} 5161556Srgrimes 5171556Srgrimesstatic void 5181556Srgrimesusage() 5191556Srgrimes{ 5201556Srgrimes 5211556Srgrimes (void)fprintf(stderr, 5221556Srgrimes "usage:\t%s\n\t %s\n\t%s\n", 52313399Speter "ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty] [-U user]", 5241556Srgrimes "[-M core] [-N system] [-W swap]", 5251556Srgrimes "ps [-L]"); 5261556Srgrimes exit(1); 5271556Srgrimes} 528