summaryrefslogtreecommitdiffstats
path: root/lib/utent.c
blob: d5e6daea1206d2861c7ad259deacf131e35661e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * SPDX-FileCopyrightText: 1993 - 1994, Julianne Frances Haugh
 * SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
 * SPDX-FileCopyrightText: 2005       , Tomasz Kłoczko
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <config.h>

#ifndef	HAVE_GETUTENT

#include "defines.h"
#include <stdio.h>
#include <fcntl.h>
#include <utmp.h>

#ifndef	lint
static char rcsid[] = "$Id$";
#endif

static int utmp_fd = -1;
static struct utmp utmp_buf;

/*
 * setutent - open or rewind the utmp file
 */

void setutent (void)
{
	if (utmp_fd == -1)
		if ((utmp_fd = open (_UTMP_FILE, O_RDWR)) == -1)
			utmp_fd = open (_UTMP_FILE, O_RDONLY);

	if (utmp_fd != -1)
		lseek (utmp_fd, (off_t) 0L, SEEK_SET);
}

/*
 * endutent - close the utmp file
 */

void endutent (void)
{
	if (utmp_fd != -1)
		close (utmp_fd);

	utmp_fd = -1;
}

/*
 * getutent - get the next record from the utmp file
 */

struct utmp *getutent (void)
{
	if (utmp_fd == -1)
		setutent ();

	if (utmp_fd == -1)
		return 0;

	if (read (utmp_fd, &utmp_buf, sizeof utmp_buf) != sizeof utmp_buf)
		return 0;

	return &utmp_buf;
}
#else
extern int errno;		/* warning: ANSI C forbids an empty source file */
#endif