From owner-svn-src-head@FreeBSD.ORG Sat Dec 5 20:22:26 2009 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 81A771065670; Sat, 5 Dec 2009 20:22:26 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 7055E8FC08; Sat, 5 Dec 2009 20:22:26 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id nB5KMQwR098956; Sat, 5 Dec 2009 20:22:26 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id nB5KMQNJ098953; Sat, 5 Dec 2009 20:22:26 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <200912052022.nB5KMQNJ098953@svn.freebsd.org> From: Ed Schouten Date: Sat, 5 Dec 2009 20:22:26 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r200160 - head/usr.bin/write X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Dec 2009 20:22:26 -0000 Author: ed Date: Sat Dec 5 20:22:26 2009 New Revision: 200160 URL: http://svn.freebsd.org/changeset/base/200160 Log: Let wall(1) use utmpx. Because our implementation guarantees the strings inside struct utmpx to be null terminated, we don't need to copy everything out, which makes the code nicer to read. Also set WARNS to 6 and add $FreeBSD$ to keep SVN happy. Modified: head/usr.bin/write/Makefile head/usr.bin/write/write.c Modified: head/usr.bin/write/Makefile ============================================================================== --- head/usr.bin/write/Makefile Sat Dec 5 20:17:04 2009 (r200159) +++ head/usr.bin/write/Makefile Sat Dec 5 20:22:26 2009 (r200160) @@ -1,7 +1,13 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 +# $FreeBSD$ PROG= write BINMODE=2555 BINGRP= tty +WARNS?= 6 + +DPADD= ${LIBULOG} +LDADD= -lulog + .include Modified: head/usr.bin/write/write.c ============================================================================== --- head/usr.bin/write/write.c Sat Dec 5 20:17:04 2009 (r200159) +++ head/usr.bin/write/write.c Sat Dec 5 20:22:26 2009 (r200160) @@ -62,8 +62,9 @@ __FBSDID("$FreeBSD$"); #include #include #include +#define _ULOG_POSIX_NAMES +#include #include -#include void done(int); void do_write(char *, char *, uid_t); @@ -146,20 +147,17 @@ usage(void) int utmp_chk(char *user, char *tty) { - struct utmp u; - int ufd; + struct utmpx lu, *u; - if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) - return(0); /* ignore error, shouldn't happen anyway */ - - while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) - if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && - strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { - (void)close(ufd); + strncpy(lu.ut_line, tty, sizeof lu.ut_line); + setutxent(); + while ((u = getutxline(&lu)) != NULL) + if (u->ut_type == USER_PROCESS && + strcmp(user, u->ut_user) == 0) { + endutxent(); return(0); } - - (void)close(ufd); + endutxent(); return(1); } @@ -177,43 +175,40 @@ utmp_chk(char *user, char *tty) void search_utmp(char *user, char *tty, char *mytty, uid_t myuid) { - struct utmp u; + struct utmpx *u; time_t bestatime, atime; - int ufd, nloggedttys, nttys, msgsok, user_is_me; - char atty[UT_LINESIZE + 1]; - - if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) - err(1, "utmp"); + int nloggedttys, nttys, msgsok, user_is_me; nloggedttys = nttys = 0; bestatime = 0; user_is_me = 0; - while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) - if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { + + setutxent(); + while ((u = getutxent()) != NULL) + if (u->ut_type == USER_PROCESS && + strcmp(user, u->ut_user) == 0) { ++nloggedttys; - (void)strncpy(atty, u.ut_line, UT_LINESIZE); - atty[UT_LINESIZE] = '\0'; - if (term_chk(atty, &msgsok, &atime, 0)) + if (term_chk(u->ut_line, &msgsok, &atime, 0)) continue; /* bad term? skip */ if (myuid && !msgsok) continue; /* skip ttys with msgs off */ - if (strcmp(atty, mytty) == 0) { + if (strcmp(u->ut_line, mytty) == 0) { user_is_me = 1; continue; /* don't write to yourself */ } ++nttys; if (atime > bestatime) { bestatime = atime; - (void)strcpy(tty, atty); + (void)strlcpy(tty, u->ut_line, MAXPATHLEN); } } + endutxent(); - (void)close(ufd); if (nloggedttys == 0) errx(1, "%s is not logged in", user); if (nttys == 0) { if (user_is_me) { /* ok, so write to yourself! */ - (void)strcpy(tty, mytty); + (void)strlcpy(tty, mytty, MAXPATHLEN); return; } errx(1, "%s has messages disabled", user);