summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorBrian Feldman <green@FreeBSD.org>1999-10-30 22:15:31 +0000
committerBrian Feldman <green@FreeBSD.org>1999-10-30 22:15:31 +0000
commit693dd96d8ddc0565281233480ecc985dc9317a38 (patch)
treef5180548ebced32c4eb9279f4a42334725dfa38c /usr.bin
parentdd10325736fa591194d58cfa92a89f439a680def (diff)
Notes
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/w/w.16
-rw-r--r--usr.bin/w/w.c130
2 files changed, 70 insertions, 66 deletions
diff --git a/usr.bin/w/w.1 b/usr.bin/w/w.1
index 6dae0be8115d..12f3da808c13 100644
--- a/usr.bin/w/w.1
+++ b/usr.bin/w/w.1
@@ -43,7 +43,7 @@
.Op Fl dhin
.Op Fl M Ar core
.Op Fl N Ar system
-.Op Ar user
+.Op Ar user ...
.Sh DESCRIPTION
The
.Nm
@@ -83,9 +83,9 @@ Show network addresses as numbers (normally
interprets addresses and attempts to display them symbolically).
.El
.Pp
-If a
+If one or more
.Ar user
-name is specified, the output is restricted to that user.
+names are specified, the output is restricted to those users.
.Sh FILES
.Bl -tag -width /var/run/utmp -compact
.It Pa /var/run/utmp
diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c
index 762dfb1e71c0..6441ee8f8966 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -98,7 +98,7 @@ int header = 1; /* true if -h flag: don't print heading */
int nflag; /* true if -n flag: don't convert addrs */
int dflag; /* true if -d flag: output debug info */
int sortidle; /* sort by idle time */
-char *sel_user; /* login of particular user selected */
+char **sel_users; /* login array of particular users selected */
char domain[MAXHOSTNAMELEN];
/*
@@ -114,11 +114,12 @@ struct entry {
struct kinfo_proc *dkp; /* debug option proc list */
} *ep, *ehead = NULL, **nextp = &ehead;
-static void pr_header __P((time_t *, int));
-static struct stat
- *ttystat __P((char *));
-static void usage __P((int));
-static int this_is_uptime __P((const char *s));
+#define debugproc(p) *((struct kinfo_proc **)&(p)->kp_eproc.e_spare[0])
+
+static void pr_header __P((time_t *, int));
+static struct stat *ttystat __P((char *, int));
+static void usage __P((int));
+static int this_is_uptime __P((const char *s));
char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */
@@ -127,19 +128,17 @@ main(argc, argv)
int argc;
char **argv;
{
- extern char *__progname;
struct kinfo_proc *kp;
struct kinfo_proc *dkp;
struct hostent *hp;
struct stat *stp;
FILE *ut;
u_long l;
- size_t arglen;
int ch, i, nentries, nusers, wcmd, longidle;
char *memf, *nlistf, *p, *x;
char buf[MAXHOSTNAMELEN], errbuf[256];
- (void) setlocale(LC_ALL, "");
+ (void)setlocale(LC_ALL, "");
/* Are we w(1) or uptime(1)? */
if (this_is_uptime(argv[0]) == 0) {
@@ -202,22 +201,32 @@ main(argc, argv)
err(1, "%s", _PATH_UTMP);
if (*argv)
- sel_user = *argv;
+ sel_users = argv;
for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
if (utmp.ut_name[0] == '\0')
continue;
- if (!(stp = ttystat(utmp.ut_line))) /* corrupted record */
- continue;
+ if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
+ continue; /* corrupted record */
++nusers;
- if (wcmd == 0 || (sel_user &&
- strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
+ if (wcmd == 0)
continue;
+ if (sel_users) {
+ int usermatch;
+ char **user;
+
+ usermatch = 0;
+ for (user = sel_users; !usermatch && *user; user++)
+ if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
+ usermatch = 1;
+ if (!usermatch)
+ continue;
+ }
if ((ep = calloc(1, sizeof(struct entry))) == NULL)
errx(1, "calloc");
*nextp = ep;
- nextp = &(ep->next);
- memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
+ nextp = &ep->next;
+ memmove(&ep->utmp, &utmp, sizeof(struct utmp));
ep->tdev = stp->st_rdev;
#ifdef CPU_CONSDEV
/*
@@ -231,7 +240,7 @@ main(argc, argv)
mib[0] = CTL_MACHDEP;
mib[1] = CPU_CONSDEV;
size = sizeof(dev_t);
- (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
+ (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
}
#endif
if ((ep->idle = now - stp->st_atime) < 0)
@@ -242,7 +251,7 @@ main(argc, argv)
if (header || wcmd == 0) {
pr_header(&now, nusers);
if (wcmd == 0)
- exit (0);
+ exit(0);
#define HEADER_USER "USER"
#define HEADER_TTY "TTY"
@@ -261,10 +270,10 @@ main(argc, argv)
if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
err(1, "%s", kvm_geterr(kd));
for (i = 0; i < nentries; i++, kp++) {
- struct proc *p = &kp->kp_proc;
+ struct proc *pr = &kp->kp_proc;
struct eproc *e;
- if (p->p_stat == SIDL || p->p_stat == SZOMB)
+ if (pr->p_stat == SIDL || pr->p_stat == SZOMB)
continue;
e = &kp->kp_eproc;
for (ep = ehead; ep != NULL; ep = ep->next) {
@@ -276,7 +285,7 @@ main(argc, argv)
/*
* Proc is 'most interesting'
*/
- if (proc_compare(&ep->kp->kp_proc, p))
+ if (proc_compare(&ep->kp->kp_proc, pr))
ep->kp = kp;
}
/*
@@ -287,7 +296,7 @@ main(argc, argv)
*/
dkp = ep->dkp;
ep->dkp = kp;
- *((struct kinfo_proc **)(&kp->kp_eproc.e_spare[0])) = dkp;
+ debugproc(kp) = dkp;
}
}
}
@@ -312,8 +321,9 @@ main(argc, argv)
}
/* sort by idle time */
if (sortidle && ehead != NULL) {
- struct entry *from = ehead, *save;
+ struct entry *from, *save;
+ from = ehead;
ehead = NULL;
while (from != NULL) {
for (nextp = &ehead;
@@ -327,21 +337,25 @@ main(argc, argv)
}
}
- if (!nflag)
+ if (!nflag) {
if (gethostname(domain, sizeof(domain) - 1) < 0 ||
- (p = strchr(domain, '.')) == 0)
+ (p = strchr(domain, '.')) == NULL)
domain[0] = '\0';
else {
domain[sizeof(domain) - 1] = '\0';
memmove(domain, p, strlen(p) + 1);
}
+ }
for (ep = ehead; ep != NULL; ep = ep->next) {
- p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
+ char host_buf[UT_HOSTSIZE + 1];
+
+ host_buf[UT_HOSTSIZE] = '\0';
+ strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
+ p = *host_buf ? host_buf : "-";
if ((x = strchr(p, ':')) != NULL)
*x++ = '\0';
- if (!nflag && isdigit(*p) &&
- (long)(l = inet_addr(p)) != -1 &&
+ if (!nflag && isdigit(*p) && (l = inet_addr(p)) != -1 &&
(hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
if (domain[0] != '\0') {
p = hp->h_name;
@@ -364,18 +378,19 @@ main(argc, argv)
}
}
if (x) {
- (void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
- ep->utmp.ut_host + UT_HOSTSIZE - x, x);
+ (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
p = buf;
}
if (dflag) {
- for (dkp = ep->dkp; dkp != NULL; dkp = *((struct kinfo_proc **)(&dkp->kp_eproc.e_spare[0]))) {
- char *p;
- p = fmt_argv(kvm_getargv(kd, dkp, argwidth),
- dkp->kp_proc.p_comm, MAXCOMLEN);
- if (p == NULL)
- p = "-";
- (void)printf( "\t\t%-9d %s\n", dkp->kp_proc.p_pid, p);
+ for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
+ char *ptr;
+
+ ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
+ dkp->kp_proc.p_comm, MAXCOMLEN);
+ if (ptr == NULL)
+ ptr = "-";
+ (void)printf("\t\t%-9d %s\n",
+ dkp->kp_proc.p_pid, ptr);
}
}
(void)printf("%-*.*s %-*.*s %-*.*s ",
@@ -406,12 +421,8 @@ pr_header(nowp, nusers)
/*
* Print time of day.
- *
- * SCCS forces the string manipulation below, as it replaces
- * %, M, and % in a character string with the file name.
*/
- (void)strftime(buf, sizeof(buf) - 1,
- __CONCAT("%l:%","M%p"), localtime(nowp));
+ (void)strftime(buf, sizeof(buf) - 1, "%l:%M%p", localtime(nowp));
buf[sizeof(buf) - 1] = '\0';
(void)printf("%s ", buf);
@@ -438,14 +449,11 @@ pr_header(nowp, nusers)
if (hrs > 0 && mins > 0)
(void)printf(" %2d:%02d,", hrs, mins);
else if (hrs > 0)
- (void)printf(" %d hr%s,",
- hrs, hrs > 1 ? "s" : "");
+ (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
else if (mins > 0)
- (void)printf(" %d min%s,",
- mins, mins > 1 ? "s" : "");
+ (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
else
- (void)printf(" %d sec%s,",
- secs, secs > 1 ? "s" : "");
+ (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
}
/* Print number of users logged in to system */
@@ -458,26 +466,24 @@ pr_header(nowp, nusers)
(void)printf(", no load average information available\n");
else {
(void)printf(", load averages:");
- for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
- if (i > 0)
- (void)printf(",");
- (void)printf(" %.2f", avenrun[i]);
- }
+ for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++)
+ (void)printf("%s %.2f", i > 0 ? "," : "", avenrun[i]);
(void)printf("\n");
}
}
static struct stat *
-ttystat(line)
+ttystat(line, sz)
char *line;
+ int sz;
{
static struct stat sb;
char ttybuf[MAXPATHLEN];
- (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
+ (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
if (stat(ttybuf, &sb)) {
warn("%s", ttybuf);
- return NULL;
+ return (NULL);
}
return (&sb);
}
@@ -488,11 +494,10 @@ usage(wcmd)
{
if (wcmd)
(void)fprintf(stderr,
- "usage: w [-dhin] [-M core] [-N system] [user]\n");
+ "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
else
- (void)fprintf(stderr,
- "usage: uptime\n");
- exit (1);
+ (void)fprintf(stderr, "usage: uptime\n");
+ exit(1);
}
static int
@@ -506,7 +511,6 @@ this_is_uptime(s)
else
u = s;
if (strcmp(u, "uptime") == 0)
- return(0);
- return(-1);
+ return (0);
+ return (-1);
}
-