Let talkd use utmpx instead of utmp.

Because strings are null terminated now, there is no need to copy
ut_line into a separate buffer first. Also enable WARNS.
This commit is contained in:
Ed Schouten 2009-12-25 11:12:05 +00:00
parent f195f6269e
commit 3eb56f7eff
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=200983
2 changed files with 17 additions and 20 deletions

View File

@ -6,6 +6,10 @@ SRCS= talkd.c announce.c process.c table.c print.c ttymsg.c
.PATH: ${.CURDIR}/../../usr.bin/wall .PATH: ${.CURDIR}/../../usr.bin/wall
MAN= talkd.8 MAN= talkd.8
CFLAGS+=-I${.CURDIR}/../../usr.bin/wall CFLAGS+=-I${.CURDIR}/../../usr.bin/wall
WFORMAT=0
WARNS?= 6
DPADD= ${LIBULOG}
LDADD= -lulog
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -59,6 +59,8 @@ static const char rcsid[] =
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#define _ULOG_POSIX_NAMES
#include <ulog.h>
#include "extern.h" #include "extern.h"
@ -181,55 +183,46 @@ do_announce(CTL_MSG *mp, CTL_RESPONSE *rp)
} }
} }
#include <utmp.h>
/* /*
* Search utmp for the local user * Search utmp for the local user
*/ */
int int
find_user(const char *name, char *tty) find_user(const char *name, char *tty)
{ {
struct utmp ubuf; struct utmpx *ut;
int status; int status;
FILE *fd;
struct stat statb; struct stat statb;
time_t best = 0; time_t best = 0;
char line[sizeof(ubuf.ut_line) + 1]; char ftty[sizeof(_PATH_DEV) - 1 + sizeof(ut->ut_line)];
char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
if ((fd = fopen(_PATH_UTMP, "r")) == NULL) { setutxent();
warnx("can't read %s", _PATH_UTMP);
return (FAILED);
}
#define SCMPN(a, b) strncmp(a, b, sizeof (a))
status = NOT_HERE; status = NOT_HERE;
(void) strcpy(ftty, _PATH_DEV); (void) strcpy(ftty, _PATH_DEV);
while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) while ((ut = getutxent()) != NULL)
if (SCMPN(ubuf.ut_name, name) == 0) { if (ut->ut_type == USER_PROCESS &&
strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line)); strcmp(ut->ut_user, name) == 0) {
line[sizeof(ubuf.ut_line)] = '\0';
if (*tty == '\0' || best != 0) { if (*tty == '\0' || best != 0) {
if (best == 0) if (best == 0)
status = PERMISSION_DENIED; status = PERMISSION_DENIED;
/* no particular tty was requested */ /* no particular tty was requested */
(void) strcpy(ftty + sizeof(_PATH_DEV) - 1, (void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
line); ut->ut_line);
if (stat(ftty, &statb) == 0) { if (stat(ftty, &statb) == 0) {
if (!(statb.st_mode & 020)) if (!(statb.st_mode & 020))
continue; continue;
if (statb.st_atime > best) { if (statb.st_atime > best) {
best = statb.st_atime; best = statb.st_atime;
(void) strcpy(tty, line); (void) strcpy(tty, ut->ut_line);
status = SUCCESS; status = SUCCESS;
continue; continue;
} }
} }
} }
if (strcmp(line, tty) == 0) { if (strcmp(ut->ut_line, tty) == 0) {
status = SUCCESS; status = SUCCESS;
break; break;
} }
} }
fclose(fd); endutxent();
return (status); return (status);
} }