17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
551223f18Sjonb * Common Development and Distribution License (the "License").
651223f18Sjonb * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
2151223f18Sjonb
227c478bd9Sstevel@tonic-gate /*
230a1278f2SGary Mills * Copyright (c) 2013 Gary Mills
240a1278f2SGary Mills *
25f5c9e9f9SCasper H.S. Dik * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
29eaaa4d5fSBryan Cantrill /*
30eaaa4d5fSBryan Cantrill * Copyright (c) 2012, Joyent, Inc. All rights reserved.
31eaaa4d5fSBryan Cantrill */
32eaaa4d5fSBryan Cantrill
337c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
347c478bd9Sstevel@tonic-gate /* All Rights Reserved */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * ps -- print things about processes.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <grp.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <limits.h>
527c478bd9Sstevel@tonic-gate #include <dirent.h>
537c478bd9Sstevel@tonic-gate #include <sys/signal.h>
547c478bd9Sstevel@tonic-gate #include <sys/fault.h>
557c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
567c478bd9Sstevel@tonic-gate #include <sys/time.h>
577c478bd9Sstevel@tonic-gate #include <procfs.h>
587c478bd9Sstevel@tonic-gate #include <locale.h>
597c478bd9Sstevel@tonic-gate #include <wctype.h>
607c478bd9Sstevel@tonic-gate #include <wchar.h>
617c478bd9Sstevel@tonic-gate #include <libw.h>
627c478bd9Sstevel@tonic-gate #include <stdarg.h>
637c478bd9Sstevel@tonic-gate #include <sys/proc.h>
647c478bd9Sstevel@tonic-gate #include <sys/pset.h>
657c478bd9Sstevel@tonic-gate #include <project.h>
667c478bd9Sstevel@tonic-gate #include <zone.h>
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #define min(a, b) ((a) > (b) ? (b) : (a))
697c478bd9Sstevel@tonic-gate #define max(a, b) ((a) < (b) ? (b) : (a))
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #define NTTYS 20 /* initial size of table for -t option */
72c6402783Sakolb #define SIZ 30 /* initial size of tables for -p, -s, -g, -h and -z */
7351223f18Sjonb
7451223f18Sjonb /*
7551223f18Sjonb * Size of buffer holding args for t, p, s, g, u, U, G, z options.
7651223f18Sjonb * Set to ZONENAME_MAX, the minimum value needed to allow any
7751223f18Sjonb * zone to be specified.
7851223f18Sjonb */
7951223f18Sjonb #define ARGSIZ ZONENAME_MAX
807c478bd9Sstevel@tonic-gate
810a1278f2SGary Mills /* Max chars in a user/group name or printed u/g id */
820a1278f2SGary Mills #define MAXUGNAME (LOGNAME_MAX+2)
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /* Structure for storing user or group info */
857c478bd9Sstevel@tonic-gate struct ugdata {
867c478bd9Sstevel@tonic-gate id_t id; /* numeric user-id or group-id */
877c478bd9Sstevel@tonic-gate char name[MAXUGNAME+1]; /* user/group name, null terminated */
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate struct ughead {
917c478bd9Sstevel@tonic-gate size_t size; /* number of ugdata structs allocated */
927c478bd9Sstevel@tonic-gate size_t nent; /* number of active entries */
937c478bd9Sstevel@tonic-gate struct ugdata *ent; /* pointer to array of actual entries */
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate enum fname { /* enumeration of field names */
977c478bd9Sstevel@tonic-gate F_USER, /* effective user of the process */
987c478bd9Sstevel@tonic-gate F_RUSER, /* real user of the process */
997c478bd9Sstevel@tonic-gate F_GROUP, /* effective group of the process */
1007c478bd9Sstevel@tonic-gate F_RGROUP, /* real group of the process */
1017c478bd9Sstevel@tonic-gate F_UID, /* numeric effective uid of the process */
1027c478bd9Sstevel@tonic-gate F_RUID, /* numeric real uid of the process */
1037c478bd9Sstevel@tonic-gate F_GID, /* numeric effective gid of the process */
1047c478bd9Sstevel@tonic-gate F_RGID, /* numeric real gid of the process */
1057c478bd9Sstevel@tonic-gate F_PID, /* process id */
1067c478bd9Sstevel@tonic-gate F_PPID, /* parent process id */
1077c478bd9Sstevel@tonic-gate F_PGID, /* process group id */
1087c478bd9Sstevel@tonic-gate F_SID, /* session id */
1097c478bd9Sstevel@tonic-gate F_PSR, /* bound processor */
1107c478bd9Sstevel@tonic-gate F_LWP, /* lwp-id */
1117c478bd9Sstevel@tonic-gate F_NLWP, /* number of lwps */
1127c478bd9Sstevel@tonic-gate F_OPRI, /* old priority (obsolete) */
1137c478bd9Sstevel@tonic-gate F_PRI, /* new priority */
1147c478bd9Sstevel@tonic-gate F_F, /* process flags */
1157c478bd9Sstevel@tonic-gate F_S, /* letter indicating the state */
1167c478bd9Sstevel@tonic-gate F_C, /* processor utilization (obsolete) */
1177c478bd9Sstevel@tonic-gate F_PCPU, /* percent of recently used cpu time */
1187c478bd9Sstevel@tonic-gate F_PMEM, /* percent of physical memory used (rss) */
1197c478bd9Sstevel@tonic-gate F_OSZ, /* virtual size of the process in pages */
1207c478bd9Sstevel@tonic-gate F_VSZ, /* virtual size of the process in kilobytes */
1217c478bd9Sstevel@tonic-gate F_RSS, /* resident set size of the process in kilobytes */
1227c478bd9Sstevel@tonic-gate F_NICE, /* "nice" value of the process */
1237c478bd9Sstevel@tonic-gate F_CLASS, /* scheduler class */
1247c478bd9Sstevel@tonic-gate F_STIME, /* start time of the process, hh:mm:ss or Month Day */
1257c478bd9Sstevel@tonic-gate F_ETIME, /* elapsed time of the process, [[dd-]hh:]mm:ss */
1267c478bd9Sstevel@tonic-gate F_TIME, /* cpu time of the process, [[dd-]hh:]mm:ss */
1277c478bd9Sstevel@tonic-gate F_TTY, /* name of the controlling terminal */
1287c478bd9Sstevel@tonic-gate F_ADDR, /* address of the process (obsolete) */
1297c478bd9Sstevel@tonic-gate F_WCHAN, /* wait channel (sleep condition variable) */
1307c478bd9Sstevel@tonic-gate F_FNAME, /* file name of command */
1317c478bd9Sstevel@tonic-gate F_COMM, /* name of command (argv[0] value) */
1327c478bd9Sstevel@tonic-gate F_ARGS, /* name of command plus all its arguments */
1337c478bd9Sstevel@tonic-gate F_TASKID, /* task id */
1347c478bd9Sstevel@tonic-gate F_PROJID, /* project id */
1357c478bd9Sstevel@tonic-gate F_PROJECT, /* project name of the process */
1367c478bd9Sstevel@tonic-gate F_PSET, /* bound processor set */
1377c478bd9Sstevel@tonic-gate F_ZONE, /* zone name */
1387c478bd9Sstevel@tonic-gate F_ZONEID, /* zone id */
139c6402783Sakolb F_CTID, /* process contract id */
140eaaa4d5fSBryan Cantrill F_LGRP, /* process home lgroup */
141eaaa4d5fSBryan Cantrill F_DMODEL /* process data model */
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate struct field {
1457c478bd9Sstevel@tonic-gate struct field *next; /* linked list */
1467c478bd9Sstevel@tonic-gate int fname; /* field index */
1477c478bd9Sstevel@tonic-gate const char *header; /* header to use */
1487c478bd9Sstevel@tonic-gate int width; /* width of field */
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate static struct field *fields = NULL; /* fields selected via -o */
1527c478bd9Sstevel@tonic-gate static struct field *last_field = NULL;
1537c478bd9Sstevel@tonic-gate static int do_header = 0;
1547c478bd9Sstevel@tonic-gate static struct timeval now;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /* array of defined fields, in fname order */
1577c478bd9Sstevel@tonic-gate struct def_field {
1587c478bd9Sstevel@tonic-gate const char *fname;
1597c478bd9Sstevel@tonic-gate const char *header;
1607c478bd9Sstevel@tonic-gate int width;
1617c478bd9Sstevel@tonic-gate int minwidth;
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate static struct def_field fname[] = {
1657c478bd9Sstevel@tonic-gate /* fname header width minwidth */
1667c478bd9Sstevel@tonic-gate { "user", "USER", 8, 8 },
1677c478bd9Sstevel@tonic-gate { "ruser", "RUSER", 8, 8 },
1687c478bd9Sstevel@tonic-gate { "group", "GROUP", 8, 8 },
1697c478bd9Sstevel@tonic-gate { "rgroup", "RGROUP", 8, 8 },
1707c478bd9Sstevel@tonic-gate { "uid", "UID", 5, 5 },
1717c478bd9Sstevel@tonic-gate { "ruid", "RUID", 5, 5 },
1727c478bd9Sstevel@tonic-gate { "gid", "GID", 5, 5 },
1737c478bd9Sstevel@tonic-gate { "rgid", "RGID", 5, 5 },
1747c478bd9Sstevel@tonic-gate { "pid", "PID", 5, 5 },
1757c478bd9Sstevel@tonic-gate { "ppid", "PPID", 5, 5 },
1767c478bd9Sstevel@tonic-gate { "pgid", "PGID", 5, 5 },
1777c478bd9Sstevel@tonic-gate { "sid", "SID", 5, 5 },
1787c478bd9Sstevel@tonic-gate { "psr", "PSR", 3, 2 },
1797c478bd9Sstevel@tonic-gate { "lwp", "LWP", 6, 2 },
1807c478bd9Sstevel@tonic-gate { "nlwp", "NLWP", 4, 2 },
1817c478bd9Sstevel@tonic-gate { "opri", "PRI", 3, 2 },
1827c478bd9Sstevel@tonic-gate { "pri", "PRI", 3, 2 },
1837c478bd9Sstevel@tonic-gate { "f", "F", 2, 2 },
1847c478bd9Sstevel@tonic-gate { "s", "S", 1, 1 },
1857c478bd9Sstevel@tonic-gate { "c", "C", 2, 2 },
1867c478bd9Sstevel@tonic-gate { "pcpu", "%CPU", 4, 4 },
1877c478bd9Sstevel@tonic-gate { "pmem", "%MEM", 4, 4 },
1887c478bd9Sstevel@tonic-gate { "osz", "SZ", 4, 4 },
1897c478bd9Sstevel@tonic-gate { "vsz", "VSZ", 4, 4 },
1907c478bd9Sstevel@tonic-gate { "rss", "RSS", 4, 4 },
1917c478bd9Sstevel@tonic-gate { "nice", "NI", 2, 2 },
1927c478bd9Sstevel@tonic-gate { "class", "CLS", 4, 2 },
1937c478bd9Sstevel@tonic-gate { "stime", "STIME", 8, 8 },
1947c478bd9Sstevel@tonic-gate { "etime", "ELAPSED", 11, 7 },
1957c478bd9Sstevel@tonic-gate { "time", "TIME", 11, 5 },
1967c478bd9Sstevel@tonic-gate { "tty", "TT", 7, 7 },
1977c478bd9Sstevel@tonic-gate #ifdef _LP64
1987c478bd9Sstevel@tonic-gate { "addr", "ADDR", 16, 8 },
1997c478bd9Sstevel@tonic-gate { "wchan", "WCHAN", 16, 8 },
2007c478bd9Sstevel@tonic-gate #else
2017c478bd9Sstevel@tonic-gate { "addr", "ADDR", 8, 8 },
2027c478bd9Sstevel@tonic-gate { "wchan", "WCHAN", 8, 8 },
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate { "fname", "COMMAND", 8, 8 },
2057c478bd9Sstevel@tonic-gate { "comm", "COMMAND", 80, 8 },
2067c478bd9Sstevel@tonic-gate { "args", "COMMAND", 80, 80 },
2077c478bd9Sstevel@tonic-gate { "taskid", "TASKID", 5, 5 },
2087c478bd9Sstevel@tonic-gate { "projid", "PROJID", 5, 5 },
2097c478bd9Sstevel@tonic-gate { "project", "PROJECT", 8, 8 },
2107c478bd9Sstevel@tonic-gate { "pset", "PSET", 3, 3 },
2117c478bd9Sstevel@tonic-gate { "zone", "ZONE", 8, 8 },
2127c478bd9Sstevel@tonic-gate { "zoneid", "ZONEID", 5, 5 },
2137c478bd9Sstevel@tonic-gate { "ctid", "CTID", 5, 5 },
214c6402783Sakolb { "lgrp", "LGRP", 4, 2 },
215eaaa4d5fSBryan Cantrill { "dmodel", "DMODEL", 6, 6 },
2167c478bd9Sstevel@tonic-gate };
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate #define NFIELDS (sizeof (fname) / sizeof (fname[0]))
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate static int retcode = 1;
2217c478bd9Sstevel@tonic-gate static int lflg;
2227c478bd9Sstevel@tonic-gate static int Aflg;
2237c478bd9Sstevel@tonic-gate static int uflg;
2247c478bd9Sstevel@tonic-gate static int Uflg;
2257c478bd9Sstevel@tonic-gate static int Gflg;
2267c478bd9Sstevel@tonic-gate static int aflg;
2277c478bd9Sstevel@tonic-gate static int dflg;
2287c478bd9Sstevel@tonic-gate static int Lflg;
2297c478bd9Sstevel@tonic-gate static int Pflg;
2300a1278f2SGary Mills static int Wflg;
2317c478bd9Sstevel@tonic-gate static int yflg;
2327c478bd9Sstevel@tonic-gate static int pflg;
2337c478bd9Sstevel@tonic-gate static int fflg;
2347c478bd9Sstevel@tonic-gate static int cflg;
2357c478bd9Sstevel@tonic-gate static int jflg;
2367c478bd9Sstevel@tonic-gate static int gflg;
2377c478bd9Sstevel@tonic-gate static int sflg;
2387c478bd9Sstevel@tonic-gate static int tflg;
2397c478bd9Sstevel@tonic-gate static int zflg;
2407c478bd9Sstevel@tonic-gate static int Zflg;
241c6402783Sakolb static int hflg;
242c6402783Sakolb static int Hflg;
243f48205beScasper static uid_t tuid = (uid_t)-1;
2447c478bd9Sstevel@tonic-gate static int errflg;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate static int ndev; /* number of devices */
2477c478bd9Sstevel@tonic-gate static int maxdev; /* number of devl structures allocated */
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate #define DNINCR 100
2507c478bd9Sstevel@tonic-gate #define DNSIZE 14
2517c478bd9Sstevel@tonic-gate static struct devl { /* device list */
2527c478bd9Sstevel@tonic-gate char dname[DNSIZE]; /* device name */
2537c478bd9Sstevel@tonic-gate dev_t ddev; /* device number */
2547c478bd9Sstevel@tonic-gate } *devl;
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate static struct tty {
2577c478bd9Sstevel@tonic-gate char *tname;
2587c478bd9Sstevel@tonic-gate dev_t tdev;
2597c478bd9Sstevel@tonic-gate } *tty = NULL; /* for t option */
2607c478bd9Sstevel@tonic-gate static size_t ttysz = 0;
2617c478bd9Sstevel@tonic-gate static int ntty = 0;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate static pid_t *pid = NULL; /* for p option */
2647c478bd9Sstevel@tonic-gate static size_t pidsz = 0;
2657c478bd9Sstevel@tonic-gate static size_t npid = 0;
2667c478bd9Sstevel@tonic-gate
267c6402783Sakolb static int *lgrps = NULL; /* list of lgroup IDs for for h option */
268c6402783Sakolb static size_t lgrps_size = 0; /* size of the lgrps list */
269c6402783Sakolb static size_t nlgrps = 0; /* number elements in the list */
270c6402783Sakolb
271c6402783Sakolb /* Maximum possible lgroup ID value */
272c6402783Sakolb #define MAX_LGRP_ID 256
273c6402783Sakolb
2747c478bd9Sstevel@tonic-gate static pid_t *grpid = NULL; /* for g option */
2757c478bd9Sstevel@tonic-gate static size_t grpidsz = 0;
2767c478bd9Sstevel@tonic-gate static int ngrpid = 0;
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate static pid_t *sessid = NULL; /* for s option */
2797c478bd9Sstevel@tonic-gate static size_t sessidsz = 0;
2807c478bd9Sstevel@tonic-gate static int nsessid = 0;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate static zoneid_t *zoneid = NULL; /* for z option */
2837c478bd9Sstevel@tonic-gate static size_t zoneidsz = 0;
2847c478bd9Sstevel@tonic-gate static int nzoneid = 0;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate static int kbytes_per_page;
2877c478bd9Sstevel@tonic-gate static int pidwidth;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate static char *procdir = "/proc"; /* standard /proc directory */
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate static struct ughead euid_tbl; /* table to store selected euid's */
2927c478bd9Sstevel@tonic-gate static struct ughead ruid_tbl; /* table to store selected real uid's */
2937c478bd9Sstevel@tonic-gate static struct ughead egid_tbl; /* table to store selected egid's */
2947c478bd9Sstevel@tonic-gate static struct ughead rgid_tbl; /* table to store selected real gid's */
2957c478bd9Sstevel@tonic-gate static prheader_t *lpsinfobuf; /* buffer to contain lpsinfo */
2967c478bd9Sstevel@tonic-gate static size_t lpbufsize;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * This constant defines the sentinal number of process IDs below which we
3007c478bd9Sstevel@tonic-gate * only examine individual entries in /proc rather than scanning through
3017c478bd9Sstevel@tonic-gate * /proc. This optimization is a huge win in the common case.
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate #define PTHRESHOLD 40
3047c478bd9Sstevel@tonic-gate
305f5c9e9f9SCasper H.S. Dik #define UCB_OPTS "-aceglnrtuvwxSU"
306f5c9e9f9SCasper H.S. Dik
3077c478bd9Sstevel@tonic-gate static void usage(void);
3087c478bd9Sstevel@tonic-gate static char *getarg(char **);
3097c478bd9Sstevel@tonic-gate static char *parse_format(char *);
3107c478bd9Sstevel@tonic-gate static char *gettty(psinfo_t *);
3117c478bd9Sstevel@tonic-gate static int prfind(int, psinfo_t *, char **);
3127c478bd9Sstevel@tonic-gate static void prcom(psinfo_t *, char *);
3137c478bd9Sstevel@tonic-gate static void prtpct(ushort_t, int);
3147c478bd9Sstevel@tonic-gate static void print_time(time_t, int);
3157c478bd9Sstevel@tonic-gate static void print_field(psinfo_t *, struct field *, const char *);
3167c478bd9Sstevel@tonic-gate static void print_zombie_field(psinfo_t *, struct field *, const char *);
3177c478bd9Sstevel@tonic-gate static void pr_fields(psinfo_t *, const char *,
3187c478bd9Sstevel@tonic-gate void (*print_fld)(psinfo_t *, struct field *, const char *));
3197c478bd9Sstevel@tonic-gate static int search(pid_t *, int, pid_t);
3207c478bd9Sstevel@tonic-gate static void add_ugentry(struct ughead *, char *);
3217c478bd9Sstevel@tonic-gate static int uconv(struct ughead *);
3227c478bd9Sstevel@tonic-gate static int gconv(struct ughead *);
323f48205beScasper static int ugfind(id_t, struct ughead *);
3247c478bd9Sstevel@tonic-gate static void prtime(timestruc_t, int, int);
3257c478bd9Sstevel@tonic-gate static void przom(psinfo_t *);
3267c478bd9Sstevel@tonic-gate static int namencnt(char *, int, int);
3277c478bd9Sstevel@tonic-gate static char *err_string(int);
3287c478bd9Sstevel@tonic-gate static int print_proc(char *pname);
3297c478bd9Sstevel@tonic-gate static time_t delta_secs(const timestruc_t *);
3307c478bd9Sstevel@tonic-gate static int str2id(const char *, pid_t *, long, long);
331f48205beScasper static int str2uid(const char *, uid_t *, unsigned long, unsigned long);
3327c478bd9Sstevel@tonic-gate static void *Realloc(void *, size_t);
3337c478bd9Sstevel@tonic-gate static int pidcmp(const void *p1, const void *p2);
3347c478bd9Sstevel@tonic-gate
335f5c9e9f9SCasper H.S. Dik extern int ucbmain(int, char **);
336f5c9e9f9SCasper H.S. Dik static int stdmain(int, char **);
337f5c9e9f9SCasper H.S. Dik
3387c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)3397c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3407c478bd9Sstevel@tonic-gate {
341f5c9e9f9SCasper H.S. Dik const char *me;
342f5c9e9f9SCasper H.S. Dik
343f5c9e9f9SCasper H.S. Dik /*
344f5c9e9f9SCasper H.S. Dik * The original two ps'es are linked in a single binary;
345f5c9e9f9SCasper H.S. Dik * their main()s are renamed to stdmain for /usr/bin/ps and
346f5c9e9f9SCasper H.S. Dik * ucbmain for /usr/ucb/ps.
347f5c9e9f9SCasper H.S. Dik * We try to figure out which instance of ps the user wants to run.
348f5c9e9f9SCasper H.S. Dik * Traditionally, the UCB variant doesn't require the flag argument
349f5c9e9f9SCasper H.S. Dik * start with a "-". If the first argument doesn't start with a
350f5c9e9f9SCasper H.S. Dik * "-", we call "ucbmain".
351f5c9e9f9SCasper H.S. Dik * If there's a first argument and it starts with a "-", we check
352f5c9e9f9SCasper H.S. Dik * whether any of the options isn't acceptable to "ucbmain"; in that
353f5c9e9f9SCasper H.S. Dik * case we run "stdmain".
354f5c9e9f9SCasper H.S. Dik * If we can't tell from the options which main to call, we check
355f5c9e9f9SCasper H.S. Dik * the binary we are running. We default to "stdmain" but
356f5c9e9f9SCasper H.S. Dik * any mention in the executable name of "ucb" causes us to call
357f5c9e9f9SCasper H.S. Dik * ucbmain.
358f5c9e9f9SCasper H.S. Dik */
359f5c9e9f9SCasper H.S. Dik if (argv[1] != NULL) {
360f5c9e9f9SCasper H.S. Dik if (argv[1][0] != '-')
361f5c9e9f9SCasper H.S. Dik return (ucbmain(argc, argv));
362f5c9e9f9SCasper H.S. Dik else if (argv[1][strspn(argv[1], UCB_OPTS)] != '\0')
363f5c9e9f9SCasper H.S. Dik return (stdmain(argc, argv));
364f5c9e9f9SCasper H.S. Dik }
365f5c9e9f9SCasper H.S. Dik
366f5c9e9f9SCasper H.S. Dik me = getexecname();
367f5c9e9f9SCasper H.S. Dik
368f5c9e9f9SCasper H.S. Dik if (me != NULL && strstr(me, "ucb") != NULL)
369f5c9e9f9SCasper H.S. Dik return (ucbmain(argc, argv));
370f5c9e9f9SCasper H.S. Dik else
371f5c9e9f9SCasper H.S. Dik return (stdmain(argc, argv));
372f5c9e9f9SCasper H.S. Dik }
373f5c9e9f9SCasper H.S. Dik
374f5c9e9f9SCasper H.S. Dik static int
stdmain(int argc,char ** argv)375f5c9e9f9SCasper H.S. Dik stdmain(int argc, char **argv)
376f5c9e9f9SCasper H.S. Dik {
3777c478bd9Sstevel@tonic-gate char *p;
3787c478bd9Sstevel@tonic-gate char *p1;
3797c478bd9Sstevel@tonic-gate char *parg;
3807c478bd9Sstevel@tonic-gate int c;
3817c478bd9Sstevel@tonic-gate int i;
3827c478bd9Sstevel@tonic-gate int pgerrflg = 0; /* err flg: non-numeric arg w/p & g options */
38316f94f58Ssayama size_t size, len;
3847c478bd9Sstevel@tonic-gate DIR *dirp;
3857c478bd9Sstevel@tonic-gate struct dirent *dentp;
3867c478bd9Sstevel@tonic-gate pid_t maxpid;
3877c478bd9Sstevel@tonic-gate pid_t id;
3887c478bd9Sstevel@tonic-gate int ret;
38916f94f58Ssayama char loc_stime_str[32];
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
3927c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
3937c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
3947c478bd9Sstevel@tonic-gate #endif
3957c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate (void) memset(&euid_tbl, 0, sizeof (euid_tbl));
3987c478bd9Sstevel@tonic-gate (void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
3997c478bd9Sstevel@tonic-gate (void) memset(&egid_tbl, 0, sizeof (egid_tbl));
4007c478bd9Sstevel@tonic-gate (void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * calculate width of pid fields based on configured MAXPID
4087c478bd9Sstevel@tonic-gate * (must be at least 5 to retain output format compatibility)
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate id = maxpid = (pid_t)sysconf(_SC_MAXPID);
4117c478bd9Sstevel@tonic-gate pidwidth = 1;
4127c478bd9Sstevel@tonic-gate while ((id /= 10) > 0)
4137c478bd9Sstevel@tonic-gate ++pidwidth;
4147c478bd9Sstevel@tonic-gate pidwidth = pidwidth < 5 ? 5 : pidwidth;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate fname[F_PID].width = fname[F_PPID].width = pidwidth;
4177c478bd9Sstevel@tonic-gate fname[F_PGID].width = fname[F_SID].width = pidwidth;
4187c478bd9Sstevel@tonic-gate
41916f94f58Ssayama /*
42016f94f58Ssayama * TRANSLATION_NOTE
42116f94f58Ssayama * Specify the printf format with width and precision for
42216f94f58Ssayama * the STIME field.
42316f94f58Ssayama */
42416f94f58Ssayama len = snprintf(loc_stime_str, sizeof (loc_stime_str),
42516f94f58Ssayama dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
42616f94f58Ssayama if (len >= sizeof (loc_stime_str))
42716f94f58Ssayama len = sizeof (loc_stime_str) - 1;
42816f94f58Ssayama
42916f94f58Ssayama fname[F_STIME].width = fname[F_STIME].minwidth = len;
43016f94f58Ssayama
4310a1278f2SGary Mills while ((c = getopt(argc, argv, "jlfceAadLPWyZHh:t:p:g:u:U:G:n:s:o:z:"))
432c6402783Sakolb != EOF)
4337c478bd9Sstevel@tonic-gate switch (c) {
434c6402783Sakolb case 'H': /* Show home lgroups */
435c6402783Sakolb Hflg++;
436c6402783Sakolb break;
437c6402783Sakolb case 'h':
438c6402783Sakolb /*
439c6402783Sakolb * Show processes/threads with given home lgroups
440c6402783Sakolb */
441c6402783Sakolb hflg++;
442c6402783Sakolb p1 = optarg;
443c6402783Sakolb do {
444c6402783Sakolb int id;
445c6402783Sakolb
446c6402783Sakolb /*
447c6402783Sakolb * Get all IDs in the list, verify for
448c6402783Sakolb * correctness and place in lgrps array.
449c6402783Sakolb */
450c6402783Sakolb parg = getarg(&p1);
451c6402783Sakolb /* Convert string to integer */
452c6402783Sakolb ret = str2id(parg, (pid_t *)&id, 0,
453c6402783Sakolb MAX_LGRP_ID);
454c6402783Sakolb /* Complain if ID didn't parse correctly */
455c6402783Sakolb if (ret != 0) {
456c6402783Sakolb pgerrflg++;
457c6402783Sakolb (void) fprintf(stderr,
458c6402783Sakolb gettext("ps: %s "), parg);
459c6402783Sakolb if (ret == EINVAL)
460c6402783Sakolb (void) fprintf(stderr,
461c6402783Sakolb gettext("is an invalid "
462c6402783Sakolb "non-numeric argument"));
463c6402783Sakolb else
464c6402783Sakolb (void) fprintf(stderr,
465c6402783Sakolb gettext("exceeds valid "
466c6402783Sakolb "range"));
467c6402783Sakolb (void) fprintf(stderr,
468c6402783Sakolb gettext(" for -h option\n"));
469c6402783Sakolb continue;
470c6402783Sakolb }
471c6402783Sakolb
472c6402783Sakolb /* Extend lgrps array if needed */
473c6402783Sakolb if (nlgrps == lgrps_size) {
474c6402783Sakolb /* Double the size of the lgrps array */
475c6402783Sakolb if (lgrps_size == 0)
476c6402783Sakolb lgrps_size = SIZ;
477c6402783Sakolb lgrps_size *= 2;
478c6402783Sakolb lgrps = Realloc(lgrps,
479c6402783Sakolb lgrps_size * sizeof (int));
480c6402783Sakolb }
481c6402783Sakolb /* place the id in the lgrps table */
482c6402783Sakolb lgrps[nlgrps++] = id;
483c6402783Sakolb } while (*p1);
484c6402783Sakolb break;
4857c478bd9Sstevel@tonic-gate case 'l': /* long listing */
4867c478bd9Sstevel@tonic-gate lflg++;
4877c478bd9Sstevel@tonic-gate break;
4887c478bd9Sstevel@tonic-gate case 'f': /* full listing */
4897c478bd9Sstevel@tonic-gate fflg++;
4907c478bd9Sstevel@tonic-gate break;
4917c478bd9Sstevel@tonic-gate case 'j':
4927c478bd9Sstevel@tonic-gate jflg++;
4937c478bd9Sstevel@tonic-gate break;
4947c478bd9Sstevel@tonic-gate case 'c':
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate * Format output to reflect scheduler changes:
4977c478bd9Sstevel@tonic-gate * high numbers for high priorities and don't
4987c478bd9Sstevel@tonic-gate * print nice or p_cpu values. 'c' option only
4997c478bd9Sstevel@tonic-gate * effective when used with 'l' or 'f' options.
5007c478bd9Sstevel@tonic-gate */
5017c478bd9Sstevel@tonic-gate cflg++;
5027c478bd9Sstevel@tonic-gate break;
5037c478bd9Sstevel@tonic-gate case 'A': /* list every process */
5047c478bd9Sstevel@tonic-gate case 'e': /* (obsolete) list every process */
5057c478bd9Sstevel@tonic-gate Aflg++;
5067c478bd9Sstevel@tonic-gate tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
507c6402783Sakolb zflg = hflg = 0;
5087c478bd9Sstevel@tonic-gate break;
5097c478bd9Sstevel@tonic-gate case 'a':
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate * Same as 'e' except no session group leaders
5127c478bd9Sstevel@tonic-gate * and no non-terminal processes.
5137c478bd9Sstevel@tonic-gate */
5147c478bd9Sstevel@tonic-gate aflg++;
5157c478bd9Sstevel@tonic-gate break;
5167c478bd9Sstevel@tonic-gate case 'd': /* same as e except no session leaders */
5177c478bd9Sstevel@tonic-gate dflg++;
5187c478bd9Sstevel@tonic-gate break;
5197c478bd9Sstevel@tonic-gate case 'L': /* show lwps */
5207c478bd9Sstevel@tonic-gate Lflg++;
5217c478bd9Sstevel@tonic-gate break;
5227c478bd9Sstevel@tonic-gate case 'P': /* show bound processor */
5237c478bd9Sstevel@tonic-gate Pflg++;
5247c478bd9Sstevel@tonic-gate break;
5250a1278f2SGary Mills case 'W': /* truncate long names */
5260a1278f2SGary Mills Wflg++;
5270a1278f2SGary Mills break;
5287c478bd9Sstevel@tonic-gate case 'y': /* omit F & ADDR, report RSS & SZ in Kby */
5297c478bd9Sstevel@tonic-gate yflg++;
5307c478bd9Sstevel@tonic-gate break;
5317c478bd9Sstevel@tonic-gate case 'n': /* no longer needed; retain as no-op */
5327c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5337c478bd9Sstevel@tonic-gate gettext("ps: warning: -n option ignored\n"));
5347c478bd9Sstevel@tonic-gate break;
5357c478bd9Sstevel@tonic-gate case 't': /* terminals */
5367c478bd9Sstevel@tonic-gate #define TSZ 30
5377c478bd9Sstevel@tonic-gate tflg++;
5387c478bd9Sstevel@tonic-gate p1 = optarg;
5397c478bd9Sstevel@tonic-gate do {
5407c478bd9Sstevel@tonic-gate char nambuf[TSZ+6]; /* for "/dev/" + '\0' */
5417c478bd9Sstevel@tonic-gate struct stat64 s;
5427c478bd9Sstevel@tonic-gate parg = getarg(&p1);
5437c478bd9Sstevel@tonic-gate p = Realloc(NULL, TSZ+1); /* for '\0' */
5447c478bd9Sstevel@tonic-gate /* zero the buffer before using it */
5457c478bd9Sstevel@tonic-gate p[0] = '\0';
5467c478bd9Sstevel@tonic-gate size = TSZ;
5477c478bd9Sstevel@tonic-gate if (isdigit(*parg)) {
5487c478bd9Sstevel@tonic-gate (void) strcpy(p, "tty");
5497c478bd9Sstevel@tonic-gate size -= 3;
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate (void) strncat(p, parg, size);
5527c478bd9Sstevel@tonic-gate if (ntty == ttysz) {
5537c478bd9Sstevel@tonic-gate if ((ttysz *= 2) == 0)
5547c478bd9Sstevel@tonic-gate ttysz = NTTYS;
5557c478bd9Sstevel@tonic-gate tty = Realloc(tty,
5567c478bd9Sstevel@tonic-gate (ttysz + 1) * sizeof (struct tty));
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate tty[ntty].tdev = PRNODEV;
5597c478bd9Sstevel@tonic-gate (void) strcpy(nambuf, "/dev/");
5607c478bd9Sstevel@tonic-gate (void) strcat(nambuf, p);
5617c478bd9Sstevel@tonic-gate if (stat64(nambuf, &s) == 0)
5627c478bd9Sstevel@tonic-gate tty[ntty].tdev = s.st_rdev;
5637c478bd9Sstevel@tonic-gate tty[ntty++].tname = p;
5647c478bd9Sstevel@tonic-gate } while (*p1);
5657c478bd9Sstevel@tonic-gate break;
5667c478bd9Sstevel@tonic-gate case 'p': /* proc ids */
5677c478bd9Sstevel@tonic-gate pflg++;
5687c478bd9Sstevel@tonic-gate p1 = optarg;
5697c478bd9Sstevel@tonic-gate do {
5707c478bd9Sstevel@tonic-gate pid_t id;
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate parg = getarg(&p1);
5737c478bd9Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5747c478bd9Sstevel@tonic-gate pgerrflg++;
5757c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5767c478bd9Sstevel@tonic-gate gettext("ps: %s "), parg);
5777c478bd9Sstevel@tonic-gate if (ret == EINVAL)
5787c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5797c478bd9Sstevel@tonic-gate gettext("is an invalid "
5807c478bd9Sstevel@tonic-gate "non-numeric argument"));
5817c478bd9Sstevel@tonic-gate else
5827c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5837c478bd9Sstevel@tonic-gate gettext("exceeds valid "
5847c478bd9Sstevel@tonic-gate "range"));
5857c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5867c478bd9Sstevel@tonic-gate gettext(" for -p option\n"));
5877c478bd9Sstevel@tonic-gate continue;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate if (npid == pidsz) {
5917c478bd9Sstevel@tonic-gate if ((pidsz *= 2) == 0)
5927c478bd9Sstevel@tonic-gate pidsz = SIZ;
5937c478bd9Sstevel@tonic-gate pid = Realloc(pid,
5947c478bd9Sstevel@tonic-gate pidsz * sizeof (pid_t));
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate pid[npid++] = id;
5977c478bd9Sstevel@tonic-gate } while (*p1);
5987c478bd9Sstevel@tonic-gate break;
5997c478bd9Sstevel@tonic-gate case 's': /* session */
6007c478bd9Sstevel@tonic-gate sflg++;
6017c478bd9Sstevel@tonic-gate p1 = optarg;
6027c478bd9Sstevel@tonic-gate do {
6037c478bd9Sstevel@tonic-gate pid_t id;
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate parg = getarg(&p1);
6067c478bd9Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
6077c478bd9Sstevel@tonic-gate pgerrflg++;
6087c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6097c478bd9Sstevel@tonic-gate gettext("ps: %s "), parg);
6107c478bd9Sstevel@tonic-gate if (ret == EINVAL)
6117c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6127c478bd9Sstevel@tonic-gate gettext("is an invalid "
6137c478bd9Sstevel@tonic-gate "non-numeric argument"));
6147c478bd9Sstevel@tonic-gate else
6157c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6167c478bd9Sstevel@tonic-gate gettext("exceeds valid "
6177c478bd9Sstevel@tonic-gate "range"));
6187c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6197c478bd9Sstevel@tonic-gate gettext(" for -s option\n"));
6207c478bd9Sstevel@tonic-gate continue;
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate if (nsessid == sessidsz) {
6247c478bd9Sstevel@tonic-gate if ((sessidsz *= 2) == 0)
6257c478bd9Sstevel@tonic-gate sessidsz = SIZ;
6267c478bd9Sstevel@tonic-gate sessid = Realloc(sessid,
6277c478bd9Sstevel@tonic-gate sessidsz * sizeof (pid_t));
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate sessid[nsessid++] = id;
6307c478bd9Sstevel@tonic-gate } while (*p1);
6317c478bd9Sstevel@tonic-gate break;
6327c478bd9Sstevel@tonic-gate case 'g': /* proc group */
6337c478bd9Sstevel@tonic-gate gflg++;
6347c478bd9Sstevel@tonic-gate p1 = optarg;
6357c478bd9Sstevel@tonic-gate do {
6367c478bd9Sstevel@tonic-gate pid_t id;
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate parg = getarg(&p1);
6397c478bd9Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
6407c478bd9Sstevel@tonic-gate pgerrflg++;
6417c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6427c478bd9Sstevel@tonic-gate gettext("ps: %s "), parg);
6437c478bd9Sstevel@tonic-gate if (ret == EINVAL)
6447c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6457c478bd9Sstevel@tonic-gate gettext("is an invalid "
6467c478bd9Sstevel@tonic-gate "non-numeric argument"));
6477c478bd9Sstevel@tonic-gate else
6487c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6497c478bd9Sstevel@tonic-gate gettext("exceeds valid "
6507c478bd9Sstevel@tonic-gate "range"));
6517c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6527c478bd9Sstevel@tonic-gate gettext(" for -g option\n"));
6537c478bd9Sstevel@tonic-gate continue;
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate if (ngrpid == grpidsz) {
6577c478bd9Sstevel@tonic-gate if ((grpidsz *= 2) == 0)
6587c478bd9Sstevel@tonic-gate grpidsz = SIZ;
6597c478bd9Sstevel@tonic-gate grpid = Realloc(grpid,
6607c478bd9Sstevel@tonic-gate grpidsz * sizeof (pid_t));
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate grpid[ngrpid++] = id;
6637c478bd9Sstevel@tonic-gate } while (*p1);
6647c478bd9Sstevel@tonic-gate break;
6657c478bd9Sstevel@tonic-gate case 'u': /* effective user name or number */
6667c478bd9Sstevel@tonic-gate uflg++;
6677c478bd9Sstevel@tonic-gate p1 = optarg;
6687c478bd9Sstevel@tonic-gate do {
6697c478bd9Sstevel@tonic-gate parg = getarg(&p1);
6707c478bd9Sstevel@tonic-gate add_ugentry(&euid_tbl, parg);
6717c478bd9Sstevel@tonic-gate } while (*p1);
6727c478bd9Sstevel@tonic-gate break;
6737c478bd9Sstevel@tonic-gate case 'U': /* real user name or number */
6747c478bd9Sstevel@tonic-gate Uflg++;
6757c478bd9Sstevel@tonic-gate p1 = optarg;
6767c478bd9Sstevel@tonic-gate do {
6777c478bd9Sstevel@tonic-gate parg = getarg(&p1);
6787c478bd9Sstevel@tonic-gate add_ugentry(&ruid_tbl, parg);
6797c478bd9Sstevel@tonic-gate } while (*p1);
6807c478bd9Sstevel@tonic-gate break;
6817c478bd9Sstevel@tonic-gate case 'G': /* real group name or number */
6827c478bd9Sstevel@tonic-gate Gflg++;
6837c478bd9Sstevel@tonic-gate p1 = optarg;
6847c478bd9Sstevel@tonic-gate do {
6857c478bd9Sstevel@tonic-gate parg = getarg(&p1);
6867c478bd9Sstevel@tonic-gate add_ugentry(&rgid_tbl, parg);
6877c478bd9Sstevel@tonic-gate } while (*p1);
6887c478bd9Sstevel@tonic-gate break;
6897c478bd9Sstevel@tonic-gate case 'o': /* output format */
6907c478bd9Sstevel@tonic-gate p = optarg;
6917c478bd9Sstevel@tonic-gate while ((p = parse_format(p)) != NULL)
6927c478bd9Sstevel@tonic-gate ;
6937c478bd9Sstevel@tonic-gate break;
6947c478bd9Sstevel@tonic-gate case 'z': /* zone name or number */
6957c478bd9Sstevel@tonic-gate zflg++;
6967c478bd9Sstevel@tonic-gate p1 = optarg;
6977c478bd9Sstevel@tonic-gate do {
6987c478bd9Sstevel@tonic-gate zoneid_t id;
6997c478bd9Sstevel@tonic-gate
7007c478bd9Sstevel@tonic-gate parg = getarg(&p1);
7017c478bd9Sstevel@tonic-gate if (zone_get_id(parg, &id) != 0) {
7027c478bd9Sstevel@tonic-gate pgerrflg++;
7037c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7047c478bd9Sstevel@tonic-gate gettext("ps: unknown zone %s\n"),
7057c478bd9Sstevel@tonic-gate parg);
7067c478bd9Sstevel@tonic-gate continue;
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate if (nzoneid == zoneidsz) {
7107c478bd9Sstevel@tonic-gate if ((zoneidsz *= 2) == 0)
7117c478bd9Sstevel@tonic-gate zoneidsz = SIZ;
7127c478bd9Sstevel@tonic-gate zoneid = Realloc(zoneid,
7137c478bd9Sstevel@tonic-gate zoneidsz * sizeof (zoneid_t));
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate zoneid[nzoneid++] = id;
7167c478bd9Sstevel@tonic-gate } while (*p1);
7177c478bd9Sstevel@tonic-gate break;
7187c478bd9Sstevel@tonic-gate case 'Z': /* show zone name */
7197c478bd9Sstevel@tonic-gate Zflg++;
7207c478bd9Sstevel@tonic-gate break;
7217c478bd9Sstevel@tonic-gate default: /* error on ? */
7227c478bd9Sstevel@tonic-gate errflg++;
7237c478bd9Sstevel@tonic-gate break;
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate if (errflg || optind < argc || pgerrflg)
7277c478bd9Sstevel@tonic-gate usage();
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate if (tflg)
7307c478bd9Sstevel@tonic-gate tty[ntty].tname = NULL;
7317c478bd9Sstevel@tonic-gate /*
7327c478bd9Sstevel@tonic-gate * If an appropriate option has not been specified, use the
7337c478bd9Sstevel@tonic-gate * current terminal and effective uid as the default.
7347c478bd9Sstevel@tonic-gate */
735c6402783Sakolb if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
7367c478bd9Sstevel@tonic-gate psinfo_t info;
7377c478bd9Sstevel@tonic-gate int procfd;
7387c478bd9Sstevel@tonic-gate char *name;
7397c478bd9Sstevel@tonic-gate char pname[100];
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate /* get our own controlling tty name using /proc */
7427c478bd9Sstevel@tonic-gate (void) snprintf(pname, sizeof (pname),
7437c478bd9Sstevel@tonic-gate "%s/self/psinfo", procdir);
7447c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0 ||
7457c478bd9Sstevel@tonic-gate read(procfd, (char *)&info, sizeof (info)) < 0 ||
7467c478bd9Sstevel@tonic-gate info.pr_ttydev == PRNODEV) {
7477c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7487c478bd9Sstevel@tonic-gate gettext("ps: no controlling terminal\n"));
7497c478bd9Sstevel@tonic-gate exit(1);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate (void) close(procfd);
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate i = 0;
7547c478bd9Sstevel@tonic-gate name = gettty(&info);
7557c478bd9Sstevel@tonic-gate if (*name == '?') {
7567c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7577c478bd9Sstevel@tonic-gate gettext("ps: can't find controlling terminal\n"));
7587c478bd9Sstevel@tonic-gate exit(1);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate if (ntty == ttysz) {
7617c478bd9Sstevel@tonic-gate if ((ttysz *= 2) == 0)
7627c478bd9Sstevel@tonic-gate ttysz = NTTYS;
7637c478bd9Sstevel@tonic-gate tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate tty[ntty].tdev = info.pr_ttydev;
7667c478bd9Sstevel@tonic-gate tty[ntty++].tname = name;
7677c478bd9Sstevel@tonic-gate tty[ntty].tname = NULL;
7687c478bd9Sstevel@tonic-gate tflg++;
7697c478bd9Sstevel@tonic-gate tuid = getuid();
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate if (Aflg) {
7727c478bd9Sstevel@tonic-gate Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
773c6402783Sakolb zflg = hflg = 0;
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate if (Aflg | aflg | dflg)
7767c478bd9Sstevel@tonic-gate tflg = 0;
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate i = 0; /* prepare to exit on name lookup errors */
7797c478bd9Sstevel@tonic-gate i += uconv(&euid_tbl);
7807c478bd9Sstevel@tonic-gate i += uconv(&ruid_tbl);
7817c478bd9Sstevel@tonic-gate i += gconv(&egid_tbl);
7827c478bd9Sstevel@tonic-gate i += gconv(&rgid_tbl);
7837c478bd9Sstevel@tonic-gate if (i)
7847c478bd9Sstevel@tonic-gate exit(1);
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate /* allocate a buffer for lwpsinfo structures */
7877c478bd9Sstevel@tonic-gate lpbufsize = 4096;
7887c478bd9Sstevel@tonic-gate if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
7897c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7907c478bd9Sstevel@tonic-gate gettext("ps: no memory\n"));
7917c478bd9Sstevel@tonic-gate exit(1);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate if (fields) { /* print user-specified header */
7957c478bd9Sstevel@tonic-gate if (do_header) {
7967c478bd9Sstevel@tonic-gate struct field *f;
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate for (f = fields; f != NULL; f = f->next) {
7997c478bd9Sstevel@tonic-gate if (f != fields)
8007c478bd9Sstevel@tonic-gate (void) printf(" ");
8017c478bd9Sstevel@tonic-gate switch (f->fname) {
8027c478bd9Sstevel@tonic-gate case F_TTY:
8037c478bd9Sstevel@tonic-gate (void) printf("%-*s",
8047c478bd9Sstevel@tonic-gate f->width, f->header);
8057c478bd9Sstevel@tonic-gate break;
8067c478bd9Sstevel@tonic-gate case F_FNAME:
8077c478bd9Sstevel@tonic-gate case F_COMM:
8087c478bd9Sstevel@tonic-gate case F_ARGS:
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate * Print these headers full width
8117c478bd9Sstevel@tonic-gate * unless they appear at the end.
8127c478bd9Sstevel@tonic-gate */
8137c478bd9Sstevel@tonic-gate if (f->next != NULL) {
8147c478bd9Sstevel@tonic-gate (void) printf("%-*s",
8157c478bd9Sstevel@tonic-gate f->width, f->header);
8167c478bd9Sstevel@tonic-gate } else {
8177c478bd9Sstevel@tonic-gate (void) printf("%s",
8187c478bd9Sstevel@tonic-gate f->header);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate break;
8217c478bd9Sstevel@tonic-gate default:
8227c478bd9Sstevel@tonic-gate (void) printf("%*s",
8237c478bd9Sstevel@tonic-gate f->width, f->header);
8247c478bd9Sstevel@tonic-gate break;
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate (void) printf("\n");
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate } else { /* print standard header */
830f5c9e9f9SCasper H.S. Dik /*
831f5c9e9f9SCasper H.S. Dik * All fields before 'PID' are printed with a trailing space
832f5c9e9f9SCasper H.S. Dik * as a separator and that is how we print the headers too.
833f5c9e9f9SCasper H.S. Dik */
8347c478bd9Sstevel@tonic-gate if (lflg) {
8357c478bd9Sstevel@tonic-gate if (yflg)
8367c478bd9Sstevel@tonic-gate (void) printf("S ");
8377c478bd9Sstevel@tonic-gate else
8387c478bd9Sstevel@tonic-gate (void) printf(" F S ");
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate if (Zflg)
8417c478bd9Sstevel@tonic-gate (void) printf(" ZONE ");
8427c478bd9Sstevel@tonic-gate if (fflg) {
8437c478bd9Sstevel@tonic-gate (void) printf(" UID ");
8447c478bd9Sstevel@tonic-gate } else if (lflg)
8457c478bd9Sstevel@tonic-gate (void) printf(" UID ");
8467c478bd9Sstevel@tonic-gate
8477c478bd9Sstevel@tonic-gate (void) printf("%*s", pidwidth, "PID");
8487c478bd9Sstevel@tonic-gate if (lflg || fflg)
8497c478bd9Sstevel@tonic-gate (void) printf(" %*s", pidwidth, "PPID");
8507c478bd9Sstevel@tonic-gate if (jflg)
8517c478bd9Sstevel@tonic-gate (void) printf(" %*s %*s", pidwidth, "PGID",
8527c478bd9Sstevel@tonic-gate pidwidth, "SID");
8537c478bd9Sstevel@tonic-gate if (Lflg)
8547c478bd9Sstevel@tonic-gate (void) printf(" LWP");
8557c478bd9Sstevel@tonic-gate if (Pflg)
8567c478bd9Sstevel@tonic-gate (void) printf(" PSR");
8577c478bd9Sstevel@tonic-gate if (Lflg && fflg)
8587c478bd9Sstevel@tonic-gate (void) printf(" NLWP");
8597c478bd9Sstevel@tonic-gate if (cflg)
8607c478bd9Sstevel@tonic-gate (void) printf(" CLS PRI");
8617c478bd9Sstevel@tonic-gate else if (lflg || fflg) {
8627c478bd9Sstevel@tonic-gate (void) printf(" C");
8637c478bd9Sstevel@tonic-gate if (lflg)
8647c478bd9Sstevel@tonic-gate (void) printf(" PRI NI");
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate if (lflg) {
8677c478bd9Sstevel@tonic-gate if (yflg)
8687c478bd9Sstevel@tonic-gate (void) printf(" RSS SZ WCHAN");
8697c478bd9Sstevel@tonic-gate else
8707c478bd9Sstevel@tonic-gate (void) printf(" ADDR SZ WCHAN");
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate if (fflg)
87316f94f58Ssayama (void) printf(" %s", loc_stime_str);
874c6402783Sakolb if (Hflg)
875c6402783Sakolb (void) printf(" LGRP");
8767c478bd9Sstevel@tonic-gate if (Lflg)
8777c478bd9Sstevel@tonic-gate (void) printf(" TTY LTIME CMD\n");
8787c478bd9Sstevel@tonic-gate else
8797c478bd9Sstevel@tonic-gate (void) printf(" TTY TIME CMD\n");
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate
883c6402783Sakolb if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
8847c478bd9Sstevel@tonic-gate npid <= PTHRESHOLD) {
8857c478bd9Sstevel@tonic-gate /*
8867c478bd9Sstevel@tonic-gate * If we are looking at specific processes go straight
8877c478bd9Sstevel@tonic-gate * to their /proc entries and don't scan /proc.
8887c478bd9Sstevel@tonic-gate */
8897c478bd9Sstevel@tonic-gate int i;
8907c478bd9Sstevel@tonic-gate
8917c478bd9Sstevel@tonic-gate (void) qsort(pid, npid, sizeof (pid_t), pidcmp);
8927c478bd9Sstevel@tonic-gate for (i = 0; i < npid; i++) {
8937c478bd9Sstevel@tonic-gate char pname[12];
8947c478bd9Sstevel@tonic-gate
8957c478bd9Sstevel@tonic-gate if (i >= 1 && pid[i] == pid[i - 1])
8967c478bd9Sstevel@tonic-gate continue;
8977c478bd9Sstevel@tonic-gate (void) sprintf(pname, "%d", (int)pid[i]);
8987c478bd9Sstevel@tonic-gate if (print_proc(pname) == 0)
8997c478bd9Sstevel@tonic-gate retcode = 0;
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate } else {
9027c478bd9Sstevel@tonic-gate /*
9037c478bd9Sstevel@tonic-gate * Determine which processes to print info about by searching
9047c478bd9Sstevel@tonic-gate * the /proc directory and looking at each process.
9057c478bd9Sstevel@tonic-gate */
9067c478bd9Sstevel@tonic-gate if ((dirp = opendir(procdir)) == NULL) {
9077c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9087c478bd9Sstevel@tonic-gate gettext("ps: cannot open PROC directory %s\n"),
9097c478bd9Sstevel@tonic-gate procdir);
9107c478bd9Sstevel@tonic-gate exit(1);
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate
9137c478bd9Sstevel@tonic-gate /* for each active process --- */
914*e503abb7SIgor Kozhukhov while ((dentp = readdir(dirp)) != NULL) {
9157c478bd9Sstevel@tonic-gate if (dentp->d_name[0] == '.') /* skip . and .. */
9167c478bd9Sstevel@tonic-gate continue;
9177c478bd9Sstevel@tonic-gate if (print_proc(dentp->d_name) == 0)
9187c478bd9Sstevel@tonic-gate retcode = 0;
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate (void) closedir(dirp);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate return (retcode);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate int
print_proc(char * pid_name)9287c478bd9Sstevel@tonic-gate print_proc(char *pid_name)
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate char pname[PATH_MAX];
9317c478bd9Sstevel@tonic-gate int pdlen;
9327c478bd9Sstevel@tonic-gate int found;
9337c478bd9Sstevel@tonic-gate int procfd; /* filedescriptor for /proc/nnnnn/psinfo */
9347c478bd9Sstevel@tonic-gate char *tp; /* ptr to ttyname, if any */
9357c478bd9Sstevel@tonic-gate psinfo_t info; /* process information from /proc */
9367c478bd9Sstevel@tonic-gate lwpsinfo_t *lwpsinfo; /* array of lwpsinfo structs */
9377c478bd9Sstevel@tonic-gate
9387c478bd9Sstevel@tonic-gate pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
9397c478bd9Sstevel@tonic-gate if (pdlen >= sizeof (pname) - 10)
9407c478bd9Sstevel@tonic-gate return (1);
9417c478bd9Sstevel@tonic-gate retry:
9427c478bd9Sstevel@tonic-gate (void) strcpy(&pname[pdlen], "psinfo");
9437c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) == -1) {
9447c478bd9Sstevel@tonic-gate /* Process may have exited meanwhile. */
9457c478bd9Sstevel@tonic-gate return (1);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate /*
9487c478bd9Sstevel@tonic-gate * Get the info structure for the process and close quickly.
9497c478bd9Sstevel@tonic-gate */
9507c478bd9Sstevel@tonic-gate if (read(procfd, (char *)&info, sizeof (info)) < 0) {
9517c478bd9Sstevel@tonic-gate int saverr = errno;
9527c478bd9Sstevel@tonic-gate
9537c478bd9Sstevel@tonic-gate (void) close(procfd);
9547c478bd9Sstevel@tonic-gate if (saverr == EAGAIN)
9557c478bd9Sstevel@tonic-gate goto retry;
9567c478bd9Sstevel@tonic-gate if (saverr != ENOENT)
9577c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9587c478bd9Sstevel@tonic-gate gettext("ps: read() on %s: %s\n"),
9597c478bd9Sstevel@tonic-gate pname, err_string(saverr));
9607c478bd9Sstevel@tonic-gate return (1);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate (void) close(procfd);
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate found = 0;
9657c478bd9Sstevel@tonic-gate if (info.pr_lwp.pr_state == 0) /* can't happen? */
9667c478bd9Sstevel@tonic-gate return (1);
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate * Omit session group leaders for 'a' and 'd' options.
9707c478bd9Sstevel@tonic-gate */
9717c478bd9Sstevel@tonic-gate if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
9727c478bd9Sstevel@tonic-gate return (1);
9737c478bd9Sstevel@tonic-gate if (Aflg || dflg)
9747c478bd9Sstevel@tonic-gate found++;
9757c478bd9Sstevel@tonic-gate else if (pflg && search(pid, npid, info.pr_pid))
9767c478bd9Sstevel@tonic-gate found++; /* ppid in p option arg list */
977f48205beScasper else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
9787c478bd9Sstevel@tonic-gate found++; /* puid in u option arg list */
979f48205beScasper else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
9807c478bd9Sstevel@tonic-gate found++; /* puid in U option arg list */
9817c478bd9Sstevel@tonic-gate #ifdef NOT_YET
982f48205beScasper else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
9837c478bd9Sstevel@tonic-gate found++; /* pgid in g option arg list */
9847c478bd9Sstevel@tonic-gate #endif /* NOT_YET */
985f48205beScasper else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
9867c478bd9Sstevel@tonic-gate found++; /* pgid in G option arg list */
9877c478bd9Sstevel@tonic-gate else if (gflg && search(grpid, ngrpid, info.pr_pgid))
9887c478bd9Sstevel@tonic-gate found++; /* grpid in g option arg list */
9897c478bd9Sstevel@tonic-gate else if (sflg && search(sessid, nsessid, info.pr_sid))
9907c478bd9Sstevel@tonic-gate found++; /* sessid in s option arg list */
9917c478bd9Sstevel@tonic-gate else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
9927c478bd9Sstevel@tonic-gate found++; /* zoneid in z option arg list */
993c6402783Sakolb else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
994c6402783Sakolb found++; /* home lgroup in h option arg list */
9957c478bd9Sstevel@tonic-gate if (!found && !tflg && !aflg)
9967c478bd9Sstevel@tonic-gate return (1);
9977c478bd9Sstevel@tonic-gate if (!prfind(found, &info, &tp))
9987c478bd9Sstevel@tonic-gate return (1);
9997c478bd9Sstevel@tonic-gate if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
10007c478bd9Sstevel@tonic-gate ssize_t prsz;
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate (void) strcpy(&pname[pdlen], "lpsinfo");
10037c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) == -1)
10047c478bd9Sstevel@tonic-gate return (1);
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate * Get the info structures for the lwps.
10077c478bd9Sstevel@tonic-gate */
10087c478bd9Sstevel@tonic-gate prsz = read(procfd, lpsinfobuf, lpbufsize);
10097c478bd9Sstevel@tonic-gate if (prsz == -1) {
10107c478bd9Sstevel@tonic-gate int saverr = errno;
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate (void) close(procfd);
10137c478bd9Sstevel@tonic-gate if (saverr == EAGAIN)
10147c478bd9Sstevel@tonic-gate goto retry;
10157c478bd9Sstevel@tonic-gate if (saverr != ENOENT)
10167c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10177c478bd9Sstevel@tonic-gate gettext("ps: read() on %s: %s\n"),
10187c478bd9Sstevel@tonic-gate pname, err_string(saverr));
10197c478bd9Sstevel@tonic-gate return (1);
10207c478bd9Sstevel@tonic-gate }
10217c478bd9Sstevel@tonic-gate (void) close(procfd);
10227c478bd9Sstevel@tonic-gate if (prsz == lpbufsize) {
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate * buffer overflow. Realloc new buffer.
10257c478bd9Sstevel@tonic-gate * Error handling is done in Realloc().
10267c478bd9Sstevel@tonic-gate */
10277c478bd9Sstevel@tonic-gate lpbufsize *= 2;
10287c478bd9Sstevel@tonic-gate lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
10297c478bd9Sstevel@tonic-gate goto retry;
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb))
10327c478bd9Sstevel@tonic-gate goto retry;
10337c478bd9Sstevel@tonic-gate lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) {
10367c478bd9Sstevel@tonic-gate prcom(&info, tp);
10377c478bd9Sstevel@tonic-gate } else {
10387c478bd9Sstevel@tonic-gate int nlwp = 0;
10397c478bd9Sstevel@tonic-gate
10407c478bd9Sstevel@tonic-gate do {
10417c478bd9Sstevel@tonic-gate info.pr_lwp = *lwpsinfo;
10427c478bd9Sstevel@tonic-gate prcom(&info, tp);
10437c478bd9Sstevel@tonic-gate /* LINTED improper alignment */
10447c478bd9Sstevel@tonic-gate lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo +
10457c478bd9Sstevel@tonic-gate lpsinfobuf->pr_entsize);
10467c478bd9Sstevel@tonic-gate } while (++nlwp < lpsinfobuf->pr_nent);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate return (0);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate
1051eaaa4d5fSBryan Cantrill static int
field_cmp(const void * l,const void * r)1052eaaa4d5fSBryan Cantrill field_cmp(const void *l, const void *r)
1053eaaa4d5fSBryan Cantrill {
1054eaaa4d5fSBryan Cantrill struct def_field *lhs = *((struct def_field **)l);
1055eaaa4d5fSBryan Cantrill struct def_field *rhs = *((struct def_field **)r);
1056eaaa4d5fSBryan Cantrill
1057eaaa4d5fSBryan Cantrill return (strcmp(lhs->fname, rhs->fname));
1058eaaa4d5fSBryan Cantrill }
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate static void
usage(void)10617c478bd9Sstevel@tonic-gate usage(void) /* print usage message and quit */
10627c478bd9Sstevel@tonic-gate {
1063eaaa4d5fSBryan Cantrill struct def_field *df, *sorted[NFIELDS];
1064eaaa4d5fSBryan Cantrill int pos = 80, i = 0;
1065eaaa4d5fSBryan Cantrill
10667c478bd9Sstevel@tonic-gate static char usage1[] =
10670a1278f2SGary Mills "ps [ -aAdefHlcjLPWyZ ] [ -o format ] [ -t termlist ]";
10687c478bd9Sstevel@tonic-gate static char usage2[] =
10697c478bd9Sstevel@tonic-gate "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
10707c478bd9Sstevel@tonic-gate static char usage3[] =
1071eaaa4d5fSBryan Cantrill "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]";
10727c478bd9Sstevel@tonic-gate static char usage4[] =
1073eaaa4d5fSBryan Cantrill "\t[ -z zonelist ] [-h lgrplist]";
10747c478bd9Sstevel@tonic-gate static char usage5[] =
1075eaaa4d5fSBryan Cantrill " 'format' is one or more of:";
10767c478bd9Sstevel@tonic-gate
10777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1078eaaa4d5fSBryan Cantrill gettext("usage: %s\n%s\n%s\n%s\n%s"),
10797c478bd9Sstevel@tonic-gate gettext(usage1), gettext(usage2), gettext(usage3),
1080eaaa4d5fSBryan Cantrill gettext(usage4), gettext(usage5));
1081eaaa4d5fSBryan Cantrill
1082eaaa4d5fSBryan Cantrill /*
1083eaaa4d5fSBryan Cantrill * Now print out the possible output formats such that they neatly fit
1084eaaa4d5fSBryan Cantrill * into eighty columns. Note that the fact that we are determining
1085eaaa4d5fSBryan Cantrill * this output programmatically means that a gettext() is impossible --
1086eaaa4d5fSBryan Cantrill * but it would be a mistake to localize the output formats anyway as
1087eaaa4d5fSBryan Cantrill * they are tokens for input, not output themselves.
1088eaaa4d5fSBryan Cantrill */
1089eaaa4d5fSBryan Cantrill for (df = &fname[0]; df < &fname[NFIELDS]; df++)
1090eaaa4d5fSBryan Cantrill sorted[i++] = df;
1091eaaa4d5fSBryan Cantrill
1092eaaa4d5fSBryan Cantrill (void) qsort(sorted, NFIELDS, sizeof (void *), field_cmp);
1093eaaa4d5fSBryan Cantrill
1094eaaa4d5fSBryan Cantrill for (i = 0; i < NFIELDS; i++) {
1095eaaa4d5fSBryan Cantrill if (pos + strlen((df = sorted[i])->fname) + 1 >= 80) {
1096eaaa4d5fSBryan Cantrill (void) fprintf(stderr, "\n\t");
1097eaaa4d5fSBryan Cantrill pos = 8;
1098eaaa4d5fSBryan Cantrill }
1099eaaa4d5fSBryan Cantrill
1100eaaa4d5fSBryan Cantrill (void) fprintf(stderr, "%s%s", pos > 8 ? " " : "", df->fname);
1101eaaa4d5fSBryan Cantrill pos += strlen(df->fname) + 1;
1102eaaa4d5fSBryan Cantrill }
1103eaaa4d5fSBryan Cantrill
1104eaaa4d5fSBryan Cantrill (void) fprintf(stderr, "\n");
1105eaaa4d5fSBryan Cantrill
11067c478bd9Sstevel@tonic-gate exit(1);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate * getarg() finds the next argument in list and copies arg into argbuf.
11117c478bd9Sstevel@tonic-gate * p1 first pts to arg passed back from getopt routine. p1 is then
11127c478bd9Sstevel@tonic-gate * bumped to next character that is not a comma or blank -- p1 NULL
11137c478bd9Sstevel@tonic-gate * indicates end of list.
11147c478bd9Sstevel@tonic-gate */
11157c478bd9Sstevel@tonic-gate static char *
getarg(char ** pp1)11167c478bd9Sstevel@tonic-gate getarg(char **pp1)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate static char argbuf[ARGSIZ];
11197c478bd9Sstevel@tonic-gate char *p1 = *pp1;
11207c478bd9Sstevel@tonic-gate char *parga = argbuf;
11217c478bd9Sstevel@tonic-gate int c;
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
11247c478bd9Sstevel@tonic-gate p1++;
11257c478bd9Sstevel@tonic-gate
11267c478bd9Sstevel@tonic-gate while ((c = *p1) != '\0' && c != ',' && !isspace(c)) {
11277c478bd9Sstevel@tonic-gate if (parga < argbuf + ARGSIZ - 1)
11287c478bd9Sstevel@tonic-gate *parga++ = c;
11297c478bd9Sstevel@tonic-gate p1++;
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate *parga = '\0';
11327c478bd9Sstevel@tonic-gate
11337c478bd9Sstevel@tonic-gate while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
11347c478bd9Sstevel@tonic-gate p1++;
11357c478bd9Sstevel@tonic-gate
11367c478bd9Sstevel@tonic-gate *pp1 = p1;
11377c478bd9Sstevel@tonic-gate
11387c478bd9Sstevel@tonic-gate return (argbuf);
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate
11417c478bd9Sstevel@tonic-gate /*
11427c478bd9Sstevel@tonic-gate * parse_format() takes the argument to the -o option,
11437c478bd9Sstevel@tonic-gate * sets up the next output field structure, and returns
11447c478bd9Sstevel@tonic-gate * a pointer to any further output field specifier(s).
11457c478bd9Sstevel@tonic-gate * As a side-effect, it increments errflg if encounters a format error.
11467c478bd9Sstevel@tonic-gate */
11477c478bd9Sstevel@tonic-gate static char *
parse_format(char * arg)11487c478bd9Sstevel@tonic-gate parse_format(char *arg)
11497c478bd9Sstevel@tonic-gate {
11507c478bd9Sstevel@tonic-gate int c;
11517c478bd9Sstevel@tonic-gate char *name;
11527c478bd9Sstevel@tonic-gate char *header = NULL;
11537c478bd9Sstevel@tonic-gate int width = 0;
11547c478bd9Sstevel@tonic-gate struct def_field *df;
11557c478bd9Sstevel@tonic-gate struct field *f;
11567c478bd9Sstevel@tonic-gate
11577c478bd9Sstevel@tonic-gate while ((c = *arg) != '\0' && (c == ',' || isspace(c)))
11587c478bd9Sstevel@tonic-gate arg++;
11597c478bd9Sstevel@tonic-gate if (c == '\0')
11607c478bd9Sstevel@tonic-gate return (NULL);
11617c478bd9Sstevel@tonic-gate name = arg;
11627c478bd9Sstevel@tonic-gate arg = strpbrk(arg, " \t\r\v\f\n,=");
11637c478bd9Sstevel@tonic-gate if (arg != NULL) {
11647c478bd9Sstevel@tonic-gate c = *arg;
11657c478bd9Sstevel@tonic-gate *arg++ = '\0';
11667c478bd9Sstevel@tonic-gate if (c == '=') {
11677c478bd9Sstevel@tonic-gate char *s;
11687c478bd9Sstevel@tonic-gate
11697c478bd9Sstevel@tonic-gate header = arg;
11707c478bd9Sstevel@tonic-gate arg = NULL;
11717c478bd9Sstevel@tonic-gate width = strlen(header);
11727c478bd9Sstevel@tonic-gate s = header + width;
11737c478bd9Sstevel@tonic-gate while (s > header && isspace(*--s))
11747c478bd9Sstevel@tonic-gate *s = '\0';
11757c478bd9Sstevel@tonic-gate while (isspace(*header))
11767c478bd9Sstevel@tonic-gate header++;
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate for (df = &fname[0]; df < &fname[NFIELDS]; df++)
11807c478bd9Sstevel@tonic-gate if (strcmp(name, df->fname) == 0) {
11817c478bd9Sstevel@tonic-gate if (strcmp(name, "lwp") == 0)
11827c478bd9Sstevel@tonic-gate Lflg++;
11837c478bd9Sstevel@tonic-gate break;
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate if (df >= &fname[NFIELDS]) {
11867c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
11877c478bd9Sstevel@tonic-gate gettext("ps: unknown output format: -o %s\n"),
11887c478bd9Sstevel@tonic-gate name);
11897c478bd9Sstevel@tonic-gate errflg++;
11907c478bd9Sstevel@tonic-gate return (arg);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate if ((f = malloc(sizeof (*f))) == NULL) {
11937c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
11947c478bd9Sstevel@tonic-gate gettext("ps: malloc() for output format failed, %s\n"),
11957c478bd9Sstevel@tonic-gate err_string(errno));
11967c478bd9Sstevel@tonic-gate exit(1);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate f->next = NULL;
11997c478bd9Sstevel@tonic-gate f->fname = df - &fname[0];
12007c478bd9Sstevel@tonic-gate f->header = header? header : df->header;
12017c478bd9Sstevel@tonic-gate if (width == 0)
12027c478bd9Sstevel@tonic-gate width = df->width;
12037c478bd9Sstevel@tonic-gate if (*f->header != '\0')
12047c478bd9Sstevel@tonic-gate do_header = 1;
12057c478bd9Sstevel@tonic-gate f->width = max(width, df->minwidth);
12067c478bd9Sstevel@tonic-gate
12077c478bd9Sstevel@tonic-gate if (fields == NULL)
12087c478bd9Sstevel@tonic-gate fields = last_field = f;
12097c478bd9Sstevel@tonic-gate else {
12107c478bd9Sstevel@tonic-gate last_field->next = f;
12117c478bd9Sstevel@tonic-gate last_field = f;
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate
12147c478bd9Sstevel@tonic-gate return (arg);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate
12177c478bd9Sstevel@tonic-gate static char *
devlookup(dev_t ddev)12187c478bd9Sstevel@tonic-gate devlookup(dev_t ddev)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate struct devl *dp;
12217c478bd9Sstevel@tonic-gate int i;
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate for (dp = devl, i = 0; i < ndev; dp++, i++) {
12247c478bd9Sstevel@tonic-gate if (dp->ddev == ddev)
12257c478bd9Sstevel@tonic-gate return (dp->dname);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate return (NULL);
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate
12307c478bd9Sstevel@tonic-gate static char *
devadd(char * name,dev_t ddev)12317c478bd9Sstevel@tonic-gate devadd(char *name, dev_t ddev)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate struct devl *dp;
12347c478bd9Sstevel@tonic-gate int leng, start, i;
12357c478bd9Sstevel@tonic-gate
12367c478bd9Sstevel@tonic-gate if (ndev == maxdev) {
12377c478bd9Sstevel@tonic-gate maxdev += DNINCR;
12387c478bd9Sstevel@tonic-gate devl = Realloc(devl, maxdev * sizeof (struct devl));
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate dp = &devl[ndev++];
12417c478bd9Sstevel@tonic-gate
12427c478bd9Sstevel@tonic-gate dp->ddev = ddev;
12437c478bd9Sstevel@tonic-gate if (name == NULL) {
12447c478bd9Sstevel@tonic-gate (void) strcpy(dp->dname, "??");
12457c478bd9Sstevel@tonic-gate return (dp->dname);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate
12487c478bd9Sstevel@tonic-gate leng = strlen(name);
12497c478bd9Sstevel@tonic-gate /* Strip off /dev/ */
12507c478bd9Sstevel@tonic-gate if (leng < DNSIZE + 4)
12517c478bd9Sstevel@tonic-gate (void) strcpy(dp->dname, &name[5]);
12527c478bd9Sstevel@tonic-gate else {
12537c478bd9Sstevel@tonic-gate start = leng - DNSIZE - 1;
12547c478bd9Sstevel@tonic-gate
12557c478bd9Sstevel@tonic-gate for (i = start; i < leng && name[i] != '/'; i++)
12567c478bd9Sstevel@tonic-gate ;
12577c478bd9Sstevel@tonic-gate if (i == leng)
12587c478bd9Sstevel@tonic-gate (void) strncpy(dp->dname, &name[start], DNSIZE);
12597c478bd9Sstevel@tonic-gate else
12607c478bd9Sstevel@tonic-gate (void) strncpy(dp->dname, &name[i+1], DNSIZE);
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate return (dp->dname);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate
12657c478bd9Sstevel@tonic-gate /*
12667c478bd9Sstevel@tonic-gate * gettty returns the user's tty number or ? if none.
12677c478bd9Sstevel@tonic-gate */
12687c478bd9Sstevel@tonic-gate static char *
gettty(psinfo_t * psinfo)12697c478bd9Sstevel@tonic-gate gettty(psinfo_t *psinfo)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate extern char *_ttyname_dev(dev_t, char *, size_t);
1272fba40a47SBryan Cantrill static zoneid_t zid = -1;
12737c478bd9Sstevel@tonic-gate char devname[TTYNAME_MAX];
12747c478bd9Sstevel@tonic-gate char *retval;
12757c478bd9Sstevel@tonic-gate
1276fba40a47SBryan Cantrill if (zid == -1)
1277fba40a47SBryan Cantrill zid = getzoneid();
1278fba40a47SBryan Cantrill
1279fba40a47SBryan Cantrill if (psinfo->pr_ttydev == PRNODEV || psinfo->pr_zoneid != zid)
12807c478bd9Sstevel@tonic-gate return ("?");
12817c478bd9Sstevel@tonic-gate
12827c478bd9Sstevel@tonic-gate if ((retval = devlookup(psinfo->pr_ttydev)) != NULL)
12837c478bd9Sstevel@tonic-gate return (retval);
12847c478bd9Sstevel@tonic-gate
12857c478bd9Sstevel@tonic-gate retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname));
12867c478bd9Sstevel@tonic-gate
12877c478bd9Sstevel@tonic-gate return (devadd(retval, psinfo->pr_ttydev));
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate * Find the process's tty and return 1 if process is to be printed.
12927c478bd9Sstevel@tonic-gate */
12937c478bd9Sstevel@tonic-gate static int
prfind(int found,psinfo_t * psinfo,char ** tpp)12947c478bd9Sstevel@tonic-gate prfind(int found, psinfo_t *psinfo, char **tpp)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate char *tp;
12977c478bd9Sstevel@tonic-gate struct tty *ttyp;
12987c478bd9Sstevel@tonic-gate
12997c478bd9Sstevel@tonic-gate if (psinfo->pr_nlwp == 0) {
13007c478bd9Sstevel@tonic-gate /* process is a zombie */
13017c478bd9Sstevel@tonic-gate *tpp = "?";
13027c478bd9Sstevel@tonic-gate if (tflg && !found)
13037c478bd9Sstevel@tonic-gate return (0);
13047c478bd9Sstevel@tonic-gate return (1);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate * Get current terminal. If none ("?") and 'a' is set, don't print
13097c478bd9Sstevel@tonic-gate * info. If 't' is set, check if term is in list of desired terminals
13107c478bd9Sstevel@tonic-gate * and print it if it is.
13117c478bd9Sstevel@tonic-gate */
13127c478bd9Sstevel@tonic-gate tp = gettty(psinfo);
13137c478bd9Sstevel@tonic-gate if (aflg && *tp == '?') {
13147c478bd9Sstevel@tonic-gate *tpp = tp;
13157c478bd9Sstevel@tonic-gate return (0);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate if (tflg && !found) {
13187c478bd9Sstevel@tonic-gate int match = 0;
13197c478bd9Sstevel@tonic-gate char *other = NULL;
13207c478bd9Sstevel@tonic-gate for (ttyp = tty; ttyp->tname != NULL; ttyp++) {
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate * Look for a name match
13237c478bd9Sstevel@tonic-gate */
13247c478bd9Sstevel@tonic-gate if (strcmp(tp, ttyp->tname) == 0) {
13257c478bd9Sstevel@tonic-gate match = 1;
13267c478bd9Sstevel@tonic-gate break;
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate /*
13297c478bd9Sstevel@tonic-gate * Look for same device under different names.
13307c478bd9Sstevel@tonic-gate */
13317c478bd9Sstevel@tonic-gate if ((other == NULL) &&
13327c478bd9Sstevel@tonic-gate (ttyp->tdev != PRNODEV) &&
13337c478bd9Sstevel@tonic-gate (psinfo->pr_ttydev == ttyp->tdev))
13347c478bd9Sstevel@tonic-gate other = ttyp->tname;
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate if (!match && (other != NULL)) {
13377c478bd9Sstevel@tonic-gate /*
13387c478bd9Sstevel@tonic-gate * found under a different name
13397c478bd9Sstevel@tonic-gate */
13407c478bd9Sstevel@tonic-gate match = 1;
13417c478bd9Sstevel@tonic-gate tp = other;
13427c478bd9Sstevel@tonic-gate }
1343f48205beScasper if (!match || (tuid != (uid_t)-1 && tuid != psinfo->pr_euid)) {
13447c478bd9Sstevel@tonic-gate /*
13457c478bd9Sstevel@tonic-gate * not found OR not matching euid
13467c478bd9Sstevel@tonic-gate */
13477c478bd9Sstevel@tonic-gate *tpp = tp;
13487c478bd9Sstevel@tonic-gate return (0);
13497c478bd9Sstevel@tonic-gate }
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate *tpp = tp;
13527c478bd9Sstevel@tonic-gate return (1);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate
13557c478bd9Sstevel@tonic-gate /*
13567c478bd9Sstevel@tonic-gate * Print info about the process.
13577c478bd9Sstevel@tonic-gate */
13587c478bd9Sstevel@tonic-gate static void
prcom(psinfo_t * psinfo,char * ttyp)13597c478bd9Sstevel@tonic-gate prcom(psinfo_t *psinfo, char *ttyp)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate char *cp;
13627c478bd9Sstevel@tonic-gate long tm;
13637c478bd9Sstevel@tonic-gate int bytesleft;
13647c478bd9Sstevel@tonic-gate int wcnt, length;
13657c478bd9Sstevel@tonic-gate wchar_t wchar;
13667c478bd9Sstevel@tonic-gate struct passwd *pwd;
13677c478bd9Sstevel@tonic-gate int zombie_lwp;
13687c478bd9Sstevel@tonic-gate char zonename[ZONENAME_MAX];
13697c478bd9Sstevel@tonic-gate
13707c478bd9Sstevel@tonic-gate /*
13717c478bd9Sstevel@tonic-gate * If process is zombie, call zombie print routine and return.
13727c478bd9Sstevel@tonic-gate */
13737c478bd9Sstevel@tonic-gate if (psinfo->pr_nlwp == 0) {
13747c478bd9Sstevel@tonic-gate if (fields != NULL)
13757c478bd9Sstevel@tonic-gate pr_fields(psinfo, ttyp, print_zombie_field);
13767c478bd9Sstevel@tonic-gate else
13777c478bd9Sstevel@tonic-gate przom(psinfo);
13787c478bd9Sstevel@tonic-gate return;
13797c478bd9Sstevel@tonic-gate }
13807c478bd9Sstevel@tonic-gate
13817c478bd9Sstevel@tonic-gate zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
13827c478bd9Sstevel@tonic-gate
13837c478bd9Sstevel@tonic-gate /*
13847c478bd9Sstevel@tonic-gate * If user specified '-o format', print requested fields and return.
13857c478bd9Sstevel@tonic-gate */
13867c478bd9Sstevel@tonic-gate if (fields != NULL) {
13877c478bd9Sstevel@tonic-gate pr_fields(psinfo, ttyp, print_field);
13887c478bd9Sstevel@tonic-gate return;
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate
13917c478bd9Sstevel@tonic-gate /*
13927c478bd9Sstevel@tonic-gate * All fields before 'PID' are printed with a trailing space as a
1393f5c9e9f9SCasper H.S. Dik * separator, rather than keeping track of which column is first. All
13947c478bd9Sstevel@tonic-gate * other fields are printed with a leading space.
13957c478bd9Sstevel@tonic-gate */
13967c478bd9Sstevel@tonic-gate if (lflg) {
13977c478bd9Sstevel@tonic-gate if (!yflg)
13987c478bd9Sstevel@tonic-gate (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
13997c478bd9Sstevel@tonic-gate (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate
14027c478bd9Sstevel@tonic-gate if (Zflg) { /* ZONE */
14037c478bd9Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename,
14047c478bd9Sstevel@tonic-gate sizeof (zonename)) < 0) {
14050a1278f2SGary Mills if (snprintf(NULL, 0, "%d",
14060a1278f2SGary Mills ((int)psinfo->pr_zoneid)) > 7)
14070a1278f2SGary Mills (void) printf(" %6.6d%c ",
14080a1278f2SGary Mills ((int)psinfo->pr_zoneid), '*');
14090a1278f2SGary Mills else
14100a1278f2SGary Mills (void) printf(" %7.7d ",
14110a1278f2SGary Mills ((int)psinfo->pr_zoneid));
14127c478bd9Sstevel@tonic-gate } else {
14130a1278f2SGary Mills size_t nw;
14140a1278f2SGary Mills
14150a1278f2SGary Mills nw = mbstowcs(NULL, zonename, 0);
14160a1278f2SGary Mills if (nw == (size_t)-1)
14170a1278f2SGary Mills (void) printf("%8.8s ", "ERROR");
14180a1278f2SGary Mills else if (nw > 8)
14190a1278f2SGary Mills (void) wprintf(L"%7.7s%c ", zonename, '*');
14200a1278f2SGary Mills else
14210a1278f2SGary Mills (void) wprintf(L"%8.8s ", zonename);
14227c478bd9Sstevel@tonic-gate }
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate
14257c478bd9Sstevel@tonic-gate if (fflg) { /* UID */
14260a1278f2SGary Mills if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
14270a1278f2SGary Mills size_t nw;
14280a1278f2SGary Mills
14290a1278f2SGary Mills nw = mbstowcs(NULL, pwd->pw_name, 0);
14300a1278f2SGary Mills if (nw == (size_t)-1)
14310a1278f2SGary Mills (void) printf("%8.8s ", "ERROR");
14320a1278f2SGary Mills else if (nw > 8)
14330a1278f2SGary Mills (void) wprintf(L"%7.7s%c ", pwd->pw_name, '*');
14340a1278f2SGary Mills else
14350a1278f2SGary Mills (void) wprintf(L"%8.8s ", pwd->pw_name);
14360a1278f2SGary Mills } else {
14370a1278f2SGary Mills if (snprintf(NULL, 0, "%u",
14380a1278f2SGary Mills (psinfo->pr_euid)) > 7)
14390a1278f2SGary Mills (void) printf(" %6.6u%c ", psinfo->pr_euid,
14400a1278f2SGary Mills '*');
14417c478bd9Sstevel@tonic-gate else
1442f48205beScasper (void) printf(" %7.7u ", psinfo->pr_euid);
14430a1278f2SGary Mills }
14447c478bd9Sstevel@tonic-gate } else if (lflg) {
14450a1278f2SGary Mills if (snprintf(NULL, 0, "%u", (psinfo->pr_euid)) > 6)
14460a1278f2SGary Mills (void) printf("%5.5u%c ", psinfo->pr_euid, '*');
14470a1278f2SGary Mills else
1448f48205beScasper (void) printf("%6u ", psinfo->pr_euid);
14497c478bd9Sstevel@tonic-gate }
14507c478bd9Sstevel@tonic-gate (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
14517c478bd9Sstevel@tonic-gate if (lflg || fflg)
14527c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth,
14537c478bd9Sstevel@tonic-gate (int)psinfo->pr_ppid); /* PPID */
14547c478bd9Sstevel@tonic-gate if (jflg) {
14557c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth,
14567c478bd9Sstevel@tonic-gate (int)psinfo->pr_pgid); /* PGID */
14577c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth,
14587c478bd9Sstevel@tonic-gate (int)psinfo->pr_sid); /* SID */
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate if (Lflg)
14617c478bd9Sstevel@tonic-gate (void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
14627c478bd9Sstevel@tonic-gate if (Pflg) {
14637c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */
14647c478bd9Sstevel@tonic-gate (void) printf(" -");
14657c478bd9Sstevel@tonic-gate else
14667c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);
14677c478bd9Sstevel@tonic-gate }
14687c478bd9Sstevel@tonic-gate if (Lflg && fflg) /* NLWP */
14697c478bd9Sstevel@tonic-gate (void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb);
14707c478bd9Sstevel@tonic-gate if (cflg) {
14717c478bd9Sstevel@tonic-gate if (zombie_lwp) /* CLS */
14727c478bd9Sstevel@tonic-gate (void) printf(" ");
14737c478bd9Sstevel@tonic-gate else
14747c478bd9Sstevel@tonic-gate (void) printf(" %4s", psinfo->pr_lwp.pr_clname);
14757c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */
14767c478bd9Sstevel@tonic-gate } else if (lflg || fflg) {
14777c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */
14787c478bd9Sstevel@tonic-gate if (lflg) { /* PRI NI */
14797c478bd9Sstevel@tonic-gate /*
14807c478bd9Sstevel@tonic-gate * Print priorities the old way (lower numbers
14817c478bd9Sstevel@tonic-gate * mean higher priority) and print nice value
14827c478bd9Sstevel@tonic-gate * for time sharing procs.
14837c478bd9Sstevel@tonic-gate */
14847c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_oldpri);
14857c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_oldpri != 0)
14867c478bd9Sstevel@tonic-gate (void) printf(" %2d", psinfo->pr_lwp.pr_nice);
14877c478bd9Sstevel@tonic-gate else
14887c478bd9Sstevel@tonic-gate (void) printf(" %2.2s",
14897c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_clname);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate }
14927c478bd9Sstevel@tonic-gate if (lflg) {
14937c478bd9Sstevel@tonic-gate if (yflg) {
14947c478bd9Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* RSS */
14957c478bd9Sstevel@tonic-gate (void) printf(" 0");
14967c478bd9Sstevel@tonic-gate else if (psinfo->pr_rssize)
14977c478bd9Sstevel@tonic-gate (void) printf(" %5lu",
14987c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_rssize);
14997c478bd9Sstevel@tonic-gate else
15007c478bd9Sstevel@tonic-gate (void) printf(" ?");
15017c478bd9Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* SZ */
15027c478bd9Sstevel@tonic-gate (void) printf(" 0");
15037c478bd9Sstevel@tonic-gate else if (psinfo->pr_size)
15047c478bd9Sstevel@tonic-gate (void) printf(" %6lu",
15057c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_size);
15067c478bd9Sstevel@tonic-gate else
15077c478bd9Sstevel@tonic-gate (void) printf(" ?");
15087c478bd9Sstevel@tonic-gate } else {
15097c478bd9Sstevel@tonic-gate #ifndef _LP64
15107c478bd9Sstevel@tonic-gate if (psinfo->pr_addr) /* ADDR */
15117c478bd9Sstevel@tonic-gate (void) printf(" %8lx",
15127c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_addr);
15137c478bd9Sstevel@tonic-gate else
15147c478bd9Sstevel@tonic-gate #endif
15157c478bd9Sstevel@tonic-gate (void) printf(" ?");
15167c478bd9Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* SZ */
15177c478bd9Sstevel@tonic-gate (void) printf(" 0");
15187c478bd9Sstevel@tonic-gate else if (psinfo->pr_size)
15197c478bd9Sstevel@tonic-gate (void) printf(" %6lu",
15207c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_size / kbytes_per_page);
15217c478bd9Sstevel@tonic-gate else
15227c478bd9Sstevel@tonic-gate (void) printf(" ?");
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_sname != 'S') /* WCHAN */
15257c478bd9Sstevel@tonic-gate (void) printf(" ");
15267c478bd9Sstevel@tonic-gate #ifndef _LP64
15277c478bd9Sstevel@tonic-gate else if (psinfo->pr_lwp.pr_wchan)
15287c478bd9Sstevel@tonic-gate (void) printf(" %8lx",
15297c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_lwp.pr_wchan);
15307c478bd9Sstevel@tonic-gate #endif
15317c478bd9Sstevel@tonic-gate else
15327c478bd9Sstevel@tonic-gate (void) printf(" ?");
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate if (fflg) { /* STIME */
153516f94f58Ssayama int width = fname[F_STIME].width;
15367c478bd9Sstevel@tonic-gate if (Lflg)
153716f94f58Ssayama prtime(psinfo->pr_lwp.pr_start, width + 1, 1);
15387c478bd9Sstevel@tonic-gate else
153916f94f58Ssayama prtime(psinfo->pr_start, width + 1, 1);
15407c478bd9Sstevel@tonic-gate }
1541c6402783Sakolb
1542c6402783Sakolb if (Hflg) {
1543c6402783Sakolb /* Display home lgroup */
1544c6402783Sakolb (void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp);
1545c6402783Sakolb }
1546c6402783Sakolb
15477c478bd9Sstevel@tonic-gate (void) printf(" %-8.14s", ttyp); /* TTY */
15487c478bd9Sstevel@tonic-gate if (Lflg) {
15497c478bd9Sstevel@tonic-gate tm = psinfo->pr_lwp.pr_time.tv_sec;
15507c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
15517c478bd9Sstevel@tonic-gate tm++;
15527c478bd9Sstevel@tonic-gate } else {
15537c478bd9Sstevel@tonic-gate tm = psinfo->pr_time.tv_sec;
15547c478bd9Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000)
15557c478bd9Sstevel@tonic-gate tm++;
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* [L]TIME */
15587c478bd9Sstevel@tonic-gate
15597c478bd9Sstevel@tonic-gate if (zombie_lwp) {
15607c478bd9Sstevel@tonic-gate (void) printf(" <defunct>\n");
15617c478bd9Sstevel@tonic-gate return;
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate
15647c478bd9Sstevel@tonic-gate if (!fflg) { /* CMD */
15657c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_fname, 16, 8);
15667c478bd9Sstevel@tonic-gate (void) printf(" %.*s\n", wcnt, psinfo->pr_fname);
15677c478bd9Sstevel@tonic-gate return;
15687c478bd9Sstevel@tonic-gate }
15697c478bd9Sstevel@tonic-gate
1570c6402783Sakolb
15717c478bd9Sstevel@tonic-gate /*
15727c478bd9Sstevel@tonic-gate * PRARGSZ == length of cmd arg string.
15737c478bd9Sstevel@tonic-gate */
15747c478bd9Sstevel@tonic-gate psinfo->pr_psargs[PRARGSZ-1] = '\0';
15757c478bd9Sstevel@tonic-gate bytesleft = PRARGSZ;
15767c478bd9Sstevel@tonic-gate for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
15777c478bd9Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX);
15787c478bd9Sstevel@tonic-gate if (length == 0)
15797c478bd9Sstevel@tonic-gate break;
15807c478bd9Sstevel@tonic-gate if (length < 0 || !iswprint(wchar)) {
15817c478bd9Sstevel@tonic-gate if (length < 0)
15827c478bd9Sstevel@tonic-gate length = 1;
15837c478bd9Sstevel@tonic-gate if (bytesleft <= length) {
15847c478bd9Sstevel@tonic-gate *cp = '\0';
15857c478bd9Sstevel@tonic-gate break;
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate /* omit the unprintable character */
15887c478bd9Sstevel@tonic-gate (void) memmove(cp, cp+length, bytesleft-length);
15897c478bd9Sstevel@tonic-gate length = 0;
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate bytesleft -= length;
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ);
15947c478bd9Sstevel@tonic-gate (void) printf(" %.*s\n", wcnt, psinfo->pr_psargs);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate
15977c478bd9Sstevel@tonic-gate /*
15987c478bd9Sstevel@tonic-gate * Print percent from 16-bit binary fraction [0 .. 1]
15997c478bd9Sstevel@tonic-gate * Round up .01 to .1 to indicate some small percentage (the 0x7000 below).
16007c478bd9Sstevel@tonic-gate */
16017c478bd9Sstevel@tonic-gate static void
prtpct(ushort_t pct,int width)16027c478bd9Sstevel@tonic-gate prtpct(ushort_t pct, int width)
16037c478bd9Sstevel@tonic-gate {
16047c478bd9Sstevel@tonic-gate uint_t value = pct; /* need 32 bits to compute with */
16057c478bd9Sstevel@tonic-gate
16067c478bd9Sstevel@tonic-gate value = ((value * 1000) + 0x7000) >> 15; /* [0 .. 1000] */
16077c478bd9Sstevel@tonic-gate if (value >= 1000)
16087c478bd9Sstevel@tonic-gate value = 999;
16097c478bd9Sstevel@tonic-gate if ((width -= 2) < 2)
16107c478bd9Sstevel@tonic-gate width = 2;
16117c478bd9Sstevel@tonic-gate (void) printf("%*u.%u", width, value / 10, value % 10);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate
16147c478bd9Sstevel@tonic-gate static void
print_time(time_t tim,int width)16157c478bd9Sstevel@tonic-gate print_time(time_t tim, int width)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate char buf[30];
16187c478bd9Sstevel@tonic-gate time_t seconds;
16197c478bd9Sstevel@tonic-gate time_t minutes;
16207c478bd9Sstevel@tonic-gate time_t hours;
16217c478bd9Sstevel@tonic-gate time_t days;
16227c478bd9Sstevel@tonic-gate
16237c478bd9Sstevel@tonic-gate if (tim < 0) {
16247c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
16257c478bd9Sstevel@tonic-gate return;
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate
16287c478bd9Sstevel@tonic-gate seconds = tim % 60;
16297c478bd9Sstevel@tonic-gate tim /= 60;
16307c478bd9Sstevel@tonic-gate minutes = tim % 60;
16317c478bd9Sstevel@tonic-gate tim /= 60;
16327c478bd9Sstevel@tonic-gate hours = tim % 24;
16337c478bd9Sstevel@tonic-gate days = tim / 24;
16347c478bd9Sstevel@tonic-gate
16357c478bd9Sstevel@tonic-gate if (days > 0) {
16367c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld",
16377c478bd9Sstevel@tonic-gate days, hours, minutes, seconds);
16387c478bd9Sstevel@tonic-gate } else if (hours > 0) {
16397c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld",
16407c478bd9Sstevel@tonic-gate hours, minutes, seconds);
16417c478bd9Sstevel@tonic-gate } else {
16427c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld",
16437c478bd9Sstevel@tonic-gate minutes, seconds);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate
16467c478bd9Sstevel@tonic-gate (void) printf("%*s", width, buf);
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate
16497c478bd9Sstevel@tonic-gate static void
print_field(psinfo_t * psinfo,struct field * f,const char * ttyp)16507c478bd9Sstevel@tonic-gate print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate int width = f->width;
16537c478bd9Sstevel@tonic-gate struct passwd *pwd;
16547c478bd9Sstevel@tonic-gate struct group *grp;
16557c478bd9Sstevel@tonic-gate time_t cputime;
16567c478bd9Sstevel@tonic-gate int bytesleft;
16577c478bd9Sstevel@tonic-gate int wcnt;
16587c478bd9Sstevel@tonic-gate wchar_t wchar;
16597c478bd9Sstevel@tonic-gate char *cp;
16607c478bd9Sstevel@tonic-gate int length;
16617c478bd9Sstevel@tonic-gate ulong_t mask;
1662*e503abb7SIgor Kozhukhov char c = '\0', *csave = NULL;
16637c478bd9Sstevel@tonic-gate int zombie_lwp;
16647c478bd9Sstevel@tonic-gate
16657c478bd9Sstevel@tonic-gate zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
16667c478bd9Sstevel@tonic-gate
16677c478bd9Sstevel@tonic-gate switch (f->fname) {
16687c478bd9Sstevel@tonic-gate case F_RUSER:
16690a1278f2SGary Mills if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) {
16700a1278f2SGary Mills size_t nw;
16710a1278f2SGary Mills
16720a1278f2SGary Mills nw = mbstowcs(NULL, pwd->pw_name, 0);
16730a1278f2SGary Mills if (nw == (size_t)-1)
16740a1278f2SGary Mills (void) printf("%*s ", width, "ERROR");
16750a1278f2SGary Mills else if (Wflg && nw > width)
16760a1278f2SGary Mills (void) wprintf(L"%.*s%c", width - 1,
16770a1278f2SGary Mills pwd->pw_name, '*');
16780a1278f2SGary Mills else
16790a1278f2SGary Mills (void) wprintf(L"%*s", width, pwd->pw_name);
16800a1278f2SGary Mills } else {
16810a1278f2SGary Mills if (Wflg && snprintf(NULL, 0, "%u",
16820a1278f2SGary Mills (psinfo->pr_uid)) > width)
16830a1278f2SGary Mills
16840a1278f2SGary Mills (void) printf("%*u%c", width - 1,
16850a1278f2SGary Mills psinfo->pr_uid, '*');
16867c478bd9Sstevel@tonic-gate else
1687f48205beScasper (void) printf("%*u", width, psinfo->pr_uid);
16880a1278f2SGary Mills }
16897c478bd9Sstevel@tonic-gate break;
16907c478bd9Sstevel@tonic-gate case F_USER:
16910a1278f2SGary Mills if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
16920a1278f2SGary Mills size_t nw;
16930a1278f2SGary Mills
16940a1278f2SGary Mills nw = mbstowcs(NULL, pwd->pw_name, 0);
16950a1278f2SGary Mills if (nw == (size_t)-1)
16960a1278f2SGary Mills (void) printf("%*s ", width, "ERROR");
16970a1278f2SGary Mills else if (Wflg && nw > width)
16980a1278f2SGary Mills (void) wprintf(L"%.*s%c", width - 1,
16990a1278f2SGary Mills pwd->pw_name, '*');
17000a1278f2SGary Mills else
17010a1278f2SGary Mills (void) wprintf(L"%*s", width, pwd->pw_name);
17020a1278f2SGary Mills } else {
17030a1278f2SGary Mills if (Wflg && snprintf(NULL, 0, "%u",
17040a1278f2SGary Mills (psinfo->pr_euid)) > width)
17050a1278f2SGary Mills
17060a1278f2SGary Mills (void) printf("%*u%c", width - 1,
17070a1278f2SGary Mills psinfo->pr_euid, '*');
17087c478bd9Sstevel@tonic-gate else
1709f48205beScasper (void) printf("%*u", width, psinfo->pr_euid);
17100a1278f2SGary Mills }
17117c478bd9Sstevel@tonic-gate break;
17127c478bd9Sstevel@tonic-gate case F_RGROUP:
17137c478bd9Sstevel@tonic-gate if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
17147c478bd9Sstevel@tonic-gate (void) printf("%*s", width, grp->gr_name);
17157c478bd9Sstevel@tonic-gate else
1716f48205beScasper (void) printf("%*u", width, psinfo->pr_gid);
17177c478bd9Sstevel@tonic-gate break;
17187c478bd9Sstevel@tonic-gate case F_GROUP:
17197c478bd9Sstevel@tonic-gate if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
17207c478bd9Sstevel@tonic-gate (void) printf("%*s", width, grp->gr_name);
17217c478bd9Sstevel@tonic-gate else
1722f48205beScasper (void) printf("%*u", width, psinfo->pr_egid);
17237c478bd9Sstevel@tonic-gate break;
17247c478bd9Sstevel@tonic-gate case F_RUID:
1725f48205beScasper (void) printf("%*u", width, psinfo->pr_uid);
17267c478bd9Sstevel@tonic-gate break;
17277c478bd9Sstevel@tonic-gate case F_UID:
1728f48205beScasper (void) printf("%*u", width, psinfo->pr_euid);
17297c478bd9Sstevel@tonic-gate break;
17307c478bd9Sstevel@tonic-gate case F_RGID:
1731f48205beScasper (void) printf("%*u", width, psinfo->pr_gid);
17327c478bd9Sstevel@tonic-gate break;
17337c478bd9Sstevel@tonic-gate case F_GID:
1734f48205beScasper (void) printf("%*u", width, psinfo->pr_egid);
17357c478bd9Sstevel@tonic-gate break;
17367c478bd9Sstevel@tonic-gate case F_PID:
17377c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_pid);
17387c478bd9Sstevel@tonic-gate break;
17397c478bd9Sstevel@tonic-gate case F_PPID:
17407c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_ppid);
17417c478bd9Sstevel@tonic-gate break;
17427c478bd9Sstevel@tonic-gate case F_PGID:
17437c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_pgid);
17447c478bd9Sstevel@tonic-gate break;
17457c478bd9Sstevel@tonic-gate case F_SID:
17467c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_sid);
17477c478bd9Sstevel@tonic-gate break;
17487c478bd9Sstevel@tonic-gate case F_PSR:
17497c478bd9Sstevel@tonic-gate if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
17507c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
17517c478bd9Sstevel@tonic-gate else
17527c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
17537c478bd9Sstevel@tonic-gate break;
17547c478bd9Sstevel@tonic-gate case F_LWP:
17557c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
17567c478bd9Sstevel@tonic-gate break;
17577c478bd9Sstevel@tonic-gate case F_NLWP:
17587c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
17597c478bd9Sstevel@tonic-gate break;
17607c478bd9Sstevel@tonic-gate case F_OPRI:
17617c478bd9Sstevel@tonic-gate if (zombie_lwp)
17627c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
17637c478bd9Sstevel@tonic-gate else
17647c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
17657c478bd9Sstevel@tonic-gate break;
17667c478bd9Sstevel@tonic-gate case F_PRI:
17677c478bd9Sstevel@tonic-gate if (zombie_lwp)
17687c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
17697c478bd9Sstevel@tonic-gate else
17707c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_pri);
17717c478bd9Sstevel@tonic-gate break;
17727c478bd9Sstevel@tonic-gate case F_F:
17737c478bd9Sstevel@tonic-gate mask = 0xffffffffUL;
17747c478bd9Sstevel@tonic-gate if (width < 8)
17757c478bd9Sstevel@tonic-gate mask >>= (8 - width) * 4;
17767c478bd9Sstevel@tonic-gate (void) printf("%*lx", width, psinfo->pr_flag & mask);
17777c478bd9Sstevel@tonic-gate break;
17787c478bd9Sstevel@tonic-gate case F_S:
17797c478bd9Sstevel@tonic-gate (void) printf("%*c", width, psinfo->pr_lwp.pr_sname);
17807c478bd9Sstevel@tonic-gate break;
17817c478bd9Sstevel@tonic-gate case F_C:
17827c478bd9Sstevel@tonic-gate if (zombie_lwp)
17837c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
17847c478bd9Sstevel@tonic-gate else
17857c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_cpu);
17867c478bd9Sstevel@tonic-gate break;
17877c478bd9Sstevel@tonic-gate case F_PCPU:
17887c478bd9Sstevel@tonic-gate if (zombie_lwp)
17897c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
17907c478bd9Sstevel@tonic-gate else if (Lflg)
17917c478bd9Sstevel@tonic-gate prtpct(psinfo->pr_lwp.pr_pctcpu, width);
17927c478bd9Sstevel@tonic-gate else
17937c478bd9Sstevel@tonic-gate prtpct(psinfo->pr_pctcpu, width);
17947c478bd9Sstevel@tonic-gate break;
17957c478bd9Sstevel@tonic-gate case F_PMEM:
17967c478bd9Sstevel@tonic-gate prtpct(psinfo->pr_pctmem, width);
17977c478bd9Sstevel@tonic-gate break;
17987c478bd9Sstevel@tonic-gate case F_OSZ:
17997c478bd9Sstevel@tonic-gate (void) printf("%*lu", width,
18007c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_size / kbytes_per_page);
18017c478bd9Sstevel@tonic-gate break;
18027c478bd9Sstevel@tonic-gate case F_VSZ:
18037c478bd9Sstevel@tonic-gate (void) printf("%*lu", width, (ulong_t)psinfo->pr_size);
18047c478bd9Sstevel@tonic-gate break;
18057c478bd9Sstevel@tonic-gate case F_RSS:
18067c478bd9Sstevel@tonic-gate (void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize);
18077c478bd9Sstevel@tonic-gate break;
18087c478bd9Sstevel@tonic-gate case F_NICE:
18097c478bd9Sstevel@tonic-gate /* if pr_oldpri is zero, then this class has no nice */
18107c478bd9Sstevel@tonic-gate if (zombie_lwp)
18117c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
18127c478bd9Sstevel@tonic-gate else if (psinfo->pr_lwp.pr_oldpri != 0)
18137c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_nice);
18147c478bd9Sstevel@tonic-gate else
18157c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width,
18167c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_clname);
18177c478bd9Sstevel@tonic-gate break;
18187c478bd9Sstevel@tonic-gate case F_CLASS:
18197c478bd9Sstevel@tonic-gate if (zombie_lwp)
18207c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
18217c478bd9Sstevel@tonic-gate else
18227c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width,
18237c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_clname);
18247c478bd9Sstevel@tonic-gate break;
18257c478bd9Sstevel@tonic-gate case F_STIME:
18267c478bd9Sstevel@tonic-gate if (Lflg)
18277c478bd9Sstevel@tonic-gate prtime(psinfo->pr_lwp.pr_start, width, 0);
18287c478bd9Sstevel@tonic-gate else
18297c478bd9Sstevel@tonic-gate prtime(psinfo->pr_start, width, 0);
18307c478bd9Sstevel@tonic-gate break;
18317c478bd9Sstevel@tonic-gate case F_ETIME:
18327c478bd9Sstevel@tonic-gate if (Lflg)
18337c478bd9Sstevel@tonic-gate print_time(delta_secs(&psinfo->pr_lwp.pr_start),
18347c478bd9Sstevel@tonic-gate width);
18357c478bd9Sstevel@tonic-gate else
18367c478bd9Sstevel@tonic-gate print_time(delta_secs(&psinfo->pr_start), width);
18377c478bd9Sstevel@tonic-gate break;
18387c478bd9Sstevel@tonic-gate case F_TIME:
18397c478bd9Sstevel@tonic-gate if (Lflg) {
18407c478bd9Sstevel@tonic-gate cputime = psinfo->pr_lwp.pr_time.tv_sec;
18417c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
18427c478bd9Sstevel@tonic-gate cputime++;
18437c478bd9Sstevel@tonic-gate } else {
18447c478bd9Sstevel@tonic-gate cputime = psinfo->pr_time.tv_sec;
18457c478bd9Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000)
18467c478bd9Sstevel@tonic-gate cputime++;
18477c478bd9Sstevel@tonic-gate }
18487c478bd9Sstevel@tonic-gate print_time(cputime, width);
18497c478bd9Sstevel@tonic-gate break;
18507c478bd9Sstevel@tonic-gate case F_TTY:
18517c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, ttyp);
18527c478bd9Sstevel@tonic-gate break;
18537c478bd9Sstevel@tonic-gate case F_ADDR:
18547c478bd9Sstevel@tonic-gate if (zombie_lwp)
18557c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
18567c478bd9Sstevel@tonic-gate else if (Lflg)
18577c478bd9Sstevel@tonic-gate (void) printf("%*lx", width,
18587c478bd9Sstevel@tonic-gate (long)psinfo->pr_lwp.pr_addr);
18597c478bd9Sstevel@tonic-gate else
18607c478bd9Sstevel@tonic-gate (void) printf("%*lx", width, (long)psinfo->pr_addr);
18617c478bd9Sstevel@tonic-gate break;
18627c478bd9Sstevel@tonic-gate case F_WCHAN:
18637c478bd9Sstevel@tonic-gate if (!zombie_lwp && psinfo->pr_lwp.pr_wchan)
18647c478bd9Sstevel@tonic-gate (void) printf("%*lx", width,
18657c478bd9Sstevel@tonic-gate (long)psinfo->pr_lwp.pr_wchan);
18667c478bd9Sstevel@tonic-gate else
18677c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width, "-");
18687c478bd9Sstevel@tonic-gate break;
18697c478bd9Sstevel@tonic-gate case F_FNAME:
18707c478bd9Sstevel@tonic-gate /*
18717c478bd9Sstevel@tonic-gate * Print full width unless this is the last output format.
18727c478bd9Sstevel@tonic-gate */
18737c478bd9Sstevel@tonic-gate if (zombie_lwp) {
18747c478bd9Sstevel@tonic-gate if (f->next != NULL)
18757c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>");
18767c478bd9Sstevel@tonic-gate else
18777c478bd9Sstevel@tonic-gate (void) printf("%s", "<defunct>");
18787c478bd9Sstevel@tonic-gate break;
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_fname, 16, width);
18817c478bd9Sstevel@tonic-gate if (f->next != NULL)
18827c478bd9Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, psinfo->pr_fname);
18837c478bd9Sstevel@tonic-gate else
18847c478bd9Sstevel@tonic-gate (void) printf("%-.*s", wcnt, psinfo->pr_fname);
18857c478bd9Sstevel@tonic-gate break;
18867c478bd9Sstevel@tonic-gate case F_COMM:
18877c478bd9Sstevel@tonic-gate if (zombie_lwp) {
18887c478bd9Sstevel@tonic-gate if (f->next != NULL)
18897c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>");
18907c478bd9Sstevel@tonic-gate else
18917c478bd9Sstevel@tonic-gate (void) printf("%s", "<defunct>");
18927c478bd9Sstevel@tonic-gate break;
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n");
18957c478bd9Sstevel@tonic-gate if (csave) {
18967c478bd9Sstevel@tonic-gate c = *csave;
18977c478bd9Sstevel@tonic-gate *csave = '\0';
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
19007c478bd9Sstevel@tonic-gate case F_ARGS:
19017c478bd9Sstevel@tonic-gate /*
19027c478bd9Sstevel@tonic-gate * PRARGSZ == length of cmd arg string.
19037c478bd9Sstevel@tonic-gate */
19047c478bd9Sstevel@tonic-gate if (zombie_lwp) {
19057c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>");
19067c478bd9Sstevel@tonic-gate break;
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate psinfo->pr_psargs[PRARGSZ-1] = '\0';
19097c478bd9Sstevel@tonic-gate bytesleft = PRARGSZ;
19107c478bd9Sstevel@tonic-gate for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
19117c478bd9Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX);
19127c478bd9Sstevel@tonic-gate if (length == 0)
19137c478bd9Sstevel@tonic-gate break;
19147c478bd9Sstevel@tonic-gate if (length < 0 || !iswprint(wchar)) {
19157c478bd9Sstevel@tonic-gate if (length < 0)
19167c478bd9Sstevel@tonic-gate length = 1;
19177c478bd9Sstevel@tonic-gate if (bytesleft <= length) {
19187c478bd9Sstevel@tonic-gate *cp = '\0';
19197c478bd9Sstevel@tonic-gate break;
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate /* omit the unprintable character */
19227c478bd9Sstevel@tonic-gate (void) memmove(cp, cp+length, bytesleft-length);
19237c478bd9Sstevel@tonic-gate length = 0;
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate bytesleft -= length;
19267c478bd9Sstevel@tonic-gate }
19277c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width);
19287c478bd9Sstevel@tonic-gate /*
19297c478bd9Sstevel@tonic-gate * Print full width unless this is the last format.
19307c478bd9Sstevel@tonic-gate */
19317c478bd9Sstevel@tonic-gate if (f->next != NULL)
19327c478bd9Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt,
19337c478bd9Sstevel@tonic-gate psinfo->pr_psargs);
19347c478bd9Sstevel@tonic-gate else
19357c478bd9Sstevel@tonic-gate (void) printf("%-.*s", wcnt,
19367c478bd9Sstevel@tonic-gate psinfo->pr_psargs);
19377c478bd9Sstevel@tonic-gate if (f->fname == F_COMM && csave)
19387c478bd9Sstevel@tonic-gate *csave = c;
19397c478bd9Sstevel@tonic-gate break;
19407c478bd9Sstevel@tonic-gate case F_TASKID:
19417c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_taskid);
19427c478bd9Sstevel@tonic-gate break;
19437c478bd9Sstevel@tonic-gate case F_PROJID:
19447c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_projid);
19457c478bd9Sstevel@tonic-gate break;
19467c478bd9Sstevel@tonic-gate case F_PROJECT:
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate struct project cproj;
19497c478bd9Sstevel@tonic-gate char proj_buf[PROJECT_BUFSZ];
19507c478bd9Sstevel@tonic-gate
19517c478bd9Sstevel@tonic-gate if ((getprojbyid(psinfo->pr_projid, &cproj,
19520a1278f2SGary Mills (void *)&proj_buf, PROJECT_BUFSZ)) == NULL) {
19530a1278f2SGary Mills if (Wflg && snprintf(NULL, 0, "%d",
19540a1278f2SGary Mills ((int)psinfo->pr_projid)) > width)
19550a1278f2SGary Mills (void) printf("%.*d%c", width - 1,
19560a1278f2SGary Mills ((int)psinfo->pr_projid), '*');
19570a1278f2SGary Mills else
19587c478bd9Sstevel@tonic-gate (void) printf("%*d", width,
19597c478bd9Sstevel@tonic-gate (int)psinfo->pr_projid);
19600a1278f2SGary Mills } else {
19610a1278f2SGary Mills size_t nw;
19620a1278f2SGary Mills
19630a1278f2SGary Mills if (cproj.pj_name != NULL)
19640a1278f2SGary Mills nw = mbstowcs(NULL, cproj.pj_name, 0);
19650a1278f2SGary Mills if (cproj.pj_name == NULL)
19660a1278f2SGary Mills (void) printf("%*s ", width, "---");
19670a1278f2SGary Mills else if (nw == (size_t)-1)
19680a1278f2SGary Mills (void) printf("%*s ", width, "ERROR");
19690a1278f2SGary Mills else if (Wflg && nw > width)
19700a1278f2SGary Mills (void) wprintf(L"%.*s%c", width - 1,
19710a1278f2SGary Mills cproj.pj_name, '*');
19727c478bd9Sstevel@tonic-gate else
19730a1278f2SGary Mills (void) wprintf(L"%*s", width,
19740a1278f2SGary Mills cproj.pj_name);
19750a1278f2SGary Mills }
19767c478bd9Sstevel@tonic-gate }
19777c478bd9Sstevel@tonic-gate break;
19787c478bd9Sstevel@tonic-gate case F_PSET:
19797c478bd9Sstevel@tonic-gate if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
19807c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
19817c478bd9Sstevel@tonic-gate else
19827c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
19837c478bd9Sstevel@tonic-gate break;
19847c478bd9Sstevel@tonic-gate case F_ZONEID:
19857c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_zoneid);
19867c478bd9Sstevel@tonic-gate break;
19877c478bd9Sstevel@tonic-gate case F_ZONE:
19887c478bd9Sstevel@tonic-gate {
19897c478bd9Sstevel@tonic-gate char zonename[ZONENAME_MAX];
19907c478bd9Sstevel@tonic-gate
19917c478bd9Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename,
19927c478bd9Sstevel@tonic-gate sizeof (zonename)) < 0) {
19930a1278f2SGary Mills if (Wflg && snprintf(NULL, 0, "%d",
19940a1278f2SGary Mills ((int)psinfo->pr_zoneid)) > width)
19950a1278f2SGary Mills (void) printf("%.*d%c", width - 1,
19960a1278f2SGary Mills ((int)psinfo->pr_zoneid), '*');
19970a1278f2SGary Mills else
19987c478bd9Sstevel@tonic-gate (void) printf("%*d", width,
19990a1278f2SGary Mills (int)psinfo->pr_zoneid);
20007c478bd9Sstevel@tonic-gate } else {
20010a1278f2SGary Mills size_t nw;
20020a1278f2SGary Mills
20030a1278f2SGary Mills nw = mbstowcs(NULL, zonename, 0);
20040a1278f2SGary Mills if (nw == (size_t)-1)
20050a1278f2SGary Mills (void) printf("%*s ", width, "ERROR");
20060a1278f2SGary Mills else if (Wflg && nw > width)
20070a1278f2SGary Mills (void) wprintf(L"%.*s%c", width - 1,
20080a1278f2SGary Mills zonename, '*');
20090a1278f2SGary Mills else
20100a1278f2SGary Mills (void) wprintf(L"%*s", width, zonename);
20117c478bd9Sstevel@tonic-gate }
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate break;
20147c478bd9Sstevel@tonic-gate case F_CTID:
20157c478bd9Sstevel@tonic-gate if (psinfo->pr_contract == -1)
20167c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
20177c478bd9Sstevel@tonic-gate else
20187c478bd9Sstevel@tonic-gate (void) printf("%*ld", width, (long)psinfo->pr_contract);
20197c478bd9Sstevel@tonic-gate break;
2020c6402783Sakolb case F_LGRP:
2021c6402783Sakolb /* Display home lgroup */
2022c6402783Sakolb (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
2023c6402783Sakolb break;
2024eaaa4d5fSBryan Cantrill
2025eaaa4d5fSBryan Cantrill case F_DMODEL:
2026eaaa4d5fSBryan Cantrill (void) printf("%*s", width,
2027eaaa4d5fSBryan Cantrill psinfo->pr_dmodel == PR_MODEL_LP64 ? "_LP64" : "_ILP32");
2028eaaa4d5fSBryan Cantrill break;
20297c478bd9Sstevel@tonic-gate }
20307c478bd9Sstevel@tonic-gate }
20317c478bd9Sstevel@tonic-gate
20327c478bd9Sstevel@tonic-gate static void
print_zombie_field(psinfo_t * psinfo,struct field * f,const char * ttyp)20337c478bd9Sstevel@tonic-gate print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
20347c478bd9Sstevel@tonic-gate {
20357c478bd9Sstevel@tonic-gate int wcnt;
20367c478bd9Sstevel@tonic-gate int width = f->width;
20377c478bd9Sstevel@tonic-gate
20387c478bd9Sstevel@tonic-gate switch (f->fname) {
20397c478bd9Sstevel@tonic-gate case F_FNAME:
20407c478bd9Sstevel@tonic-gate case F_COMM:
20417c478bd9Sstevel@tonic-gate case F_ARGS:
20427c478bd9Sstevel@tonic-gate /*
20437c478bd9Sstevel@tonic-gate * Print full width unless this is the last output format.
20447c478bd9Sstevel@tonic-gate */
20457c478bd9Sstevel@tonic-gate wcnt = min(width, sizeof ("<defunct>"));
20467c478bd9Sstevel@tonic-gate if (f->next != NULL)
20477c478bd9Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, "<defunct>");
20487c478bd9Sstevel@tonic-gate else
20497c478bd9Sstevel@tonic-gate (void) printf("%-.*s", wcnt, "<defunct>");
20507c478bd9Sstevel@tonic-gate break;
20517c478bd9Sstevel@tonic-gate
20527c478bd9Sstevel@tonic-gate case F_PSR:
20537c478bd9Sstevel@tonic-gate case F_PCPU:
20547c478bd9Sstevel@tonic-gate case F_PMEM:
20557c478bd9Sstevel@tonic-gate case F_NICE:
20567c478bd9Sstevel@tonic-gate case F_CLASS:
20577c478bd9Sstevel@tonic-gate case F_STIME:
20587c478bd9Sstevel@tonic-gate case F_ETIME:
20597c478bd9Sstevel@tonic-gate case F_WCHAN:
20607c478bd9Sstevel@tonic-gate case F_PSET:
20617c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-");
20627c478bd9Sstevel@tonic-gate break;
20637c478bd9Sstevel@tonic-gate
20647c478bd9Sstevel@tonic-gate case F_OPRI:
20657c478bd9Sstevel@tonic-gate case F_PRI:
20667c478bd9Sstevel@tonic-gate case F_OSZ:
20677c478bd9Sstevel@tonic-gate case F_VSZ:
20687c478bd9Sstevel@tonic-gate case F_RSS:
20697c478bd9Sstevel@tonic-gate (void) printf("%*d", width, 0);
20707c478bd9Sstevel@tonic-gate break;
20717c478bd9Sstevel@tonic-gate
20727c478bd9Sstevel@tonic-gate default:
20737c478bd9Sstevel@tonic-gate print_field(psinfo, f, ttyp);
20747c478bd9Sstevel@tonic-gate break;
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate }
20777c478bd9Sstevel@tonic-gate
20787c478bd9Sstevel@tonic-gate static void
pr_fields(psinfo_t * psinfo,const char * ttyp,void (* print_fld)(psinfo_t *,struct field *,const char *))20797c478bd9Sstevel@tonic-gate pr_fields(psinfo_t *psinfo, const char *ttyp,
20807c478bd9Sstevel@tonic-gate void (*print_fld)(psinfo_t *, struct field *, const char *))
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate struct field *f;
20837c478bd9Sstevel@tonic-gate
20847c478bd9Sstevel@tonic-gate for (f = fields; f != NULL; f = f->next) {
20857c478bd9Sstevel@tonic-gate print_fld(psinfo, f, ttyp);
20867c478bd9Sstevel@tonic-gate if (f->next != NULL)
20877c478bd9Sstevel@tonic-gate (void) printf(" ");
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate (void) printf("\n");
20907c478bd9Sstevel@tonic-gate }
20917c478bd9Sstevel@tonic-gate
20927c478bd9Sstevel@tonic-gate /*
20937c478bd9Sstevel@tonic-gate * Returns 1 if arg is found in array arr, of length num; 0 otherwise.
20947c478bd9Sstevel@tonic-gate */
20957c478bd9Sstevel@tonic-gate static int
search(pid_t * arr,int number,pid_t arg)20967c478bd9Sstevel@tonic-gate search(pid_t *arr, int number, pid_t arg)
20977c478bd9Sstevel@tonic-gate {
20987c478bd9Sstevel@tonic-gate int i;
20997c478bd9Sstevel@tonic-gate
21007c478bd9Sstevel@tonic-gate for (i = 0; i < number; i++)
21017c478bd9Sstevel@tonic-gate if (arg == arr[i])
21027c478bd9Sstevel@tonic-gate return (1);
21037c478bd9Sstevel@tonic-gate return (0);
21047c478bd9Sstevel@tonic-gate }
21057c478bd9Sstevel@tonic-gate
21067c478bd9Sstevel@tonic-gate /*
21077c478bd9Sstevel@tonic-gate * Add an entry (user, group) to the specified table.
21087c478bd9Sstevel@tonic-gate */
21097c478bd9Sstevel@tonic-gate static void
add_ugentry(struct ughead * tbl,char * name)21107c478bd9Sstevel@tonic-gate add_ugentry(struct ughead *tbl, char *name)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate struct ugdata *entp;
21137c478bd9Sstevel@tonic-gate
21147c478bd9Sstevel@tonic-gate if (tbl->size == tbl->nent) { /* reallocate the table entries */
21157c478bd9Sstevel@tonic-gate if ((tbl->size *= 2) == 0)
21167c478bd9Sstevel@tonic-gate tbl->size = 32; /* first time */
21177c478bd9Sstevel@tonic-gate tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata));
21187c478bd9Sstevel@tonic-gate }
21197c478bd9Sstevel@tonic-gate entp = &tbl->ent[tbl->nent++];
21207c478bd9Sstevel@tonic-gate entp->id = 0;
21217c478bd9Sstevel@tonic-gate (void) strncpy(entp->name, name, MAXUGNAME);
21227c478bd9Sstevel@tonic-gate entp->name[MAXUGNAME] = '\0';
21237c478bd9Sstevel@tonic-gate }
21247c478bd9Sstevel@tonic-gate
21257c478bd9Sstevel@tonic-gate static int
uconv(struct ughead * uhead)21267c478bd9Sstevel@tonic-gate uconv(struct ughead *uhead)
21277c478bd9Sstevel@tonic-gate {
21287c478bd9Sstevel@tonic-gate struct ugdata *utbl = uhead->ent;
21297c478bd9Sstevel@tonic-gate int n = uhead->nent;
21307c478bd9Sstevel@tonic-gate struct passwd *pwd;
21317c478bd9Sstevel@tonic-gate int i;
21327c478bd9Sstevel@tonic-gate int fnd = 0;
21337c478bd9Sstevel@tonic-gate uid_t uid;
21347c478bd9Sstevel@tonic-gate
21357c478bd9Sstevel@tonic-gate /*
21367c478bd9Sstevel@tonic-gate * Ask the name service for names.
21377c478bd9Sstevel@tonic-gate */
21387c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
21397c478bd9Sstevel@tonic-gate /*
21407c478bd9Sstevel@tonic-gate * If name is numeric, ask for numeric id
21417c478bd9Sstevel@tonic-gate */
2142f48205beScasper if (str2uid(utbl[i].name, &uid, 0, MAXEPHUID) == 0)
21437c478bd9Sstevel@tonic-gate pwd = getpwuid(uid);
21447c478bd9Sstevel@tonic-gate else
21457c478bd9Sstevel@tonic-gate pwd = getpwnam(utbl[i].name);
21467c478bd9Sstevel@tonic-gate
21477c478bd9Sstevel@tonic-gate /*
21487c478bd9Sstevel@tonic-gate * If found, enter found index into tbl array.
21497c478bd9Sstevel@tonic-gate */
21507c478bd9Sstevel@tonic-gate if (pwd == NULL) {
21517c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
21527c478bd9Sstevel@tonic-gate gettext("ps: unknown user %s\n"), utbl[i].name);
21537c478bd9Sstevel@tonic-gate continue;
21547c478bd9Sstevel@tonic-gate }
21557c478bd9Sstevel@tonic-gate
21567c478bd9Sstevel@tonic-gate utbl[fnd].id = pwd->pw_uid;
21577c478bd9Sstevel@tonic-gate (void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME);
21587c478bd9Sstevel@tonic-gate fnd++;
21597c478bd9Sstevel@tonic-gate }
21607c478bd9Sstevel@tonic-gate
21617c478bd9Sstevel@tonic-gate uhead->nent = fnd; /* in case it changed */
21627c478bd9Sstevel@tonic-gate return (n - fnd);
21637c478bd9Sstevel@tonic-gate }
21647c478bd9Sstevel@tonic-gate
21657c478bd9Sstevel@tonic-gate static int
gconv(struct ughead * ghead)21667c478bd9Sstevel@tonic-gate gconv(struct ughead *ghead)
21677c478bd9Sstevel@tonic-gate {
21687c478bd9Sstevel@tonic-gate struct ugdata *gtbl = ghead->ent;
21697c478bd9Sstevel@tonic-gate int n = ghead->nent;
21707c478bd9Sstevel@tonic-gate struct group *grp;
21717c478bd9Sstevel@tonic-gate gid_t gid;
21727c478bd9Sstevel@tonic-gate int i;
21737c478bd9Sstevel@tonic-gate int fnd = 0;
21747c478bd9Sstevel@tonic-gate
21757c478bd9Sstevel@tonic-gate /*
21767c478bd9Sstevel@tonic-gate * Ask the name service for names.
21777c478bd9Sstevel@tonic-gate */
21787c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
21797c478bd9Sstevel@tonic-gate /*
21807c478bd9Sstevel@tonic-gate * If name is numeric, ask for numeric id
21817c478bd9Sstevel@tonic-gate */
2182f48205beScasper if (str2uid(gtbl[i].name, (uid_t *)&gid, 0, MAXEPHUID) == 0)
21837c478bd9Sstevel@tonic-gate grp = getgrgid(gid);
21847c478bd9Sstevel@tonic-gate else
21857c478bd9Sstevel@tonic-gate grp = getgrnam(gtbl[i].name);
21867c478bd9Sstevel@tonic-gate /*
21877c478bd9Sstevel@tonic-gate * If found, enter found index into tbl array.
21887c478bd9Sstevel@tonic-gate */
21897c478bd9Sstevel@tonic-gate if (grp == NULL) {
21907c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
21917c478bd9Sstevel@tonic-gate gettext("ps: unknown group %s\n"), gtbl[i].name);
21927c478bd9Sstevel@tonic-gate continue;
21937c478bd9Sstevel@tonic-gate }
21947c478bd9Sstevel@tonic-gate
21957c478bd9Sstevel@tonic-gate gtbl[fnd].id = grp->gr_gid;
21967c478bd9Sstevel@tonic-gate (void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME);
21977c478bd9Sstevel@tonic-gate fnd++;
21987c478bd9Sstevel@tonic-gate }
21997c478bd9Sstevel@tonic-gate
22007c478bd9Sstevel@tonic-gate ghead->nent = fnd; /* in case it changed */
22017c478bd9Sstevel@tonic-gate return (n - fnd);
22027c478bd9Sstevel@tonic-gate }
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate /*
22057c478bd9Sstevel@tonic-gate * Return 1 if puid is in table, otherwise 0.
22067c478bd9Sstevel@tonic-gate */
22077c478bd9Sstevel@tonic-gate static int
ugfind(id_t id,struct ughead * ughead)22087c478bd9Sstevel@tonic-gate ugfind(id_t id, struct ughead *ughead)
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate struct ugdata *utbl = ughead->ent;
22117c478bd9Sstevel@tonic-gate int n = ughead->nent;
22127c478bd9Sstevel@tonic-gate int i;
22137c478bd9Sstevel@tonic-gate
22147c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++)
22157c478bd9Sstevel@tonic-gate if (utbl[i].id == id)
22167c478bd9Sstevel@tonic-gate return (1);
22177c478bd9Sstevel@tonic-gate return (0);
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate
22207c478bd9Sstevel@tonic-gate /*
22217c478bd9Sstevel@tonic-gate * Print starting time of process unless process started more than 24 hours
22227c478bd9Sstevel@tonic-gate * ago, in which case the date is printed. The date is printed in the form
22237c478bd9Sstevel@tonic-gate * "MMM dd" if old format, else the blank is replaced with an '_' so
22247c478bd9Sstevel@tonic-gate * it appears as a single word (for parseability).
22257c478bd9Sstevel@tonic-gate */
22267c478bd9Sstevel@tonic-gate static void
prtime(timestruc_t st,int width,int old)22277c478bd9Sstevel@tonic-gate prtime(timestruc_t st, int width, int old)
22287c478bd9Sstevel@tonic-gate {
22297c478bd9Sstevel@tonic-gate char sttim[26];
22307c478bd9Sstevel@tonic-gate time_t starttime;
22317c478bd9Sstevel@tonic-gate
22327c478bd9Sstevel@tonic-gate starttime = st.tv_sec;
22337c478bd9Sstevel@tonic-gate if (st.tv_nsec > 500000000)
22347c478bd9Sstevel@tonic-gate starttime++;
22357c478bd9Sstevel@tonic-gate if ((now.tv_sec - starttime) >= 24*60*60) {
223616f94f58Ssayama (void) strftime(sttim, sizeof (sttim), old?
223716f94f58Ssayama /*
223816f94f58Ssayama * TRANSLATION_NOTE
223916f94f58Ssayama * This time format is used by STIME field when -f option
224016f94f58Ssayama * is specified. Used for processes that begun more than
224116f94f58Ssayama * 24 hours.
224216f94f58Ssayama */
224316f94f58Ssayama dcgettext(NULL, "%b %d", LC_TIME) :
224416f94f58Ssayama /*
224516f94f58Ssayama * TRANSLATION_NOTE
224616f94f58Ssayama * This time format is used by STIME field when -o option
224716f94f58Ssayama * is specified. Used for processes that begun more than
224816f94f58Ssayama * 24 hours.
224916f94f58Ssayama */
225016f94f58Ssayama dcgettext(NULL, "%b_%d", LC_TIME), localtime(&starttime));
22517c478bd9Sstevel@tonic-gate } else {
225216f94f58Ssayama /*
225316f94f58Ssayama * TRANSLATION_NOTE
225416f94f58Ssayama * This time format is used by STIME field when -f or -o option
225516f94f58Ssayama * is specified. Used for processes that begun less than
225616f94f58Ssayama * 24 hours.
225716f94f58Ssayama */
225816f94f58Ssayama (void) strftime(sttim, sizeof (sttim),
225916f94f58Ssayama dcgettext(NULL, "%H:%M:%S", LC_TIME),
226016f94f58Ssayama localtime(&starttime));
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width, sttim);
22637c478bd9Sstevel@tonic-gate }
22647c478bd9Sstevel@tonic-gate
22657c478bd9Sstevel@tonic-gate static void
przom(psinfo_t * psinfo)22667c478bd9Sstevel@tonic-gate przom(psinfo_t *psinfo)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate long tm;
22697c478bd9Sstevel@tonic-gate struct passwd *pwd;
22707c478bd9Sstevel@tonic-gate char zonename[ZONENAME_MAX];
22717c478bd9Sstevel@tonic-gate
22727c478bd9Sstevel@tonic-gate /*
22737c478bd9Sstevel@tonic-gate * All fields before 'PID' are printed with a trailing space as a
22747c478bd9Sstevel@tonic-gate * spearator, rather than keeping track of which column is first. All
22757c478bd9Sstevel@tonic-gate * other fields are printed with a leading space.
22767c478bd9Sstevel@tonic-gate */
22777c478bd9Sstevel@tonic-gate if (lflg) { /* F S */
22787c478bd9Sstevel@tonic-gate if (!yflg)
22797c478bd9Sstevel@tonic-gate (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
22807c478bd9Sstevel@tonic-gate (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate if (Zflg) {
22837c478bd9Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename,
22847c478bd9Sstevel@tonic-gate sizeof (zonename)) < 0) {
22850a1278f2SGary Mills if (snprintf(NULL, 0, "%d",
22860a1278f2SGary Mills ((int)psinfo->pr_zoneid)) > 7)
22870a1278f2SGary Mills (void) printf(" %6.6d%c ",
22880a1278f2SGary Mills ((int)psinfo->pr_zoneid), '*');
22890a1278f2SGary Mills else
22900a1278f2SGary Mills (void) printf(" %7.7d ",
22910a1278f2SGary Mills ((int)psinfo->pr_zoneid));
22927c478bd9Sstevel@tonic-gate } else {
22930a1278f2SGary Mills size_t nw;
22940a1278f2SGary Mills
22950a1278f2SGary Mills nw = mbstowcs(NULL, zonename, 0);
22960a1278f2SGary Mills if (nw == (size_t)-1)
22970a1278f2SGary Mills (void) printf("%8.8s ", "ERROR");
22980a1278f2SGary Mills else if (nw > 8)
22990a1278f2SGary Mills (void) wprintf(L"%7.7s%c ", zonename, '*');
23000a1278f2SGary Mills else
23010a1278f2SGary Mills (void) wprintf(L"%8.8s ", zonename);
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate }
2304c6402783Sakolb if (Hflg) {
2305c6402783Sakolb /* Display home lgroup */
2306c6402783Sakolb (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2307c6402783Sakolb }
23087c478bd9Sstevel@tonic-gate if (fflg) {
23090a1278f2SGary Mills if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
23100a1278f2SGary Mills size_t nw;
23110a1278f2SGary Mills
23120a1278f2SGary Mills nw = mbstowcs(NULL, pwd->pw_name, 0);
23130a1278f2SGary Mills if (nw == (size_t)-1)
23140a1278f2SGary Mills (void) printf("%8.8s ", "ERROR");
23150a1278f2SGary Mills else if (nw > 8)
23160a1278f2SGary Mills (void) wprintf(L"%7.7s%c ", pwd->pw_name, '*');
23170a1278f2SGary Mills else
23180a1278f2SGary Mills (void) wprintf(L"%8.8s ", pwd->pw_name);
23190a1278f2SGary Mills } else {
23200a1278f2SGary Mills if (snprintf(NULL, 0, "%u",
23210a1278f2SGary Mills (psinfo->pr_euid)) > 7)
23220a1278f2SGary Mills (void) printf(" %6.6u%c ", psinfo->pr_euid,
23230a1278f2SGary Mills '*');
23247c478bd9Sstevel@tonic-gate else
2325f48205beScasper (void) printf(" %7.7u ", psinfo->pr_euid);
23260a1278f2SGary Mills }
23270a1278f2SGary Mills } else if (lflg) {
23280a1278f2SGary Mills if (snprintf(NULL, 0, "%u", (psinfo->pr_euid)) > 6)
23290a1278f2SGary Mills (void) printf("%5.5u%c ", psinfo->pr_euid, '*');
23300a1278f2SGary Mills else
2331f48205beScasper (void) printf("%6u ", psinfo->pr_euid);
23320a1278f2SGary Mills }
23337c478bd9Sstevel@tonic-gate
23347c478bd9Sstevel@tonic-gate (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
23357c478bd9Sstevel@tonic-gate if (lflg || fflg)
23367c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth,
23377c478bd9Sstevel@tonic-gate (int)psinfo->pr_ppid); /* PPID */
23387c478bd9Sstevel@tonic-gate
23397c478bd9Sstevel@tonic-gate if (jflg) {
23407c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth,
23417c478bd9Sstevel@tonic-gate (int)psinfo->pr_pgid); /* PGID */
23427c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth,
23437c478bd9Sstevel@tonic-gate (int)psinfo->pr_sid); /* SID */
23447c478bd9Sstevel@tonic-gate }
23457c478bd9Sstevel@tonic-gate
23467c478bd9Sstevel@tonic-gate if (Lflg)
23477c478bd9Sstevel@tonic-gate (void) printf(" %5d", 0); /* LWP */
23487c478bd9Sstevel@tonic-gate if (Pflg)
23497c478bd9Sstevel@tonic-gate (void) printf(" -"); /* PSR */
23507c478bd9Sstevel@tonic-gate if (Lflg && fflg)
23517c478bd9Sstevel@tonic-gate (void) printf(" %5d", 0); /* NLWP */
23527c478bd9Sstevel@tonic-gate
23537c478bd9Sstevel@tonic-gate if (cflg) {
23547c478bd9Sstevel@tonic-gate (void) printf(" %4s", "-"); /* zombies have no class */
23557c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */
23567c478bd9Sstevel@tonic-gate } else if (lflg || fflg) {
23577c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */
23587c478bd9Sstevel@tonic-gate if (lflg)
23597c478bd9Sstevel@tonic-gate (void) printf(" %3d %2s",
23607c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_oldpri, "-"); /* PRI NI */
23617c478bd9Sstevel@tonic-gate }
23627c478bd9Sstevel@tonic-gate if (lflg) {
23637c478bd9Sstevel@tonic-gate if (yflg) /* RSS SZ WCHAN */
23647c478bd9Sstevel@tonic-gate (void) printf(" %5d %6d %8s", 0, 0, "-");
23657c478bd9Sstevel@tonic-gate else /* ADDR SZ WCHAN */
23667c478bd9Sstevel@tonic-gate (void) printf(" %8s %6d %8s", "-", 0, "-");
23677c478bd9Sstevel@tonic-gate }
236816f94f58Ssayama if (fflg) {
236916f94f58Ssayama int width = fname[F_STIME].width;
237016f94f58Ssayama (void) printf(" %*.*s", width, width, "-"); /* STIME */
237116f94f58Ssayama }
23727c478bd9Sstevel@tonic-gate (void) printf(" %-8.14s", "?"); /* TTY */
23737c478bd9Sstevel@tonic-gate
23747c478bd9Sstevel@tonic-gate tm = psinfo->pr_time.tv_sec;
23757c478bd9Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000)
23767c478bd9Sstevel@tonic-gate tm++;
23777c478bd9Sstevel@tonic-gate (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* TIME */
23787c478bd9Sstevel@tonic-gate (void) printf(" <defunct>\n");
23797c478bd9Sstevel@tonic-gate }
23807c478bd9Sstevel@tonic-gate
23817c478bd9Sstevel@tonic-gate /*
23827c478bd9Sstevel@tonic-gate * Function to compute the number of printable bytes in a multibyte
23837c478bd9Sstevel@tonic-gate * command string ("internationalization").
23847c478bd9Sstevel@tonic-gate */
23857c478bd9Sstevel@tonic-gate static int
namencnt(char * cmd,int csisize,int scrsize)23867c478bd9Sstevel@tonic-gate namencnt(char *cmd, int csisize, int scrsize)
23877c478bd9Sstevel@tonic-gate {
23887c478bd9Sstevel@tonic-gate int csiwcnt = 0, scrwcnt = 0;
23897c478bd9Sstevel@tonic-gate int ncsisz, nscrsz;
23907c478bd9Sstevel@tonic-gate wchar_t wchar;
23917c478bd9Sstevel@tonic-gate int len;
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate while (*cmd != '\0') {
23947c478bd9Sstevel@tonic-gate if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX)
23957c478bd9Sstevel@tonic-gate len = MB_CUR_MAX;
23967c478bd9Sstevel@tonic-gate if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0)
23977c478bd9Sstevel@tonic-gate return (8); /* default to use for illegal chars */
23987c478bd9Sstevel@tonic-gate if ((nscrsz = wcwidth(wchar)) <= 0)
23997c478bd9Sstevel@tonic-gate return (8);
24007c478bd9Sstevel@tonic-gate if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize)
24017c478bd9Sstevel@tonic-gate break;
24027c478bd9Sstevel@tonic-gate csiwcnt += ncsisz;
24037c478bd9Sstevel@tonic-gate scrwcnt += nscrsz;
24047c478bd9Sstevel@tonic-gate cmd += ncsisz;
24057c478bd9Sstevel@tonic-gate }
24067c478bd9Sstevel@tonic-gate return (csiwcnt);
24077c478bd9Sstevel@tonic-gate }
24087c478bd9Sstevel@tonic-gate
24097c478bd9Sstevel@tonic-gate static char *
err_string(int err)24107c478bd9Sstevel@tonic-gate err_string(int err)
24117c478bd9Sstevel@tonic-gate {
24127c478bd9Sstevel@tonic-gate static char buf[32];
24137c478bd9Sstevel@tonic-gate char *str = strerror(err);
24147c478bd9Sstevel@tonic-gate
24157c478bd9Sstevel@tonic-gate if (str == NULL)
24167c478bd9Sstevel@tonic-gate (void) snprintf(str = buf, sizeof (buf), "Errno #%d", err);
24177c478bd9Sstevel@tonic-gate
24187c478bd9Sstevel@tonic-gate return (str);
24197c478bd9Sstevel@tonic-gate }
24207c478bd9Sstevel@tonic-gate
24217c478bd9Sstevel@tonic-gate /* If allocation fails, die */
24227c478bd9Sstevel@tonic-gate static void *
Realloc(void * ptr,size_t size)24237c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size)
24247c478bd9Sstevel@tonic-gate {
24257c478bd9Sstevel@tonic-gate ptr = realloc(ptr, size);
24267c478bd9Sstevel@tonic-gate if (ptr == NULL) {
24277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("ps: no memory\n"));
24287c478bd9Sstevel@tonic-gate exit(1);
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate return (ptr);
24317c478bd9Sstevel@tonic-gate }
24327c478bd9Sstevel@tonic-gate
24337c478bd9Sstevel@tonic-gate static time_t
delta_secs(const timestruc_t * start)24347c478bd9Sstevel@tonic-gate delta_secs(const timestruc_t *start)
24357c478bd9Sstevel@tonic-gate {
24367c478bd9Sstevel@tonic-gate time_t seconds = now.tv_sec - start->tv_sec;
24377c478bd9Sstevel@tonic-gate long nanosecs = now.tv_usec * 1000 - start->tv_nsec;
24387c478bd9Sstevel@tonic-gate
24397c478bd9Sstevel@tonic-gate if (nanosecs >= (NANOSEC / 2))
24407c478bd9Sstevel@tonic-gate seconds++;
24417c478bd9Sstevel@tonic-gate else if (nanosecs < -(NANOSEC / 2))
24427c478bd9Sstevel@tonic-gate seconds--;
24437c478bd9Sstevel@tonic-gate
24447c478bd9Sstevel@tonic-gate return (seconds);
24457c478bd9Sstevel@tonic-gate }
24467c478bd9Sstevel@tonic-gate
24477c478bd9Sstevel@tonic-gate /*
24487c478bd9Sstevel@tonic-gate * Returns the following:
24497c478bd9Sstevel@tonic-gate *
24507c478bd9Sstevel@tonic-gate * 0 No error
24517c478bd9Sstevel@tonic-gate * EINVAL Invalid number
24527c478bd9Sstevel@tonic-gate * ERANGE Value exceeds (min, max) range
24537c478bd9Sstevel@tonic-gate */
24547c478bd9Sstevel@tonic-gate static int
str2id(const char * p,pid_t * val,long min,long max)24557c478bd9Sstevel@tonic-gate str2id(const char *p, pid_t *val, long min, long max)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate char *q;
24587c478bd9Sstevel@tonic-gate long number;
24597c478bd9Sstevel@tonic-gate int error;
24607c478bd9Sstevel@tonic-gate
24617c478bd9Sstevel@tonic-gate errno = 0;
24627c478bd9Sstevel@tonic-gate number = strtol(p, &q, 10);
24637c478bd9Sstevel@tonic-gate
24647c478bd9Sstevel@tonic-gate if (errno != 0 || q == p || *q != '\0') {
24657c478bd9Sstevel@tonic-gate if ((error = errno) == 0) {
24667c478bd9Sstevel@tonic-gate /*
24677c478bd9Sstevel@tonic-gate * strtol() can fail without setting errno, or it can
24687c478bd9Sstevel@tonic-gate * set it to EINVAL or ERANGE. In the case errno is
24697c478bd9Sstevel@tonic-gate * still zero, return EINVAL.
24707c478bd9Sstevel@tonic-gate */
24717c478bd9Sstevel@tonic-gate error = EINVAL;
24727c478bd9Sstevel@tonic-gate }
24737c478bd9Sstevel@tonic-gate } else if (number < min || number > max) {
24747c478bd9Sstevel@tonic-gate error = ERANGE;
24757c478bd9Sstevel@tonic-gate } else {
24767c478bd9Sstevel@tonic-gate error = 0;
24777c478bd9Sstevel@tonic-gate }
24787c478bd9Sstevel@tonic-gate
24797c478bd9Sstevel@tonic-gate *val = number;
24807c478bd9Sstevel@tonic-gate
24817c478bd9Sstevel@tonic-gate return (error);
24827c478bd9Sstevel@tonic-gate }
24837c478bd9Sstevel@tonic-gate
2484f48205beScasper /*
2485f48205beScasper * Returns the following:
2486f48205beScasper *
2487f48205beScasper * 0 No error
2488f48205beScasper * EINVAL Invalid number
2489f48205beScasper * ERANGE Value exceeds (min, max) range
2490f48205beScasper */
2491f48205beScasper static int
str2uid(const char * p,uid_t * val,unsigned long min,unsigned long max)2492f48205beScasper str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
2493f48205beScasper {
2494f48205beScasper char *q;
2495f48205beScasper unsigned long number;
2496f48205beScasper int error;
2497f48205beScasper
2498f48205beScasper errno = 0;
2499f48205beScasper number = strtoul(p, &q, 10);
2500f48205beScasper
2501f48205beScasper if (errno != 0 || q == p || *q != '\0') {
2502f48205beScasper if ((error = errno) == 0) {
2503f48205beScasper /*
2504f48205beScasper * strtoul() can fail without setting errno, or it can
2505f48205beScasper * set it to EINVAL or ERANGE. In the case errno is
2506f48205beScasper * still zero, return EINVAL.
2507f48205beScasper */
2508f48205beScasper error = EINVAL;
2509f48205beScasper }
2510f48205beScasper } else if (number < min || number > max) {
2511f48205beScasper error = ERANGE;
2512f48205beScasper } else {
2513f48205beScasper error = 0;
2514f48205beScasper }
2515f48205beScasper
2516f48205beScasper *val = number;
2517f48205beScasper
2518f48205beScasper return (error);
2519f48205beScasper }
2520f48205beScasper
25217c478bd9Sstevel@tonic-gate static int
pidcmp(const void * p1,const void * p2)25227c478bd9Sstevel@tonic-gate pidcmp(const void *p1, const void *p2)
25237c478bd9Sstevel@tonic-gate {
25247c478bd9Sstevel@tonic-gate pid_t i = *((pid_t *)p1);
25257c478bd9Sstevel@tonic-gate pid_t j = *((pid_t *)p2);
25267c478bd9Sstevel@tonic-gate
25277c478bd9Sstevel@tonic-gate return (i - j);
25287c478bd9Sstevel@tonic-gate }
2529