1*9292cfb2Schristos /* $NetBSD: utmp.c,v 1.10 2011/10/15 23:00:02 christos Exp $ */
27908c76cSchristos
37908c76cSchristos /*-
47908c76cSchristos * Copyright (c) 2002 The NetBSD Foundation, Inc.
57908c76cSchristos * All rights reserved.
67908c76cSchristos *
77908c76cSchristos * This code is derived from software contributed to The NetBSD Foundation
87908c76cSchristos * by Christos Zoulas.
97908c76cSchristos *
107908c76cSchristos * Redistribution and use in source and binary forms, with or without
117908c76cSchristos * modification, are permitted provided that the following conditions
127908c76cSchristos * are met:
137908c76cSchristos * 1. Redistributions of source code must retain the above copyright
147908c76cSchristos * notice, this list of conditions and the following disclaimer.
157908c76cSchristos * 2. Redistributions in binary form must reproduce the above copyright
167908c76cSchristos * notice, this list of conditions and the following disclaimer in the
177908c76cSchristos * documentation and/or other materials provided with the distribution.
187908c76cSchristos *
197908c76cSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
207908c76cSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
217908c76cSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
227908c76cSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
237908c76cSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
247908c76cSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257908c76cSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
267908c76cSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
277908c76cSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287908c76cSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
297908c76cSchristos * POSSIBILITY OF SUCH DAMAGE.
307908c76cSchristos */
317908c76cSchristos #include <sys/cdefs.h>
327908c76cSchristos
337908c76cSchristos #if defined(LIBC_SCCS) && !defined(lint)
34*9292cfb2Schristos __RCSID("$NetBSD: utmp.c,v 1.10 2011/10/15 23:00:02 christos Exp $");
357908c76cSchristos #endif /* LIBC_SCCS and not lint */
367908c76cSchristos
376352cc43Skleink #include "namespace.h"
38f04f8bc9Schristos #include <sys/types.h>
39f04f8bc9Schristos #include <sys/param.h>
407908c76cSchristos #include <stdio.h>
41b18986f5Snathanw #include <string.h>
427908c76cSchristos #include <time.h>
437908c76cSchristos #include <utmp.h>
44461a86f9Schristos #include <sys/stat.h>
457908c76cSchristos
467908c76cSchristos static struct utmp utmp;
477908c76cSchristos static FILE *ut;
48f04f8bc9Schristos static char utfile[MAXPATHLEN] = _PATH_UTMP;
497908c76cSchristos
507908c76cSchristos void
setutent(void)517908c76cSchristos setutent(void)
527908c76cSchristos {
53f084e3caSchristos if (ut == NULL)
54f084e3caSchristos return;
55f084e3caSchristos (void)fseeko(ut, (off_t)0, SEEK_SET);
567908c76cSchristos }
577908c76cSchristos
587908c76cSchristos struct utmp *
getutent(void)597908c76cSchristos getutent(void)
607908c76cSchristos {
61f084e3caSchristos if (ut == NULL) {
62461a86f9Schristos struct stat st;
63461a86f9Schristos off_t numentries;
64*9292cfb2Schristos if ((ut = fopen(utfile, "re")) == NULL)
657908c76cSchristos return NULL;
66461a86f9Schristos if (fstat(fileno(ut), &st) == -1)
67461a86f9Schristos goto out;
68461a86f9Schristos /*
69461a86f9Schristos * If we have a an old version utmp file bail.
70461a86f9Schristos */
71461a86f9Schristos numentries = st.st_size / sizeof(utmp);
726f550c4dSlukem if ((off_t)(numentries * sizeof(utmp)) != st.st_size)
73461a86f9Schristos goto out;
747908c76cSchristos }
75f084e3caSchristos if (fread(&utmp, sizeof(utmp), 1, ut) == 1)
76f084e3caSchristos return &utmp;
77461a86f9Schristos out:
78461a86f9Schristos (void)fclose(ut);
797908c76cSchristos return NULL;
807908c76cSchristos }
817908c76cSchristos
827908c76cSchristos void
endutent(void)837908c76cSchristos endutent(void)
847908c76cSchristos {
857908c76cSchristos if (ut != NULL) {
867908c76cSchristos (void)fclose(ut);
877908c76cSchristos ut = NULL;
887908c76cSchristos }
897908c76cSchristos }
90f04f8bc9Schristos
91f04f8bc9Schristos int
utmpname(const char * fname)92f04f8bc9Schristos utmpname(const char *fname)
93f04f8bc9Schristos {
94f04f8bc9Schristos size_t len = strlen(fname);
95f04f8bc9Schristos
96f04f8bc9Schristos if (len >= sizeof(utfile))
97f04f8bc9Schristos return 0;
98f04f8bc9Schristos
99f04f8bc9Schristos /* must not end in x! */
100f04f8bc9Schristos if (fname[len - 1] == 'x')
101f04f8bc9Schristos return 0;
102f04f8bc9Schristos
103233424cdSitojun (void)strlcpy(utfile, fname, sizeof(utfile));
104f04f8bc9Schristos endutent();
105f04f8bc9Schristos return 1;
106f04f8bc9Schristos }
107