Create and strvis(3) the argument and environment lists only as needed.

Do not keep extra copies around.
This commit is contained in:
mycroft 1995-05-18 20:33:20 +00:00
parent 5ea2d5e6a6
commit 3541700d61
6 changed files with 122 additions and 195 deletions

View File

@ -1,8 +1,8 @@
# $NetBSD: Makefile,v 1.13 1995/03/21 09:07:56 cgd Exp $
# $NetBSD: Makefile,v 1.14 1995/05/18 20:33:20 mycroft Exp $
# @(#)Makefile 8.1 (Berkeley) 6/2/93
PROG= ps
SRCS= fmt.c keyword.c nlist.c print.c ps.c
SRCS= keyword.c nlist.c print.c ps.c
DPADD= ${LIBMATH} ${LIBKVM}
LDADD= -lm -lkvm
BINGRP= kmem

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.8 1995/03/21 09:07:58 cgd Exp $ */
/* $NetBSD: extern.h,v 1.9 1995/05/18 20:33:22 mycroft Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@ -51,7 +51,6 @@ void command __P((KINFO *, VARENT *));
void cputime __P((KINFO *, VARENT *));
int donlist __P((void));
void evar __P((KINFO *, VARENT *));
char *fmt_argv __P((char **, char *, int));
double getpcpu __P((KINFO *));
double getpmem __P((KINFO *));
void logname __P((KINFO *, VARENT *));

View File

@ -1,117 +0,0 @@
/* $NetBSD: fmt.c,v 1.7 1995/05/18 15:35:59 mycroft Exp $ */
/*-
* Copyright (c) 1992, 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)fmt.c 8.4 (Berkeley) 4/15/94";
#else
static char rcsid[] = "$NetBSD: fmt.c,v 1.7 1995/05/18 15:35:59 mycroft Exp $";
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vis.h>
#include "ps.h"
static char *cmdpart __P((char *));
static char *shquote __P((char **));
/*
* XXX
* This is a stub until marc does the real one.
*/
static char *
shquote(argv)
char **argv;
{
char **p, *dst, *src;
static char buf[4096]; /* XXX */
if (*argv == 0) {
buf[0] = 0;
return (buf);
}
dst = buf;
for (p = argv; (src = *p++) != 0; ) {
if (*src == 0)
continue;
strvis(dst, src, VIS_TAB | VIS_NL | VIS_CSTYLE);
dst += strlen(dst);
*dst++ = ' ';
}
*dst = '\0';
return (buf);
}
static char *
cmdpart(arg0)
char *arg0;
{
char *cp;
return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
}
char *
fmt_argv(argv, cmd, maxlen)
char **argv;
char *cmd;
int maxlen;
{
int len;
char *ap, *cp;
if (argv == 0 || argv[0] == 0)
ap = "";
else
ap = shquote(argv);
len = strlen(ap);
if ((cp = malloc(len + maxlen + 3)) == NULL)
return (NULL);
if (cmd &&
(ap[0] == '\0' || strncmp(cmdpart(argv[0]), cmd, maxlen) != 0))
sprintf(cp, "%s(%.*s)", ap, maxlen, cmd);
else {
bcopy(ap, cp, len);
cp[len - 1] = '\0';
}
return (cp);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: print.c,v 1.20 1995/05/18 15:27:32 mycroft Exp $ */
/* $NetBSD: print.c,v 1.21 1995/05/18 20:33:24 mycroft Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
#else
static char rcsid[] = "$NetBSD: print.c,v 1.20 1995/05/18 15:27:32 mycroft Exp $";
static char rcsid[] = "$NetBSD: print.c,v 1.21 1995/05/18 20:33:24 mycroft Exp $";
#endif
#endif /* not lint */
@ -62,6 +62,7 @@ static char rcsid[] = "$NetBSD: print.c,v 1.20 1995/05/18 15:27:32 mycroft Exp $
#endif
#include <err.h>
#include <kvm.h>
#include <math.h>
#include <nlist.h>
#include <stddef.h>
@ -74,6 +75,22 @@ static char rcsid[] = "$NetBSD: print.c,v 1.20 1995/05/18 15:27:32 mycroft Exp $
#include "ps.h"
extern kvm_t *kd;
extern int needenv, needcomm, commandonly;
static char *cmdpart __P((char *));
static void fmt_puts __P((char *, int *));
static void fmt_putc __P((int, int *));
static char *
cmdpart(arg0)
char *arg0;
{
char *cp;
return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
}
void
printheader()
{
@ -95,38 +112,104 @@ printheader()
(void)putchar('\n');
}
static void
fmt_puts(s, leftp)
char *s;
int *leftp;
{
static char *v = 0, *nv;
static int maxlen = 0;
int len;
if (*leftp == 0)
return;
len = strlen(s) * 4 + 1;
if (len > maxlen) {
if (maxlen == 0)
maxlen = getpagesize();
while (len > maxlen)
maxlen *= 2;
nv = realloc(v, maxlen);
if (nv == 0)
return;
v = nv;
}
strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
if (*leftp != -1) {
len = strlen(v);
if (len > *leftp) {
printf("%.*s", *leftp, v);
*leftp = 0;
} else {
printf("%s", v);
*leftp -= len;
}
} else
printf("%s", v);
}
static void
fmt_putc(c, leftp)
int c;
int *leftp;
{
if (*leftp == 0)
return;
if (*leftp != -1)
*leftp -= 1;
putchar(c);
}
void
command(k, ve)
KINFO *k;
command(ki, ve)
KINFO *ki;
VARENT *ve;
{
VAR *v;
int left;
char *cp;
char **argv, **p;
v = ve->var;
if (ve->next == NULL) {
/* last field */
if (termwidth == UNLIMITED) {
if (k->ki_env)
(void)printf("%s ", k->ki_env);
(void)printf("%s", k->ki_args);
} else {
if (ve->next != NULL || termwidth != UNLIMITED) {
if (ve->next == NULL) {
left = termwidth - (totwidth - v->width);
if (left < 1) /* already wrapped, just use std width */
left = v->width;
if ((cp = k->ki_env) != NULL) {
while (--left >= 0 && *cp)
(void)putchar(*cp++);
if (--left >= 0)
putchar(' ');
}
for (cp = k->ki_args; --left >= 0 && *cp != '\0';)
(void)putchar(*cp++);
}
} else
left = v->width;
} else
/* XXX env? */
(void)printf("%-*.*s", v->width, v->width, k->ki_args);
left = -1;
if (needenv) {
argv = kvm_getenvv(kd, ki->ki_p, termwidth);
if (p = argv) {
while (*p) {
fmt_puts(*p, &left);
p++;
fmt_putc(' ', &left);
}
}
}
if (needcomm) {
if (!commandonly) {
argv = kvm_getargv(kd, ki->ki_p, termwidth);
if (p = argv) {
while (*p) {
fmt_puts(*p, &left);
p++;
fmt_putc(' ', &left);
}
}
if (argv == 0 || argv[0] == 0 ||
strcmp(cmdpart(argv[0]), KI_PROC(ki)->p_comm)) {
fmt_putc('(', &left);
fmt_puts(KI_PROC(ki)->p_comm, &left);
fmt_putc(')', &left);
}
} else {
fmt_puts(KI_PROC(ki)->p_comm, &left);
}
}
}
void

View File

@ -1,4 +1,4 @@
/* $NetBSD: ps.c,v 1.14 1995/05/18 14:37:03 mycroft Exp $ */
/* $NetBSD: ps.c,v 1.15 1995/05/18 20:33:25 mycroft Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -43,7 +43,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
#else
static char rcsid[] = "$NetBSD: ps.c,v 1.14 1995/05/18 14:37:03 mycroft Exp $";
static char rcsid[] = "$NetBSD: ps.c,v 1.15 1995/05/18 20:33:25 mycroft Exp $";
#endif
#endif /* not lint */
@ -83,12 +83,10 @@ int sumrusage; /* -S */
int termwidth; /* width of screen (0 == infinity) */
int totwidth; /* calculated width of requested variables */
static int needuser, needcomm, needenv, commandonly;
int needuser, needcomm, needenv, commandonly;
enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
static char *fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int),
KINFO *, char *, int));
static char *kludge_oldps_options __P((char *));
static int pscomp __P((const void *, const void *));
static void saveuser __P((KINFO *));
@ -116,7 +114,7 @@ main(argc, argv)
dev_t ttydev;
pid_t pid;
uid_t uid;
int all, ch, flag, i, ofmt, lineno, nentries;
int all, ch, flag, i, fmt, lineno, nentries;
int prtheader, wflag, what, xflg;
char *nlistf, *memf, *swapf, errbuf[256];
@ -131,7 +129,7 @@ main(argc, argv)
if (argc > 1)
argv[1] = kludge_oldps_options(argv[1]);
all = ofmt = prtheader = wflag = xflg = 0;
all = fmt = prtheader = wflag = xflg = 0;
pid = -1;
uid = (uid_t) -1;
ttydev = NODEV;
@ -158,7 +156,7 @@ main(argc, argv)
break;
case 'j':
parsefmt(jfmt);
ofmt = 1;
fmt = 1;
jfmt[0] = '\0';
break;
case 'L':
@ -166,7 +164,7 @@ main(argc, argv)
exit(0);
case 'l':
parsefmt(lfmt);
ofmt = 1;
fmt = 1;
lfmt[0] = '\0';
break;
case 'M':
@ -183,11 +181,11 @@ main(argc, argv)
parsefmt(optarg);
parsefmt(o2);
o1[0] = o2[0] = '\0';
ofmt = 1;
fmt = 1;
break;
case 'o':
parsefmt(optarg);
ofmt = 1;
fmt = 1;
break;
case 'p':
pid = atol(optarg);
@ -224,13 +222,13 @@ main(argc, argv)
case 'u':
parsefmt(ufmt);
sortby = SORTCPU;
ofmt = 1;
fmt = 1;
ufmt[0] = '\0';
break;
case 'v':
parsefmt(vfmt);
sortby = SORTMEM;
ofmt = 1;
fmt = 1;
vfmt[0] = '\0';
break;
case 'W':
@ -275,7 +273,7 @@ main(argc, argv)
if (kd == 0)
errx(1, "%s", errbuf);
if (!ofmt)
if (!fmt)
parsefmt(dfmt);
if (!all && ttydev == NODEV && pid == -1) /* XXX - should be cleaner */
@ -334,25 +332,6 @@ main(argc, argv)
(KI_PROC(ki)->p_flag & P_CONTROLT ) == 0))
continue;
for (vent = vhead; vent; vent = vent->next) {
/*
* get arguments if needed
*/
if (needcomm) {
if (commandonly)
ki->ki_args =
strdup(KI_PROC(ki)->p_comm);
else
ki->ki_args =
fmt(kvm_getargv, ki,
KI_PROC(ki)->p_comm, MAXCOMLEN);
} else
ki->ki_args = NULL;
if (needenv)
ki->ki_env =
fmt(kvm_getenvv, ki, (char *)NULL, 0);
else
ki->ki_env = NULL;
(vent->var->oproc)(ki, vent);
if (vent->next != NULL)
(void)putchar(' ');
@ -388,21 +367,6 @@ scanvars()
totwidth--;
}
static char *
fmt(fn, ki, comm, maxlen)
char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int));
KINFO *ki;
char *comm;
int maxlen;
{
char *s;
if ((s =
fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL)
err(1, NULL);
return (s);
}
static void
saveuser(ki)
KINFO *ki;

View File

@ -1,4 +1,4 @@
/* $NetBSD: ps.h,v 1.7 1995/05/08 22:39:43 cgd Exp $ */
/* $NetBSD: ps.h,v 1.8 1995/05/18 20:33:26 mycroft Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -52,8 +52,6 @@ struct usave {
typedef struct kinfo {
struct kinfo_proc *ki_p; /* proc structure */
struct usave ki_u; /* interesting parts of user */
char *ki_args; /* exec args */
char *ki_env; /* environment */
} KINFO;
/* Variables. */