/*-
* Copyright (c) 1980, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
The Regents of the University of California. All rights reserved.");
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: main.c,v 1.58 2024/06/16 22:44:01 kre Exp $");
#endif /* not lint */
gid_t egid; /* XXX needed by initiostat() and initkre() */
int
main(int argc, char **argv)
{
int ch;
char errbuf[_POSIX2_LINE_MAX];
const char *all;
struct clockinfo clk;
size_t len;
int bflag = 0;
all = "all";
egid = getegid();
(void)setegid(getgid());
while ((ch = getopt(argc, argv, "M:N:bnw:t:z")) != -1)
switch(ch) {
case 'M':
memf = optarg;
break;
case 'N':
nlistf = optarg;
break;
case 'b':
bflag = !bflag;
break;
case 'n':
nflag = !nflag;
break;
case 't':
if ((turns = atoi(optarg)) <= 0)
errx(EXIT_FAILURE, "turns <= 0.");
break;
case 'w':
if ((naptime = strtod(optarg, NULL)) <= 0)
errx(EXIT_FAILURE, "interval <= 0.");
break;
case 'z':
showzero = true;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
for ( ; argc > 0; argc--, argv++) {
struct mode *p;
int modefound = 0;
if (isdigit((unsigned char)argv[0][0])) {
naptime = strtod(argv[0], NULL);
if (naptime <= 0)
naptime = 5;
continue;
}
for (p = modes; p->c_name ; p++) {
if (strstr(p->c_name, argv[0]) == p->c_name) {
curmode = p;
modefound++;
break;
}
if (strstr(all, argv[0]) == all) {
allcounter=0;
allflag=1;
}
}
if (!modefound && !allflag)
error("%s: Unknown command.", argv[0]);
}
/*
* Discard setgid privileges. If not the running kernel, we toss
* them away totally so that bad guys can't print interesting stuff
* from kernel memory, otherwise switch back to kmem for the
* duration of the kvm_openfiles() call.
*/
if (nlistf != NULL || memf != NULL)
(void)setgid(getgid());
else
(void)setegid(egid);
/*
* Initialize display. Load average appears in a one line
* window of its own. Current command's display appears in
* an overlapping sub-window of stdscr configured by the display
* routines to minimize update work by curses.
*/
if (initscr() == NULL)
errx(EXIT_FAILURE, "couldn't initialize screen");