Import NetBSD w(1) and uptime(1)

Change-Id: Id6cc36f4befbce4be3a471ae920d75972a44bef1
This commit is contained in:
David van Moolenbroek
2014-08-26 16:03:05 +00:00
parent 61df9b64d1
commit 11eaad3501
13 changed files with 1611 additions and 1 deletions

98
bin/ps/extern.h Normal file
View File

@@ -0,0 +1,98 @@
/* $NetBSD: extern.h,v 1.33 2010/05/31 03:18:33 rmind Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
* 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.
*
* @(#)extern.h 8.3 (Berkeley) 4/2/94
*/
/*
* We expect to be included by ps.h, which will already have
* defined the types we use.
*/
extern double ccpu;
extern int eval, fscale, mempages, nlistread, rawcpu, maxslp, uspace;
extern int sumrusage, termwidth, totwidth;
extern int needenv, needcomm, commandonly;
extern uid_t myuid;
extern kvm_t *kd;
extern VAR var[];
extern VARLIST displaylist;
extern VARLIST sortlist;
void command(void *, VARENT *, int);
void cpuid(void *, VARENT *, int);
void cputime(void *, VARENT *, int);
int donlist(void);
int donlist_sysctl(void);
void fmt_puts(char *, int *);
void fmt_putc(int, int *);
void elapsed(void *, VARENT *, int);
double getpcpu(const struct kinfo_proc2 *);
double getpmem(const struct kinfo_proc2 *);
void gname(void *, VARENT *, int);
void groups(void *, VARENT *, int);
void groupnames(void *, VARENT *, int);
void logname(void *, VARENT *, int);
void longtname(void *, VARENT *, int);
void lname(void *, VARENT *, int);
void lstarted(void *, VARENT *, int);
void lstate(void *, VARENT *, int);
void maxrss(void *, VARENT *, int);
void nlisterr(struct nlist *);
void p_rssize(void *, VARENT *, int);
void pagein(void *, VARENT *, int);
void parsefmt(const char *);
void parsefmt_insert(const char *, VARENT **);
void parsesort(const char *);
VARENT * varlist_find(VARLIST *, const char *);
void emul(void *, VARENT *, int);
void pcpu(void *, VARENT *, int);
void pmem(void *, VARENT *, int);
void pnice(void *, VARENT *, int);
void pri(void *, VARENT *, int);
void printheader(void);
void putimeval(void *, VARENT *, int);
void pvar(void *, VARENT *, int);
void rgname(void *, VARENT *, int);
void rssize(void *, VARENT *, int);
void runame(void *, VARENT *, int);
void showkey(void);
void started(void *, VARENT *, int);
void state(void *, VARENT *, int);
void svgname(void *, VARENT *, int);
void svuname(void *, VARENT *, int);
void tdev(void *, VARENT *, int);
void tname(void *, VARENT *, int);
void tsize(void *, VARENT *, int);
void ucomm(void *, VARENT *, int);
void uname(void *, VARENT *, int);
void uvar(void *, VARENT *, int);
void vsize(void *, VARENT *, int);
void wchan(void *, VARENT *, int);

60
bin/ps/fmt.c Normal file
View File

@@ -0,0 +1,60 @@
/* $NetBSD: fmt.c,v 1.21 2007/12/12 22:55:43 lukem Exp $ */
#include <sys/cdefs.h>
__RCSID("$NetBSD: fmt.c,v 1.21 2007/12/12 22:55:43 lukem Exp $");
#include <kvm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "ps.h"
void
fmt_puts(char *s, int *leftp)
{
static char *v = 0;
static int maxlen = 0;
char *nv;
int len, nlen;
if (*leftp == 0)
return;
len = strlen(s) * 4 + 1;
if (len > maxlen) {
if (maxlen == 0)
nlen = getpagesize();
else
nlen = maxlen;
while (len > nlen)
nlen *= 2;
nv = realloc(v, nlen);
if (nv == 0)
return;
v = nv;
maxlen = nlen;
}
len = strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
if (*leftp != -1) {
if (len > *leftp) {
v[*leftp] = '\0';
*leftp = 0;
} else
*leftp -= len;
}
(void)printf("%s", v);
}
void
fmt_putc(int c, int *leftp)
{
if (*leftp == 0)
return;
if (*leftp != -1)
*leftp -= 1;
putchar(c);
}

94
bin/ps/ps.h Normal file
View File

@@ -0,0 +1,94 @@
/* $NetBSD: ps.h,v 1.26 2006/10/02 17:54:35 apb Exp $ */
/*-
* Copyright (c) 1990, 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.
*
* @(#)ps.h 8.1 (Berkeley) 5/31/93
*/
#include <sys/queue.h>
#define UNLIMITED 0 /* unlimited terminal width */
#define PRINTMODE 0 /* print values */
#define WIDTHMODE 1 /* determine width of column */
enum type {
UNSPECIFIED,
CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, ULONG,
KPTR, KPTR24, INT32, UINT32, SIGLIST, INT64, UINT64,
TIMEVAL, CPUTIME, PCPU, VSIZE
};
/* Variables. */
typedef SIMPLEQ_HEAD(varlist, varent) VARLIST;
typedef struct varent {
SIMPLEQ_ENTRY(varent) next;
struct var *var;
} VARENT;
typedef struct var {
const char *name; /* name(s) of variable */
const char *header; /* header, possibly changed from default */
#define COMM 0x01 /* needs exec arguments and environment (XXX) */
#define ARGV0 0x02 /* only print argv[0] */
#define LJUST 0x04 /* left adjust on output (trailing blanks) */
#define INF127 0x08 /* 127 = infinity: if > 127, print 127. */
#define LWP 0x10 /* dispatch to kinfo_lwp routine */
#define UAREA 0x20 /* need to check p_uvalid */
#define ALIAS 0x40 /* entry is alias for 'header' */
u_int flag;
/* output routine */
void (*oproc)(void *, struct varent *, int);
/*
* The following (optional) elements are hooks for passing information
* to the generic output routine: pvar (that which prints simple
* elements from struct kinfo_proc2).
*/
int off; /* offset in structure */
enum type type; /* type of element */
const char *fmt; /* printf format */
/* current longest element */
int width; /* printing width */
int64_t longestp; /* longest positive signed value */
int64_t longestn; /* longest negative signed value */
u_int64_t longestu; /* longest unsigned value */
double longestpd; /* longest positive double */
double longestnd; /* longest negative double */
} VAR;
#define OUTPUT(vent, ki, kl, mode) do { \
if ((vent)->var->flag & LWP) \
((vent)->var->oproc)((void *)(kl), (vent), (mode)); \
else \
((vent)->var->oproc)((void *)(ki), (vent), (mode)); \
} while (/*CONSTCOND*/ 0)
#include "extern.h"