1*59a92d18SAlex Hornung /* $NetBSD: utmp.c,v 1.8 2009/01/11 02:46:27 christos Exp $ */ 2*59a92d18SAlex Hornung 3*59a92d18SAlex Hornung /*- 4*59a92d18SAlex Hornung * Copyright (c) 2002 The NetBSD Foundation, Inc. 5*59a92d18SAlex Hornung * All rights reserved. 6*59a92d18SAlex Hornung * 7*59a92d18SAlex Hornung * This code is derived from software contributed to The NetBSD Foundation 8*59a92d18SAlex Hornung * by Christos Zoulas. 9*59a92d18SAlex Hornung * 10*59a92d18SAlex Hornung * Redistribution and use in source and binary forms, with or without 11*59a92d18SAlex Hornung * modification, are permitted provided that the following conditions 12*59a92d18SAlex Hornung * are met: 13*59a92d18SAlex Hornung * 1. Redistributions of source code must retain the above copyright 14*59a92d18SAlex Hornung * notice, this list of conditions and the following disclaimer. 15*59a92d18SAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright 16*59a92d18SAlex Hornung * notice, this list of conditions and the following disclaimer in the 17*59a92d18SAlex Hornung * documentation and/or other materials provided with the distribution. 18*59a92d18SAlex Hornung * 19*59a92d18SAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*59a92d18SAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*59a92d18SAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*59a92d18SAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*59a92d18SAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*59a92d18SAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*59a92d18SAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*59a92d18SAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*59a92d18SAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*59a92d18SAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*59a92d18SAlex Hornung * POSSIBILITY OF SUCH DAMAGE. 30*59a92d18SAlex Hornung */ 31*59a92d18SAlex Hornung #include <sys/cdefs.h> 32*59a92d18SAlex Hornung 33*59a92d18SAlex Hornung #include "namespace.h" 34*59a92d18SAlex Hornung #include <sys/types.h> 35*59a92d18SAlex Hornung #include <sys/param.h> 36*59a92d18SAlex Hornung #include <stdio.h> 37*59a92d18SAlex Hornung #include <string.h> 38*59a92d18SAlex Hornung #include <time.h> 39*59a92d18SAlex Hornung #include <utmp.h> 40*59a92d18SAlex Hornung #include <sys/stat.h> 41*59a92d18SAlex Hornung 42*59a92d18SAlex Hornung static struct utmp utmp; 43*59a92d18SAlex Hornung static FILE *ut; 44*59a92d18SAlex Hornung static char utfile[MAXPATHLEN] = _PATH_UTMP; 45*59a92d18SAlex Hornung 46*59a92d18SAlex Hornung void 47*59a92d18SAlex Hornung setutent(void) 48*59a92d18SAlex Hornung { 49*59a92d18SAlex Hornung if (ut == NULL) 50*59a92d18SAlex Hornung return; 51*59a92d18SAlex Hornung (void)fseeko(ut, (off_t)0, SEEK_SET); 52*59a92d18SAlex Hornung } 53*59a92d18SAlex Hornung 54*59a92d18SAlex Hornung struct utmp * 55*59a92d18SAlex Hornung getutent(void) 56*59a92d18SAlex Hornung { 57*59a92d18SAlex Hornung if (ut == NULL) { 58*59a92d18SAlex Hornung struct stat st; 59*59a92d18SAlex Hornung off_t numentries; 60*59a92d18SAlex Hornung if ((ut = fopen(utfile, "r")) == NULL) 61*59a92d18SAlex Hornung return NULL; 62*59a92d18SAlex Hornung if (fstat(fileno(ut), &st) == -1) 63*59a92d18SAlex Hornung goto out; 64*59a92d18SAlex Hornung /* 65*59a92d18SAlex Hornung * If we have a an old version utmp file bail. 66*59a92d18SAlex Hornung */ 67*59a92d18SAlex Hornung numentries = st.st_size / sizeof(utmp); 68*59a92d18SAlex Hornung if ((off_t)(numentries * sizeof(utmp)) != st.st_size) 69*59a92d18SAlex Hornung goto out; 70*59a92d18SAlex Hornung } 71*59a92d18SAlex Hornung if (fread(&utmp, sizeof(utmp), 1, ut) == 1) 72*59a92d18SAlex Hornung return &utmp; 73*59a92d18SAlex Hornung out: 74*59a92d18SAlex Hornung (void)fclose(ut); 75*59a92d18SAlex Hornung return NULL; 76*59a92d18SAlex Hornung } 77*59a92d18SAlex Hornung 78*59a92d18SAlex Hornung void 79*59a92d18SAlex Hornung endutent(void) 80*59a92d18SAlex Hornung { 81*59a92d18SAlex Hornung if (ut != NULL) { 82*59a92d18SAlex Hornung (void)fclose(ut); 83*59a92d18SAlex Hornung ut = NULL; 84*59a92d18SAlex Hornung } 85*59a92d18SAlex Hornung } 86*59a92d18SAlex Hornung 87*59a92d18SAlex Hornung int 88*59a92d18SAlex Hornung utmpname(const char *fname) 89*59a92d18SAlex Hornung { 90*59a92d18SAlex Hornung size_t len = strlen(fname); 91*59a92d18SAlex Hornung 92*59a92d18SAlex Hornung if (len >= sizeof(utfile)) 93*59a92d18SAlex Hornung return 0; 94*59a92d18SAlex Hornung 95*59a92d18SAlex Hornung /* must not end in x! */ 96*59a92d18SAlex Hornung if (fname[len - 1] == 'x') 97*59a92d18SAlex Hornung return 0; 98*59a92d18SAlex Hornung 99*59a92d18SAlex Hornung (void)strlcpy(utfile, fname, sizeof(utfile)); 100*59a92d18SAlex Hornung endutent(); 101*59a92d18SAlex Hornung return 1; 102*59a92d18SAlex Hornung } 103