- Don't core dump when we have stale utmp entries; warn the user instead.

- Make this work again in single user when invoked as uptime and utmp is
  not present.
This commit is contained in:
christos 2000-08-21 00:19:06 +00:00
parent 1ef7adc1bf
commit 2120a34cd6

View File

@ -1,4 +1,4 @@
/* $NetBSD: w.c,v 1.40 2000/07/22 03:14:06 simonb Exp $ */ /* $NetBSD: w.c,v 1.41 2000/08/21 00:19:06 christos Exp $ */
/*- /*-
* Copyright (c) 1980, 1991, 1993, 1994 * Copyright (c) 1980, 1991, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
#if 0 #if 0
static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94"; static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94";
#else #else
__RCSID("$NetBSD: w.c,v 1.40 2000/07/22 03:14:06 simonb Exp $"); __RCSID("$NetBSD: w.c,v 1.41 2000/08/21 00:19:06 christos Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -180,13 +180,13 @@ main(argc, argv)
errx(1, "%s", errbuf); errx(1, "%s", errbuf);
(void)time(&now); (void)time(&now);
if ((ut = fopen(_PATH_UTMP, "r")) == NULL) if ((ut = fopen(_PATH_UTMP, "r")) == NULL && wcmd)
err(1, "%s", _PATH_UTMP); err(1, "%s", _PATH_UTMP);
if (*argv) if (*argv)
sel_user = *argv; sel_user = *argv;
for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { for (nusers = 0; ut && fread(&utmp, sizeof(utmp), 1, ut);) {
if (utmp.ut_name[0] == '\0') if (utmp.ut_name[0] == '\0')
continue; continue;
++nusers; ++nusers;
@ -217,7 +217,10 @@ main(argc, argv)
if ((ep->idle = now - stp->st_atime) < 0) if ((ep->idle = now - stp->st_atime) < 0)
ep->idle = 0; ep->idle = 0;
} }
if (ut)
(void)fclose(ut); (void)fclose(ut);
else
nusers = 1;
if (header || wcmd == 0) { if (header || wcmd == 0) {
pr_header(&now, nusers); pr_header(&now, nusers);
@ -319,6 +322,13 @@ main(argc, argv)
(int)(ep->utmp.ut_host + UT_HOSTSIZE - x), x); (int)(ep->utmp.ut_host + UT_HOSTSIZE - x), x);
p = buf; p = buf;
} }
if (ep->kp == NULL) {
warnx("Stale utmp entry: %.*s %.*s %.*s",
UT_NAMESIZE, ep->utmp.ut_name,
UT_LINESIZE, ep->utmp.ut_line,
UT_HOSTSIZE, ep->utmp.ut_host);
continue;
}
(void)printf("%-*s %-2.2s %-*.*s ", (void)printf("%-*s %-2.2s %-*.*s ",
lognamelen, ep->kp->p_login, lognamelen, ep->kp->p_login,
(strncmp(ep->utmp.ut_line, "tty", 3) && (strncmp(ep->utmp.ut_line, "tty", 3) &&