]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - bin/ps/print.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / bin / ps / print.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)print.c     8.6 (Berkeley) 4/16/94";
33 #endif /* not lint */
34 #endif
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/proc.h>
43 #include <sys/stat.h>
44
45 #include <sys/mac.h>
46 #include <sys/user.h>
47 #include <sys/sysctl.h>
48
49 #include <err.h>
50 #include <grp.h>
51 #include <langinfo.h>
52 #include <locale.h>
53 #include <math.h>
54 #include <nlist.h>
55 #include <pwd.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <vis.h>
62
63 #include "ps.h"
64
65 #define ps_pgtok(a)     (((a) * getpagesize()) / 1024)
66
67 void
68 printheader(void)
69 {
70         VAR *v;
71         struct varent *vent;
72
73         STAILQ_FOREACH(vent, &varlist, next_ve)
74                 if (*vent->header != '\0')
75                         break;
76         if (!vent)
77                 return;
78
79         STAILQ_FOREACH(vent, &varlist, next_ve) {
80                 v = vent->var;
81                 if (v->flag & LJUST) {
82                         if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */
83                                 (void)printf("%s", vent->header);
84                         else
85                                 (void)printf("%-*s", v->width, vent->header);
86                 } else
87                         (void)printf("%*s", v->width, vent->header);
88                 if (STAILQ_NEXT(vent, next_ve) != NULL)
89                         (void)putchar(' ');
90         }
91         (void)putchar('\n');
92 }
93
94 void
95 arguments(KINFO *k, VARENT *ve)
96 {
97         VAR *v;
98         int left;
99         char *cp, *vis_args;
100
101         v = ve->var;
102         if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
103                 errx(1, "malloc failed");
104         strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
105         if (STAILQ_NEXT(ve, next_ve) == NULL) {
106                 /* last field */
107                 if (termwidth == UNLIMITED) {
108                         (void)printf("%s", vis_args);
109                 } else {
110                         left = termwidth - (totwidth - v->width);
111                         if (left < 1) /* already wrapped, just use std width */
112                                 left = v->width;
113                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
114                                 (void)putchar(*cp++);
115                 }
116         } else {
117                 (void)printf("%-*.*s", v->width, v->width, vis_args);
118         }
119         free(vis_args);
120 }
121
122 void
123 command(KINFO *k, VARENT *ve)
124 {
125         VAR *v;
126         int left;
127         char *cp, *vis_env, *vis_args;
128
129         v = ve->var;
130         if (cflag) {
131                 /* If it is the last field, then don't pad */
132                 if (STAILQ_NEXT(ve, next_ve) == NULL) {
133                         if (k->ki_d.prefix)
134                                 (void)printf("%s", k->ki_d.prefix);
135                         (void)printf("%s", k->ki_p->ki_comm);
136                         if (showthreads && k->ki_p->ki_numthreads > 1)
137                                 (void)printf("/%s", k->ki_p->ki_ocomm);
138                 } else
139                         (void)printf("%-*s", v->width, k->ki_p->ki_comm);
140                 return;
141         }
142         if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
143                 errx(1, "malloc failed");
144         strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
145
146         if (STAILQ_NEXT(ve, next_ve) == NULL) {
147                 /* last field */
148
149                 if (k->ki_env) {
150                         if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
151                             == NULL)
152                                 errx(1, "malloc failed");
153                         strvis(vis_env, k->ki_env,
154                             VIS_TAB | VIS_NL | VIS_NOSLASH);
155                 } else
156                         vis_env = NULL;
157
158                 if (termwidth == UNLIMITED) {
159                         if (k->ki_d.prefix)
160                                 (void)printf("%s", k->ki_d.prefix);
161                         if (vis_env)
162                                 (void)printf("%s ", vis_env);
163                         (void)printf("%s", vis_args);
164                 } else {
165                         left = termwidth - (totwidth - v->width);
166                         if (left < 1) /* already wrapped, just use std width */
167                                 left = v->width;
168                         if ((cp = k->ki_d.prefix) != NULL)
169                                 while (--left >= 0 && *cp)
170                                         (void)putchar(*cp++);
171                         if ((cp = vis_env) != NULL) {
172                                 while (--left >= 0 && *cp)
173                                         (void)putchar(*cp++);
174                                 if (--left >= 0)
175                                         putchar(' ');
176                         }
177                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
178                                 (void)putchar(*cp++);
179                 }
180                 if (vis_env != NULL)
181                         free(vis_env);
182         } else
183                 /* ki_d.prefix & ki_env aren't shown for interim fields */
184                 (void)printf("%-*.*s", v->width, v->width, vis_args);
185         free(vis_args);
186 }
187
188 void
189 ucomm(KINFO *k, VARENT *ve)
190 {
191         char tmpbuff[COMMLEN + OCOMMLEN + 2];
192         VAR *v;
193
194         v = ve->var;
195         if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */
196                 if (k->ki_d.prefix)
197                         (void)printf("%s", k->ki_d.prefix);
198                 (void)printf("%s", k->ki_p->ki_comm);
199                 if (showthreads && k->ki_p->ki_numthreads > 1)
200                         printf("/%s", k->ki_p->ki_ocomm);
201         } else {
202                 bzero(tmpbuff, sizeof(tmpbuff));
203                 if (showthreads && k->ki_p->ki_numthreads > 1)
204                         sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
205                             k->ki_p->ki_ocomm);
206                 else
207                         sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
208                 (void)printf("%-*s", v->width, tmpbuff);
209         }
210 }
211
212 void
213 tdnam(KINFO *k, VARENT *ve)
214 {
215         VAR *v;
216
217         v = ve->var;
218         if (showthreads && k->ki_p->ki_numthreads > 1)
219                 (void)printf("%-*s", v->width, k->ki_p->ki_ocomm);
220         else
221                 (void)printf("%-*s", v->width, "      ");
222 }
223
224 void
225 logname(KINFO *k, VARENT *ve)
226 {
227         VAR *v;
228         char *s;
229
230         v = ve->var;
231         (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
232 }
233
234 void
235 state(KINFO *k, VARENT *ve)
236 {
237         int flag, tdflags;
238         char *cp;
239         VAR *v;
240         char buf[16];
241
242         v = ve->var;
243         flag = k->ki_p->ki_flag;
244         tdflags = k->ki_p->ki_tdflags;  /* XXXKSE */
245         cp = buf;
246
247         switch (k->ki_p->ki_stat) {
248
249         case SSTOP:
250                 *cp = 'T';
251                 break;
252
253         case SSLEEP:
254                 if (tdflags & TDF_SINTR)        /* interruptable (long) */
255                         *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
256                 else
257                         *cp = 'D';
258                 break;
259
260         case SRUN:
261         case SIDL:
262                 *cp = 'R';
263                 break;
264
265         case SWAIT:
266                 *cp = 'W';
267                 break;
268
269         case SLOCK:
270                 *cp = 'L';
271                 break;
272
273         case SZOMB:
274                 *cp = 'Z';
275                 break;
276
277         default:
278                 *cp = '?';
279         }
280         cp++;
281         if (!(flag & P_INMEM))
282                 *cp++ = 'W';
283         if (k->ki_p->ki_nice < NZERO)
284                 *cp++ = '<';
285         else if (k->ki_p->ki_nice > NZERO)
286                 *cp++ = 'N';
287         if (flag & P_TRACED)
288                 *cp++ = 'X';
289         if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
290                 *cp++ = 'E';
291         if (flag & P_PPWAIT)
292                 *cp++ = 'V';
293         if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
294                 *cp++ = 'L';
295         if (k->ki_p->ki_kiflag & KI_SLEADER)
296                 *cp++ = 's';
297         if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
298                 *cp++ = '+';
299         if (flag & P_JAILED)
300                 *cp++ = 'J';
301         *cp = '\0';
302         (void)printf("%-*s", v->width, buf);
303 }
304
305 #define scalepri(x)     ((x) - PZERO)
306
307 void
308 pri(KINFO *k, VARENT *ve)
309 {
310         VAR *v;
311
312         v = ve->var;
313         (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
314 }
315
316 void
317 upr(KINFO *k, VARENT *ve)
318 {
319         VAR *v;
320
321         v = ve->var;
322         (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
323 }
324 #undef scalepri
325
326 void
327 uname(KINFO *k, VARENT *ve)
328 {
329         VAR *v;
330
331         v = ve->var;
332         (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
333 }
334
335 int
336 s_uname(KINFO *k)
337 {
338         return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
339 }
340
341 void
342 rgroupname(KINFO *k, VARENT *ve)
343 {
344         VAR *v;
345
346         v = ve->var;
347         (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
348 }
349
350 int
351 s_rgroupname(KINFO *k)
352 {
353         return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
354 }
355
356 void
357 runame(KINFO *k, VARENT *ve)
358 {
359         VAR *v;
360
361         v = ve->var;
362         (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
363 }
364
365 int
366 s_runame(KINFO *k)
367 {
368         return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
369 }
370
371
372 void
373 tdev(KINFO *k, VARENT *ve)
374 {
375         VAR *v;
376         dev_t dev;
377         char buff[16];
378
379         v = ve->var;
380         dev = k->ki_p->ki_tdev;
381         if (dev == NODEV)
382                 (void)printf("%*s", v->width, "??");
383         else {
384                 (void)snprintf(buff, sizeof(buff),
385                     "%d/%d", major(dev), minor(dev));
386                 (void)printf("%*s", v->width, buff);
387         }
388 }
389
390 void
391 tname(KINFO *k, VARENT *ve)
392 {
393         VAR *v;
394         dev_t dev;
395         char *ttname;
396
397         v = ve->var;
398         dev = k->ki_p->ki_tdev;
399         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
400                 (void)printf("%*s ", v->width - 1, "??");
401         else {
402                 if (strncmp(ttname, "tty", 3) == 0 ||
403                     strncmp(ttname, "cua", 3) == 0)
404                         ttname += 3;
405                 if (strncmp(ttname, "pts/", 4) == 0)
406                         ttname += 4;
407                 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
408                     k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
409         }
410 }
411
412 void
413 longtname(KINFO *k, VARENT *ve)
414 {
415         VAR *v;
416         dev_t dev;
417         char *ttname;
418
419         v = ve->var;
420         dev = k->ki_p->ki_tdev;
421         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
422                 (void)printf("%-*s", v->width, "??");
423         else
424                 (void)printf("%-*s", v->width, ttname);
425 }
426
427 void
428 started(KINFO *k, VARENT *ve)
429 {
430         VAR *v;
431         time_t then;
432         struct tm *tp;
433         static int use_ampm = -1;
434         char buf[100];
435
436         v = ve->var;
437         if (!k->ki_valid) {
438                 (void)printf("%-*s", v->width, "-");
439                 return;
440         }
441         if (use_ampm < 0)
442                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
443         then = k->ki_p->ki_start.tv_sec;
444         tp = localtime(&then);
445         if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
446                 (void)strftime(buf, sizeof(buf),
447                     use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
448         } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
449                 (void)strftime(buf, sizeof(buf),
450                     use_ampm ? "%a%I%p" : "%a%H  ", tp);
451         } else
452                 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
453         (void)printf("%-*s", v->width, buf);
454 }
455
456 void
457 lstarted(KINFO *k, VARENT *ve)
458 {
459         VAR *v;
460         time_t then;
461         char buf[100];
462
463         v = ve->var;
464         if (!k->ki_valid) {
465                 (void)printf("%-*s", v->width, "-");
466                 return;
467         }
468         then = k->ki_p->ki_start.tv_sec;
469         (void)strftime(buf, sizeof(buf), "%c", localtime(&then));
470         (void)printf("%-*s", v->width, buf);
471 }
472
473 void
474 lockname(KINFO *k, VARENT *ve)
475 {
476         VAR *v;
477
478         v = ve->var;
479         if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
480                 if (k->ki_p->ki_lockname[0] != 0)
481                         (void)printf("%-*.*s", v->width, v->width,
482                             k->ki_p->ki_lockname);
483                 else
484                         (void)printf("%-*s", v->width, "???");
485         } else
486                 (void)printf("%-*s", v->width, "-");
487 }
488
489 void
490 wchan(KINFO *k, VARENT *ve)
491 {
492         VAR *v;
493
494         v = ve->var;
495         if (k->ki_p->ki_wchan) {
496                 if (k->ki_p->ki_wmesg[0] != 0)
497                         (void)printf("%-*.*s", v->width, v->width,
498                             k->ki_p->ki_wmesg);
499                 else
500                         (void)printf("%-*lx", v->width,
501                             (long)k->ki_p->ki_wchan);
502         } else
503                 (void)printf("%-*s", v->width, "-");
504 }
505
506 void
507 nwchan(KINFO *k, VARENT *ve)
508 {
509         VAR *v;
510
511         v = ve->var;
512         if (k->ki_p->ki_wchan) {
513                 (void)printf("%0*lx", v->width,
514                     (long)k->ki_p->ki_wchan);
515         } else
516                 (void)printf("%-*s", v->width, "-");
517 }
518
519 void
520 mwchan(KINFO *k, VARENT *ve)
521 {
522         VAR *v;
523
524         v = ve->var;
525         if (k->ki_p->ki_wchan) {
526                 if (k->ki_p->ki_wmesg[0] != 0)
527                         (void)printf("%-*.*s", v->width, v->width,
528                             k->ki_p->ki_wmesg);
529                 else
530                         (void)printf("%-*lx", v->width,
531                             (long)k->ki_p->ki_wchan);
532         } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
533                 if (k->ki_p->ki_lockname[0]) {
534                         (void)printf("%-*.*s", v->width, v->width,
535                             k->ki_p->ki_lockname);
536                 } else
537                         (void)printf("%-*s", v->width, "???");
538         } else
539                 (void)printf("%-*s", v->width, "-");
540 }
541
542 void
543 vsize(KINFO *k, VARENT *ve)
544 {
545         VAR *v;
546
547         v = ve->var;
548         (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
549 }
550
551 void
552 cputime(KINFO *k, VARENT *ve)
553 {
554         VAR *v;
555         long secs;
556         long psecs;     /* "parts" of a second. first micro, then centi */
557         char obuff[128];
558         static char decimal_point;
559
560         if (decimal_point == '\0')
561                 decimal_point = localeconv()->decimal_point[0];
562         v = ve->var;
563         if (!k->ki_valid) {
564                 secs = 0;
565                 psecs = 0;
566         } else {
567                 /*
568                  * This counts time spent handling interrupts.  We could
569                  * fix this, but it is not 100% trivial (and interrupt
570                  * time fractions only work on the sparc anyway).       XXX
571                  */
572                 secs = k->ki_p->ki_runtime / 1000000;
573                 psecs = k->ki_p->ki_runtime % 1000000;
574                 if (sumrusage) {
575                         secs += k->ki_p->ki_childtime.tv_sec;
576                         psecs += k->ki_p->ki_childtime.tv_usec;
577                 }
578                 /*
579                  * round and scale to 100's
580                  */
581                 psecs = (psecs + 5000) / 10000;
582                 secs += psecs / 100;
583                 psecs = psecs % 100;
584         }
585         (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
586             secs / 60, secs % 60, decimal_point, psecs);
587         (void)printf("%*s", v->width, obuff);
588 }
589
590 void
591 elapsed(KINFO *k, VARENT *ve)
592 {
593         VAR *v;
594         time_t val;
595         int days, hours, mins, secs;
596         char obuff[128];
597
598         v = ve->var;
599         if (!k->ki_valid) {
600                 (void)printf("%-*s", v->width, "-");
601                 return;
602         }
603         val = now - k->ki_p->ki_start.tv_sec;
604         days = val / (24 * 60 * 60);
605         val %= 24 * 60 * 60;
606         hours = val / (60 * 60);
607         val %= 60 * 60;
608         mins = val / 60;
609         secs = val % 60;
610         if (days != 0)
611                 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
612                     days, hours, mins, secs);
613         else if (hours != 0)
614                 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
615                     hours, mins, secs);
616         else
617                 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
618         (void)printf("%*s", v->width, obuff);
619 }
620
621 double
622 getpcpu(const KINFO *k)
623 {
624         static int failure;
625
626         if (!nlistread)
627                 failure = donlist();
628         if (failure)
629                 return (0.0);
630
631 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
632
633         /* XXX - I don't like this */
634         if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
635                 return (0.0);
636         if (rawcpu)
637                 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
638         return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
639                 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
640 }
641
642 void
643 pcpu(KINFO *k, VARENT *ve)
644 {
645         VAR *v;
646
647         v = ve->var;
648         (void)printf("%*.1f", v->width, getpcpu(k));
649 }
650
651 static double
652 getpmem(KINFO *k)
653 {
654         static int failure;
655         double fracmem;
656
657         if (!nlistread)
658                 failure = donlist();
659         if (failure)
660                 return (0.0);
661
662         if ((k->ki_p->ki_flag & P_INMEM) == 0)
663                 return (0.0);
664         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
665         /* XXX don't have info about shared */
666         fracmem = ((float)k->ki_p->ki_rssize) / mempages;
667         return (100.0 * fracmem);
668 }
669
670 void
671 pmem(KINFO *k, VARENT *ve)
672 {
673         VAR *v;
674
675         v = ve->var;
676         (void)printf("%*.1f", v->width, getpmem(k));
677 }
678
679 void
680 pagein(KINFO *k, VARENT *ve)
681 {
682         VAR *v;
683
684         v = ve->var;
685         (void)printf("%*ld", v->width,
686             k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
687 }
688
689 /* ARGSUSED */
690 void
691 maxrss(KINFO *k __unused, VARENT *ve)
692 {
693         VAR *v;
694
695         v = ve->var;
696         /* XXX not yet */
697         (void)printf("%*s", v->width, "-");
698 }
699
700 void
701 priorityr(KINFO *k, VARENT *ve)
702 {
703         VAR *v;
704         struct priority *lpri;
705         char str[8];
706         unsigned class, level;
707
708         v = ve->var;
709         lpri = &k->ki_p->ki_pri;
710         class = lpri->pri_class;
711         level = lpri->pri_level;
712         switch (class) {
713         case PRI_ITHD:
714                 snprintf(str, sizeof(str), "intr:%u", level);
715                 break;
716         case PRI_REALTIME:
717                 snprintf(str, sizeof(str), "real:%u", level);
718                 break;
719         case PRI_TIMESHARE:
720                 strncpy(str, "normal", sizeof(str));
721                 break;
722         case PRI_IDLE:
723                 snprintf(str, sizeof(str), "idle:%u", level);
724                 break;
725         default:
726                 snprintf(str, sizeof(str), "%u:%u", class, level);
727                 break;
728         }
729         str[sizeof(str) - 1] = '\0';
730         (void)printf("%*s", v->width, str);
731 }
732
733 /*
734  * Generic output routines.  Print fields from various prototype
735  * structures.
736  */
737 static void
738 printval(void *bp, VAR *v)
739 {
740         static char ofmt[32] = "%";
741         const char *fcp;
742         char *cp;
743
744         cp = ofmt + 1;
745         fcp = v->fmt;
746         if (v->flag & LJUST)
747                 *cp++ = '-';
748         *cp++ = '*';
749         while ((*cp++ = *fcp++));
750
751 #define CHKINF127(n)    (((n) > 127) && (v->flag & INF127) ? 127 : (n))
752
753         switch (v->type) {
754         case CHAR:
755                 (void)printf(ofmt, v->width, *(char *)bp);
756                 break;
757         case UCHAR:
758                 (void)printf(ofmt, v->width, *(u_char *)bp);
759                 break;
760         case SHORT:
761                 (void)printf(ofmt, v->width, *(short *)bp);
762                 break;
763         case USHORT:
764                 (void)printf(ofmt, v->width, *(u_short *)bp);
765                 break;
766         case INT:
767                 (void)printf(ofmt, v->width, *(int *)bp);
768                 break;
769         case UINT:
770                 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
771                 break;
772         case LONG:
773                 (void)printf(ofmt, v->width, *(long *)bp);
774                 break;
775         case ULONG:
776                 (void)printf(ofmt, v->width, *(u_long *)bp);
777                 break;
778         case KPTR:
779                 (void)printf(ofmt, v->width, *(u_long *)bp);
780                 break;
781         case PGTOK:
782                 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
783                 break;
784         default:
785                 errx(1, "unknown type %d", v->type);
786         }
787 }
788
789 void
790 kvar(KINFO *k, VARENT *ve)
791 {
792         VAR *v;
793
794         v = ve->var;
795         printval((char *)((char *)k->ki_p + v->off), v);
796 }
797
798 void
799 rvar(KINFO *k, VARENT *ve)
800 {
801         VAR *v;
802
803         v = ve->var;
804         if (k->ki_valid)
805                 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
806         else
807                 (void)printf("%*s", v->width, "-");
808 }
809
810 void
811 emulname(KINFO *k, VARENT *ve)
812 {
813         VAR *v;
814
815         v = ve->var;
816         printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
817 }
818
819 void
820 label(KINFO *k, VARENT *ve)
821 {
822         char *string;
823         VAR *v;
824         mac_t proclabel;
825         int error;
826
827         v = ve->var;
828         string = NULL;
829         if (mac_prepare_process_label(&proclabel) == -1) {
830                 warn("mac_prepare_process_label");
831                 goto out;
832         }
833         error = mac_get_pid(k->ki_p->ki_pid, proclabel);
834         if (error == 0) {
835                 if (mac_to_text(proclabel, &string) == -1)
836                         string = NULL;
837         }
838         mac_free(proclabel);
839 out:
840         if (string != NULL) {
841                 (void)printf("%-*s", v->width, string);
842                 free(string);
843         } else
844                 (void)printf("%-*s", v->width, "  -");
845         return;
846 }
847
848 int
849 s_comm(KINFO *k)
850 {
851         char tmpbuff[COMMLEN + OCOMMLEN + 2];
852
853         bzero(tmpbuff, sizeof(tmpbuff));
854         if (showthreads && k->ki_p->ki_numthreads > 1)
855                 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
856                     k->ki_p->ki_ocomm);
857         else
858                 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
859         return (strlen(tmpbuff));
860 }
861
862 int
863 s_label(KINFO *k)
864 {
865         char *string = NULL;
866         mac_t proclabel;
867         int error, size = 0;
868
869         if (mac_prepare_process_label(&proclabel) == -1) {
870                 warn("mac_prepare_process_label");
871                 return (0);
872         }
873         error = mac_get_pid(k->ki_p->ki_pid, proclabel);
874         if (error == 0 && mac_to_text(proclabel, &string) == 0) {
875                 size = strlen(string);
876                 free(string);
877         }
878         mac_free(proclabel);
879         return (size);
880 }