1174199Srwatson/*-
2174199Srwatson * Copyright (c) 2007 Robert N. M. Watson
3174199Srwatson * All rights reserved.
4174199Srwatson *
5174199Srwatson * Redistribution and use in source and binary forms, with or without
6174199Srwatson * modification, are permitted provided that the following conditions
7174199Srwatson * are met:
8174199Srwatson * 1. Redistributions of source code must retain the above copyright
9174199Srwatson *    notice, this list of conditions and the following disclaimer.
10174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11174199Srwatson *    notice, this list of conditions and the following disclaimer in the
12174199Srwatson *    documentation and/or other materials provided with the distribution.
13174199Srwatson *
14174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174199Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174199Srwatson * SUCH DAMAGE.
25174199Srwatson *
26174199Srwatson * $FreeBSD$
27174199Srwatson */
28174199Srwatson
29186567Srwatson#include <sys/param.h>
30174199Srwatson#include <sys/sysctl.h>
31174530Srwatson#include <sys/user.h>
32174199Srwatson
33174199Srwatson#include <err.h>
34174199Srwatson#include <errno.h>
35221807Sstas#include <libprocstat.h>
36174199Srwatson#include <limits.h>
37174199Srwatson#include <stdio.h>
38200462Sdelphij#include <stdlib.h>
39174199Srwatson#include <string.h>
40174199Srwatson
41174199Srwatson#include "procstat.h"
42174199Srwatson
43227838Strocinystatic void
44249680Strocinydo_args(struct procstat *procstat, struct kinfo_proc *kipp, int env)
45174199Srwatson{
46249680Strociny	int i;
47249680Strociny	char **args;
48174199Srwatson
49249680Strociny	if (!hflag) {
50227838Strociny		printf("%5s %-16s %-53s\n", "PID", "COMM",
51227838Strociny		    env ? "ENVIRONMENT" : "ARGS");
52249680Strociny	}
53174199Srwatson
54249680Strociny	args = env ? procstat_getenvv(procstat, kipp, 0) :
55249680Strociny	    procstat_getargv(procstat, kipp, 0);
56249680Strociny
57249680Strociny	printf("%5d %-16s", kipp->ki_pid, kipp->ki_comm);
58249680Strociny
59249680Strociny	if (args == NULL) {
60249680Strociny		printf(" -\n");
61174199Srwatson		return;
62174199Srwatson	}
63174199Srwatson
64249680Strociny	for (i = 0; args[i] != NULL; i++)
65249680Strociny		printf(" %s", args[i]);
66174199Srwatson	printf("\n");
67174199Srwatson}
68227838Strociny
69227838Strocinyvoid
70249680Strocinyprocstat_args(struct procstat *procstat, struct kinfo_proc *kipp)
71227838Strociny{
72249680Strociny	do_args(procstat, kipp, 0);
73227838Strociny}
74227838Strociny
75227838Strocinyvoid
76249680Strocinyprocstat_env(struct procstat *procstat, struct kinfo_proc *kipp)
77227838Strociny{
78249680Strociny	do_args(procstat, kipp, 1);
79227838Strociny}
80