Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 6 Dec 2009 01:10:30 +0000 (UTC)
From:      Ed Schouten <ed@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r200172 - head/usr.bin/w
Message-ID:  <200912060110.nB61AUAF005505@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ed
Date: Sun Dec  6 01:10:30 2009
New Revision: 200172
URL: http://svn.freebsd.org/changeset/base/200172

Log:
  Let w(1) use utmpx.
  
  We don't have UT_*SIZE anymore. One of the reasons for that is because
  all strings are null terminated, there is no need for apps to copy
  strings out of the utmpx structure. This means we can define W_DISP*SIZE
  lengths for all columns.
  
  While there, adjust the sizes a little. Steal some bytes from the
  username column, while extending the hostname column quite a bit.

Modified:
  head/usr.bin/w/Makefile
  head/usr.bin/w/w.c

Modified: head/usr.bin/w/Makefile
==============================================================================
--- head/usr.bin/w/Makefile	Sun Dec  6 00:10:13 2009	(r200171)
+++ head/usr.bin/w/Makefile	Sun Dec  6 01:10:30 2009	(r200172)
@@ -4,8 +4,8 @@
 PROG=	w
 SRCS=	fmt.c pr_time.c proc_compare.c w.c
 MAN=	w.1 uptime.1
-DPADD=	${LIBKVM} ${LIBUTIL}
-LDADD=	-lkvm -lutil
+DPADD=	${LIBKVM} ${LIBULOG} ${LIBUTIL}
+LDADD=	-lkvm -lulog -lutil
 #BINGRP= kmem
 #BINMODE=2555
 LINKS=	${BINDIR}/w ${BINDIR}/uptime

Modified: head/usr.bin/w/w.c
==============================================================================
--- head/usr.bin/w/w.c	Sun Dec  6 00:10:13 2009	(r200171)
+++ head/usr.bin/w/w.c	Sun Dec  6 01:10:30 2009	(r200172)
@@ -83,14 +83,15 @@ static const char sccsid[] = "@(#)w.c	8.
 #include <stdlib.h>
 #include <string.h>
 #include <timeconv.h>
+#define	_ULOG_POSIX_NAMES
+#include <ulog.h>
 #include <unistd.h>
-#include <utmp.h>
 #include <vis.h>
 
 #include "extern.h"
 
 struct timeval	boottime;
-struct utmp	utmp;
+struct utmpx   *utmp;
 struct winsize	ws;
 kvm_t	       *kd;
 time_t		now;		/* the current time of day */
@@ -109,7 +110,7 @@ char	      **sel_users;	/* login array o
  */
 struct	entry {
 	struct	entry *next;
-	struct	utmp utmp;
+	struct	utmpx utmp;
 	dev_t	tdev;			/* dev_t of terminal */
 	time_t	idle;			/* idle time of terminal in seconds */
 	struct	kinfo_proc *kp;		/* `most interesting' proc */
@@ -119,11 +120,12 @@ struct	entry {
 
 #define	debugproc(p) *((struct kinfo_proc **)&(p)->ki_udata)
 
-/* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
-#define	W_DISPHOSTSIZE	16
+#define	W_DISPUSERSIZE	10
+#define	W_DISPLINESIZE	8
+#define	W_DISPHOSTSIZE	24
 
 static void		 pr_header(time_t *, int);
-static struct stat	*ttystat(char *, int);
+static struct stat	*ttystat(char *);
 static void		 usage(int);
 static int		 this_is_uptime(const char *s);
 
@@ -135,7 +137,6 @@ main(int argc, char *argv[])
 	struct kinfo_proc *kp;
 	struct kinfo_proc *dkp;
 	struct stat *stp;
-	FILE *ut;
 	time_t touched;
 	int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid;
 	const char *memf, *nlistf, *p;
@@ -208,16 +209,15 @@ main(int argc, char *argv[])
 		errx(1, "%s", errbuf);
 
 	(void)time(&now);
-	if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
-		err(1, "%s", _PATH_UTMP);
 
 	if (*argv)
 		sel_users = argv;
 
-	for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
-		if (utmp.ut_name[0] == '\0')
+	setutxent();
+	for (nusers = 0; (utmp = getutxent()) != NULL;) {
+		if (utmp->ut_type != USER_PROCESS)
 			continue;
-		if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
+		if (!(stp = ttystat(utmp->ut_line)))
 			continue;	/* corrupted record */
 		++nusers;
 		if (wcmd == 0)
@@ -228,7 +228,7 @@ main(int argc, char *argv[])
 
 			usermatch = 0;
 			for (user = sel_users; !usermatch && *user; user++)
-				if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
+				if (!strcmp(utmp->ut_user, *user))
 					usermatch = 1;
 			if (!usermatch)
 				continue;
@@ -237,7 +237,7 @@ main(int argc, char *argv[])
 			errx(1, "calloc");
 		*nextp = ep;
 		nextp = &ep->next;
-		memmove(&ep->utmp, &utmp, sizeof(struct utmp));
+		memmove(&ep->utmp, utmp, sizeof *utmp);
 		ep->tdev = stp->st_rdev;
 		/*
 		 * If this is the console device, attempt to ascertain
@@ -250,14 +250,14 @@ main(int argc, char *argv[])
 			(void)sysctlbyname("machdep.consdev", &ep->tdev, &size, NULL, 0);
 		}
 		touched = stp->st_atime;
-		if (touched < ep->utmp.ut_time) {
+		if (touched < ep->utmp.ut_tv.tv_sec) {
 			/* tty untouched since before login */
-			touched = ep->utmp.ut_time;
+			touched = ep->utmp.ut_tv.tv_sec;
 		}
 		if ((ep->idle = now - touched) < 0)
 			ep->idle = 0;
 	}
