]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - bin/ps/print.c
MFC r309400:
[FreeBSD/stable/8.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 static void
552 printtime(KINFO *k, VARENT *ve, long secs, long psecs)
553 /* psecs is "parts" of a second. first micro, then centi */
554 {
555         VAR *v;
556         char obuff[128];
557         static char decimal_point;
558
559         if (decimal_point == '\0')
560                 decimal_point = localeconv()->decimal_point[0];
561         v = ve->var;
562         if (!k->ki_valid) {
563                 secs = 0;
564                 psecs = 0;
565         } else {
566                 /* round and scale to 100's */
567                 psecs = (psecs + 5000) / 10000;
568                 secs += psecs / 100;
569                 psecs = psecs % 100;
570         }
571         (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
572             secs / 60, secs % 60, decimal_point, psecs);
573         (void)printf("%*s", v->width, obuff);
574 }
575
576 void
577 cputime(KINFO *k, VARENT *ve)
578 {
579         long secs, psecs;
580
581         /*
582          * This counts time spent handling interrupts.  We could
583          * fix this, but it is not 100% trivial (and interrupt
584          * time fractions only work on the sparc anyway).       XXX
585          */
586         secs = k->ki_p->ki_runtime / 1000000;
587         psecs = k->ki_p->ki_runtime % 1000000;
588         if (sumrusage) {
589                 secs += k->ki_p->ki_childtime.tv_sec;
590                 psecs += k->ki_p->ki_childtime.tv_usec;
591         }
592         printtime(k, ve, secs, psecs);
593 }
594
595 void
596 systime(KINFO *k, VARENT *ve)
597 {
598         long secs, psecs;
599
600         secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
601         psecs = k->ki_p->ki_rusage.ru_stime.tv_usec;
602         if (sumrusage) {
603                 secs += k->ki_p->ki_childstime.tv_sec;
604                 psecs += k->ki_p->ki_childstime.tv_usec;
605         }
606         printtime(k, ve, secs, psecs);
607 }
608
609 void
610 usertime(KINFO *k, VARENT *ve)
611 {
612         long secs, psecs;
613
614         secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
615         psecs = k->ki_p->ki_rusage.ru_utime.tv_usec;
616         if (sumrusage) {
617                 secs += k->ki_p->ki_childutime.tv_sec;
618                 psecs += k->ki_p->ki_childutime.tv_usec;
619         }
620         printtime(k, ve, secs, psecs);
621 }
622
623 void
624 elapsed(KINFO *k, VARENT *ve)
625 {
626         VAR *v;
627         time_t val;
628         int days, hours, mins, secs;
629         char obuff[128];
630
631         v = ve->var;
632         if (!k->ki_valid) {
633                 (void)printf("%-*s", v->width, "-");
634                 return;
635         }
636         val = now - k->ki_p->ki_start.tv_sec;
637         days = val / (24 * 60 * 60);
638         val %= 24 * 60 * 60;
639         hours = val / (60 * 60);
640         val %= 60 * 60;
641         mins = val / 60;
642         secs = val % 60;
643         if (days != 0)
644                 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
645                     days, hours, mins, secs);
646         else if (hours != 0)
647                 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
648                     hours, mins, secs);
649         else
650                 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
651         (void)printf("%*s", v->width, obuff);
652 }
653
654 double
655 getpcpu(const KINFO *k)
656 {
657         static int failure;
658
659         if (!nlistread)
660                 failure = donlist();
661         if (failure)
662                 return (0.0);
663
664 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
665
666         /* XXX - I don't like this */
667         if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
668                 return (0.0);
669         if (rawcpu)
670                 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
671         return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
672                 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
673 }
674
675 void
676 pcpu(KINFO *k, VARENT *ve)
677 {
678         VAR *v;
679
680         v = ve->var;
681         (void)printf("%*.1f", v->width, getpcpu(k));
682 }
683
684 static double
685 getpmem(KINFO *k)
686 {
687         static int failure;
688         double fracmem;
689
690         if (!nlistread)
691                 failure = donlist();
692         if (failure)
693                 return (0.0);
694
695         if ((k->ki_p->ki_flag & P_INMEM) == 0)
696                 return (0.0);
697         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
698         /* XXX don't have info about shared */
699         fracmem = ((float)k->ki_p->ki_rssize) / mempages;
700         return (100.0 * fracmem);
701 }
702
703 void
704 pmem(KINFO *k, VARENT *ve)
705 {
706         VAR *v;
707
708         v = ve->var;
709         (void)printf("%*.1f", v->width, getpmem(k));
710 }
711
712 void
713 pagein(KINFO *k, VARENT *ve)
714 {
715         VAR *v;
716
717         v = ve->var;
718         (void)printf("%*ld", v->width,
719             k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
720 }
721
722 /* ARGSUSED */
723 void
724 maxrss(KINFO *k __unused, VARENT *ve)
725 {
726         VAR *v;
727
728         v = ve->var;
729         /* XXX not yet */
730         (void)printf("%*s", v->width, "-");
731 }
732
733 void
734 priorityr(KINFO *k, VARENT *ve)
735 {
736         VAR *v;
737         struct priority *lpri;
738         char str[8];
739         unsigned class, level;
740
741         v = ve->var;
742         lpri = &k->ki_p->ki_pri;
743         class = lpri->pri_class;
744         level = lpri->pri_level;
745         switch (class) {
746         case PRI_ITHD:
747                 snprintf(str, sizeof(str), "intr:%u", level);
748                 break;
749         case PRI_REALTIME:
750                 snprintf(str, sizeof(str), "real:%u", level);
751                 break;
752         case PRI_TIMESHARE:
753                 strncpy(str, "normal", sizeof(str));
754                 break;
755         case PRI_IDLE:
756                 snprintf(str, sizeof(str), "idle:%u", level);
757                 break;
758         default:
759                 snprintf(str, sizeof(str), "%u:%u", class, level);
760                 break;
761         }
762         str[sizeof(str) - 1] = '\0';
763         (void)printf("%*s", v->width, str);
764 }
765
766 /*
767  * Generic output routines.  Print fields from various prototype
768  * structures.
769  */
770 static void
771 printval(void *bp, VAR *v)
772 {
773         static char ofmt[32] = "%";
774         const char *fcp;
775         char *cp;
776
777         cp = ofmt + 1;
778         fcp = v->fmt;
779         if (v->flag & LJUST)
780                 *cp++ = '-';
781         *cp++ = '*';
782         while ((*cp++ = *fcp++));
783
784 #define CHKINF127(n)    (((n) > 127) && (v->flag & INF127) ? 127 : (n))
785
786         switch (v->type) {
787         case CHAR:
788                 (void)printf(ofmt, v->width, *(char *)bp);
789                 break;
790         case UCHAR:
791                 (void)printf(ofmt, v->width, *(u_char *)bp);
792                 break;
793         case SHORT:
794                 (void)printf(ofmt, v->width, *(short *)bp);
795                 break;
796         case USHORT:
797                 (void)printf(ofmt, v->width, *(u_short *)bp);
798                 break;
799         case INT:
800                 (void)printf(ofmt, v->width, *(int *)bp);
801                 break;
802         case UINT:
803                 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
804                 break;
805         case LONG:
806                 (void)printf(ofmt, v->width, *(long *)bp);
807                 break;
808         case ULONG:
809                 (void)printf(ofmt, v->width, *(u_long *)bp);
810                 break;
811         case KPTR:
812                 (void)printf(ofmt, v->width, *(u_long *)bp);
813                 break;
814         case PGTOK:
815                 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
816                 break;
817         default:
818                 errx(1, "unknown type %d", v->type);
819         }
820 }
821
822 void
823 kvar(KINFO *k, VARENT *ve)
824 {
825         VAR *v;
826
827         v = ve->var;
828         printval((char *)((char *)k->ki_p + v->off), v);
829 }
830
831 void
832 rvar(KINFO *k, VARENT *ve)
833 {
834         VAR *v;
835
836         v = ve->var;
837         if (k->ki_valid)
838                 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
839         else
840                 (void)printf("%*s", v->width, "-");
841 }
842
843 void
844 emulname(KINFO *k, VARENT *ve)
845 {
846         VAR *v;
847
848         v = ve->var;
849         printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
850 }
851
852 void
853 label(KINFO *k, VARENT *ve)
854 {
855         char *string;
856         VAR *v;
857         mac_t proclabel;
858         int error;
859
860         v = ve->var;
861         string = NULL;
862         if (mac_prepare_process_label(&proclabel) == -1) {
863                 warn("mac_prepare_process_label");
864                 goto out;
865         }
866         error = mac_get_pid(k->ki_p->ki_pid, proclabel);
867         if (error == 0) {
868                 if (mac_to_text(proclabel, &string) == -1)
869                         string = NULL;
870         }
871         mac_free(proclabel);
872 out:
873         if (string != NULL) {
874                 (void)printf("%-*s", v->width, string);
875                 free(string);
876         } else
877                 (void)printf("%-*s", v->width, "  -");
878         return;
879 }
880
881 int
882 s_comm(KINFO *k)
883 {
884         char tmpbuff[COMMLEN + OCOMMLEN + 2];
885
886         bzero(tmpbuff, sizeof(tmpbuff));
887         if (showthreads && k->ki_p->ki_numthreads > 1)
888                 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
889                     k->ki_p->ki_ocomm);
890         else
891                 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
892         return (strlen(tmpbuff));
893 }
894
895 int
896 s_label(KINFO *k)
897 {
898         char *string = NULL;
899         mac_t proclabel;
900         int error, size = 0;
901
902         if (mac_prepare_process_label(&proclabel) == -1) {
903                 warn("mac_prepare_process_label");
904                 return (0);
905         }
906         error = mac_get_pid(k->ki_p->ki_pid, proclabel);
907         if (error == 0 && mac_to_text(proclabel, &string) == 0) {
908                 size = strlen(string);
909                 free(string);
910         }
911         mac_free(proclabel);
912         return (size);
913 }