xref: /minix3/lib/libutil/logwtmp.c (revision 0c3983b25a88161cf074524e5c94585a2582ae82)
1*0c3983b2SBen Gras /*	$NetBSD: logwtmp.c,v 1.14 2003/08/07 16:44:59 agc Exp $	*/
2*0c3983b2SBen Gras 
3*0c3983b2SBen Gras /*
4*0c3983b2SBen Gras  * Copyright (c) 1988, 1993
5*0c3983b2SBen Gras  *	The Regents of the University of California.  All rights reserved.
6*0c3983b2SBen Gras  *
7*0c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
8*0c3983b2SBen Gras  * modification, are permitted provided that the following conditions
9*0c3983b2SBen Gras  * are met:
10*0c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
11*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
12*0c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*0c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
15*0c3983b2SBen Gras  * 3. Neither the name of the University nor the names of its contributors
16*0c3983b2SBen Gras  *    may be used to endorse or promote products derived from this software
17*0c3983b2SBen Gras  *    without specific prior written permission.
18*0c3983b2SBen Gras  *
19*0c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*0c3983b2SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0c3983b2SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0c3983b2SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*0c3983b2SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0c3983b2SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0c3983b2SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0c3983b2SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0c3983b2SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0c3983b2SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0c3983b2SBen Gras  * SUCH DAMAGE.
30*0c3983b2SBen Gras  */
31*0c3983b2SBen Gras 
32*0c3983b2SBen Gras #include <sys/cdefs.h>
33*0c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0c3983b2SBen Gras #if 0
35*0c3983b2SBen Gras static char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
36*0c3983b2SBen Gras #else
37*0c3983b2SBen Gras __RCSID("$NetBSD: logwtmp.c,v 1.14 2003/08/07 16:44:59 agc Exp $");
38*0c3983b2SBen Gras #endif
39*0c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
40*0c3983b2SBen Gras 
41*0c3983b2SBen Gras #include <sys/types.h>
42*0c3983b2SBen Gras #include <sys/file.h>
43*0c3983b2SBen Gras #include <sys/time.h>
44*0c3983b2SBen Gras #include <sys/stat.h>
45*0c3983b2SBen Gras 
46*0c3983b2SBen Gras #include <assert.h>
47*0c3983b2SBen Gras #include <string.h>
48*0c3983b2SBen Gras #include <time.h>
49*0c3983b2SBen Gras #include <unistd.h>
50*0c3983b2SBen Gras #include <utmp.h>
51*0c3983b2SBen Gras #include <util.h>
52*0c3983b2SBen Gras 
53*0c3983b2SBen Gras void
logwtmp(const char * line,const char * name,const char * host)54*0c3983b2SBen Gras logwtmp(const char *line, const char *name, const char *host)
55*0c3983b2SBen Gras {
56*0c3983b2SBen Gras 	struct utmp ut;
57*0c3983b2SBen Gras 	struct stat buf;
58*0c3983b2SBen Gras 	int fd;
59*0c3983b2SBen Gras 
60*0c3983b2SBen Gras 	_DIAGASSERT(line != NULL);
61*0c3983b2SBen Gras 	_DIAGASSERT(name != NULL);
62*0c3983b2SBen Gras 	_DIAGASSERT(host != NULL);
63*0c3983b2SBen Gras 
64*0c3983b2SBen Gras 	if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
65*0c3983b2SBen Gras 		return;
66*0c3983b2SBen Gras 	if (fstat(fd, &buf) == 0) {
67*0c3983b2SBen Gras 		(void) strncpy(ut.ut_line, line, sizeof(ut.ut_line));
68*0c3983b2SBen Gras 		(void) strncpy(ut.ut_name, name, sizeof(ut.ut_name));
69*0c3983b2SBen Gras 		(void) strncpy(ut.ut_host, host, sizeof(ut.ut_host));
70*0c3983b2SBen Gras 		(void) time(&ut.ut_time);
71*0c3983b2SBen Gras 		if (write(fd, &ut, sizeof(struct utmp)) != sizeof(struct utmp))
72*0c3983b2SBen Gras 			(void) ftruncate(fd, buf.st_size);
73*0c3983b2SBen Gras 	}
74*0c3983b2SBen Gras 	(void) close(fd);
75*0c3983b2SBen Gras }
76