-	(void)fclose(ut);
+	endutxent();
 
 	if (header || wcmd == 0) {
 		pr_header(&now, nusers);
@@ -271,11 +271,11 @@ main(int argc, char *argv[])
 #define HEADER_FROM		"FROM"
 #define HEADER_LOGIN_IDLE	"LOGIN@  IDLE "
 #define HEADER_WHAT		"WHAT\n"
-#define WUSED  (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
+#define WUSED  (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \
 		sizeof(HEADER_LOGIN_IDLE) + 3)	/* header width incl. spaces */ 
 		(void)printf("%-*.*s %-*.*s %-*.*s  %s", 
-				UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
-				UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
+				W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER,
+				W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY,
 				W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
 				HEADER_LOGIN_IDLE HEADER_WHAT);
 	}
@@ -347,7 +347,6 @@ main(int argc, char *argv[])
 	}
 
 	for (ep = ehead; ep != NULL; ep = ep->next) {
-		char host_buf[UT_HOSTSIZE + 1];
 		struct addrinfo hints, *res;
 		struct sockaddr_storage ss;
 		struct sockaddr *sa = (struct sockaddr *)&ss;
@@ -356,9 +355,7 @@ main(int argc, char *argv[])
 		time_t t;
 		int isaddr;
 
-		host_buf[UT_HOSTSIZE] = '\0';
-		strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
-		p = *host_buf ? host_buf : "-";
+		p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
 		if ((x_suffix = strrchr(p, ':')) != NULL) {
 			if ((dot = strchr(x_suffix, '.')) != NULL &&
 			    strchr(dot+1, '.') == NULL)
@@ -419,13 +416,13 @@ main(int argc, char *argv[])
 			}
 		}
 		(void)printf("%-*.*s %-*.*s %-*.*s ",
-		    UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
-		    UT_LINESIZE, UT_LINESIZE,
+		    W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
+		    W_DISPLINESIZE, W_DISPLINESIZE,
 		    strncmp(ep->utmp.ut_line, "tty", 3) &&
 		    strncmp(ep->utmp.ut_line, "cua", 3) ?
 		    ep->utmp.ut_line : ep->utmp.ut_line + 3,
 		    W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
-		t = _time_to_time32(ep->utmp.ut_time);
+		t = ep->utmp.ut_tv.tv_sec;
 		longattime = pr_attime(&t, &now);
 		longidle = pr_idle(ep->idle);
 		(void)printf("%.*s\n", argwidth - longidle - longattime,
@@ -496,12 +493,12 @@ pr_header(time_t *nowp, int nusers)
 }
 
 static struct stat *
-ttystat(char *line, int sz)
+ttystat(char *line)
 {
 	static struct stat sb;
 	char ttybuf[MAXPATHLEN];
 
-	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
+	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
 	if (stat(ttybuf, &sb) == 0) {
 		return (&sb);
 	} else



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200912060110.nB61AUAF005505>