Deleted Added
full compact
w.c (77367) w.c (80407)
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
43#endif
44static const char rcsid[] =
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/usr.bin/w/w.c 77367 2001-05-28 21:30:31Z phk $";
45 "$FreeBSD: head/usr.bin/w/w.c 80407 2001-07-26 19:20:13Z brian $";
46#endif /* not lint */
47
48/*
49 * w - print system status (who and what)
50 *
51 * This program is similar to the systat command on Tenex/Tops 10/20
52 *
53 */

--- 13 unchanged lines hidden (view full) ---

67#include <arpa/inet.h>
68
69#include <ctype.h>
70#include <err.h>
71#include <errno.h>
72#include <fcntl.h>
73#include <kvm.h>
74#include <langinfo.h>
46#endif /* not lint */
47
48/*
49 * w - print system status (who and what)
50 *
51 * This program is similar to the systat command on Tenex/Tops 10/20
52 *
53 */

--- 13 unchanged lines hidden (view full) ---

67#include <arpa/inet.h>
68
69#include <ctype.h>
70#include <err.h>
71#include <errno.h>
72#include <fcntl.h>
73#include <kvm.h>
74#include <langinfo.h>
75#include <libutil.h>
75#include <limits.h>
76#include <locale.h>
77#include <netdb.h>
78#include <nlist.h>
79#include <paths.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>

--- 16 unchanged lines hidden (view full) ---

99int argwidth; /* width of tty */
100int header = 1; /* true if -h flag: don't print heading */
101int nflag; /* true if -n flag: don't convert addrs */
102int dflag; /* true if -d flag: output debug info */
103int sortidle; /* sort by idle time */
104int use_ampm; /* use AM/PM time */
105int use_comma; /* use comma as floats separator */
106char **sel_users; /* login array of particular users selected */
76#include <limits.h>
77#include <locale.h>
78#include <netdb.h>
79#include <nlist.h>
80#include <paths.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>

--- 16 unchanged lines hidden (view full) ---

100int argwidth; /* width of tty */
101int header = 1; /* true if -h flag: don't print heading */
102int nflag; /* true if -n flag: don't convert addrs */
103int dflag; /* true if -d flag: output debug info */
104int sortidle; /* sort by idle time */
105int use_ampm; /* use AM/PM time */
106int use_comma; /* use comma as floats separator */
107char **sel_users; /* login array of particular users selected */
107char domain[MAXHOSTNAMELEN];
108
109/*
110 * One of these per active utmp entry.
111 */
112struct entry {
113 struct entry *next;
114 struct utmp utmp;
115 dev_t tdev; /* dev_t of terminal */
116 time_t idle; /* idle time of terminal in seconds */
117 struct kinfo_proc *kp; /* `most interesting' proc */
118 char *args; /* arg list of interesting process */
119 struct kinfo_proc *dkp; /* debug option proc list */
120} *ep, *ehead = NULL, **nextp = &ehead;
121
122#define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
123
108
109/*
110 * One of these per active utmp entry.
111 */
112struct entry {
113 struct entry *next;
114 struct utmp utmp;
115 dev_t tdev; /* dev_t of terminal */
116 time_t idle; /* idle time of terminal in seconds */
117 struct kinfo_proc *kp; /* `most interesting' proc */
118 char *args; /* arg list of interesting process */
119 struct kinfo_proc *dkp; /* debug option proc list */
120} *ep, *ehead = NULL, **nextp = &ehead;
121
122#define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
123
124/* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
125#define W_DISPHOSTSIZE 16
126
124static void pr_header __P((time_t *, int));
125static struct stat *ttystat __P((char *, int));
126static void usage __P((int));
127static int this_is_uptime __P((const char *s));
128
129char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */
130
131int

--- 4 unchanged lines hidden (view full) ---

136 struct kinfo_proc *kp;
137 struct kinfo_proc *dkp;
138 struct hostent *hp;
139 struct stat *stp;
140 FILE *ut;
141 u_long l;
142 time_t touched;
143 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
127static void pr_header __P((time_t *, int));
128static struct stat *ttystat __P((char *, int));
129static void usage __P((int));
130static int this_is_uptime __P((const char *s));
131
132char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */
133
134int

--- 4 unchanged lines hidden (view full) ---

139 struct kinfo_proc *kp;
140 struct kinfo_proc *dkp;
141 struct hostent *hp;
142 struct stat *stp;
143 FILE *ut;
144 u_long l;
145 time_t touched;
146 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
144 char *memf, *nlistf, *p, *x;
147 char *memf, *nlistf, *p, *x_suffix;
145 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
148 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
149 char fn[MAXHOSTNAMELEN];
150 char *dot;
146
147 (void)setlocale(LC_ALL, "");
148 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
149 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
150
151 /* Are we w(1) or uptime(1)? */
152 if (this_is_uptime(argv[0]) == 0) {
153 wcmd = 0;

--- 117 unchanged lines hidden (view full) ---

271 exit(0);
272 }
273
274#define HEADER_USER "USER"
275#define HEADER_TTY "TTY"
276#define HEADER_FROM "FROM"
277#define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
278#define HEADER_WHAT "WHAT\n"
151
152 (void)setlocale(LC_ALL, "");
153 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
154 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
155
156 /* Are we w(1) or uptime(1)? */
157 if (this_is_uptime(argv[0]) == 0) {
158 wcmd = 0;

--- 117 unchanged lines hidden (view full) ---

276 exit(0);
277 }
278
279#define HEADER_USER "USER"
280#define HEADER_TTY "TTY"
281#define HEADER_FROM "FROM"
282#define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
283#define HEADER_WHAT "WHAT\n"
279#define WUSED (UT_NAMESIZE + UT_LINESIZE + UT_HOSTSIZE + \
284#define WUSED (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
280 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
281 (void)printf("%-*.*s %-*.*s %-*.*s %s",
282 UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
283 UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
285 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
286 (void)printf("%-*.*s %-*.*s %-*.*s %s",
287 UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
288 UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
284 UT_HOSTSIZE, UT_HOSTSIZE, HEADER_FROM,
289 W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
285 HEADER_LOGIN_IDLE HEADER_WHAT);
286 }
287
288 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
289 err(1, "%s", kvm_geterr(kd));
290 for (i = 0; i < nentries; i++, kp++) {
291 if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB)
292 continue;

--- 53 unchanged lines hidden (view full) ---

346 continue;
347 save = from;
348 from = from->next;
349 save->next = *nextp;
350 *nextp = save;
351 }
352 }
353
290 HEADER_LOGIN_IDLE HEADER_WHAT);
291 }
292
293 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
294 err(1, "%s", kvm_geterr(kd));
295 for (i = 0; i < nentries; i++, kp++) {
296 if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB)
297 continue;

--- 53 unchanged lines hidden (view full) ---

351 continue;
352 save = from;
353 from = from->next;
354 save->next = *nextp;
355 *nextp = save;
356 }
357 }
358
354 if (!nflag) {
355 if (gethostname(domain, sizeof(domain) - 1) < 0 ||
356 (p = strchr(domain, '.')) == NULL)
357 domain[0] = '\0';
358 else {
359 domain[sizeof(domain) - 1] = '\0';
360 memmove(domain, p, strlen(p) + 1);
361 }
362 }
363
364 for (ep = ehead; ep != NULL; ep = ep->next) {
365 char host_buf[UT_HOSTSIZE + 1];
359 for (ep = ehead; ep != NULL; ep = ep->next) {
360 char host_buf[UT_HOSTSIZE + 1];
361 struct sockaddr_storage ss;
362 struct sockaddr *sa = (struct sockaddr *)&ss;
363 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
364 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
365 int isaddr;
366
367 host_buf[UT_HOSTSIZE] = '\0';
368 strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
369 p = *host_buf ? host_buf : "-";
366
367 host_buf[UT_HOSTSIZE] = '\0';
368 strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
369 p = *host_buf ? host_buf : "-";
370 if ((x = strchr(p, ':')) != NULL)
371 *x++ = '\0';
372 if (!nflag && isdigit(*p) && (l = inet_addr(p)) != -1 &&
373 (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
374 if (domain[0] != '\0') {
375 p = hp->h_name;
376 p += strlen(hp->h_name);
377 p -= strlen(domain);
378 if (p > hp->h_name &&
379 strcasecmp(p, domain) == 0)
380 *p = '\0';
381 }
382 p = hp->h_name;
370 if ((x_suffix = strrchr(p, ':')) != NULL) {
371 if ((dot = strchr(x_suffix, '.')) != NULL &&
372 strchr(dot+1, '.') == NULL)
373 *x_suffix++ = '\0';
374 else
375 x_suffix = NULL;
383 }
376 }
384 if (nflag && *p && strcmp(p, "-") &&
385 inet_addr(p) == INADDR_NONE) {
386 hp = gethostbyname(p);
387
388 if (hp != NULL) {
389 struct in_addr in;
390
391 memmove(&in, hp->h_addr, sizeof(in));
392 p = inet_ntoa(in);
377 if (!nflag) {
378 /* Attempt to change an IP address into a name */
379 isaddr = 0;
380 memset(&ss, '\0', sizeof(ss));
381 if (inet_pton(AF_INET6, p, &sin6->sin6_addr) == 1) {
382 sin6->sin6_len = sizeof(*sin6);
383 sin6->sin6_family = AF_INET6;
384 isaddr = 1;
385 } else if (inet_pton(AF_INET, p, &sin->sin_addr) == 1) {
386 sin->sin_len = sizeof(*sin);
387 sin->sin_family = AF_INET;
388 isaddr = 1;
393 }
389 }
390 if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
391 sa->sa_len) == HOSTNAME_FOUND)
392 p = fn;
394 }
393 }
395 if (x) {
396 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
394 if (x_suffix) {
395 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
397 p = buf;
398 }
399 if (dflag) {
400 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
401 char *ptr;
402
403 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
404 dkp->ki_comm, MAXCOMLEN);

--- 4 unchanged lines hidden (view full) ---

409 }
410 }
411 (void)printf("%-*.*s %-*.*s %-*.*s ",
412 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
413 UT_LINESIZE, UT_LINESIZE,
414 strncmp(ep->utmp.ut_line, "tty", 3) &&
415 strncmp(ep->utmp.ut_line, "cua", 3) ?
416 ep->utmp.ut_line : ep->utmp.ut_line + 3,
396 p = buf;
397 }
398 if (dflag) {
399 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
400 char *ptr;
401
402 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
403 dkp->ki_comm, MAXCOMLEN);

--- 4 unchanged lines hidden (view full) ---

408 }
409 }
410 (void)printf("%-*.*s %-*.*s %-*.*s ",
411 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
412 UT_LINESIZE, UT_LINESIZE,
413 strncmp(ep->utmp.ut_line, "tty", 3) &&
414 strncmp(ep->utmp.ut_line, "cua", 3) ?
415 ep->utmp.ut_line : ep->utmp.ut_line + 3,
417 UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
416 W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
418 pr_attime(&ep->utmp.ut_time, &now);
419 longidle = pr_idle(ep->idle);
420 (void)printf("%.*s\n", argwidth - longidle, ep->args);
421 }
422 (void)kvm_close(kd);
423 exit(0);
424}
425

--- 111 unchanged lines hidden ---
417 pr_attime(&ep->utmp.ut_time, &now);
418 longidle = pr_idle(ep->idle);
419 (void)printf("%.*s\n", argwidth - longidle, ep->args);
420 }
421 (void)kvm_close(kd);
422 exit(0);
423}
424

--- 111 unchanged lines hidden ---