Initial commit for Tizen
[profile/extras/shadow-utils.git] / lib / utent.c
1 /*
2  * Copyright (c) 1993 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1998, Marek Michałkiewicz
4  * Copyright (c) 2005       , Tomasz Kłoczko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holders or contributors may not be used to
16  *    endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <config.h>
33
34 #ifndef HAVE_GETUTENT
35
36 #include "defines.h"
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <utmp.h>
40
41 #ifndef lint
42 static char rcsid[] = "$Id: utent.c 1980 2008-04-27 00:40:09Z nekral-guest $";
43 #endif
44
45 static int utmp_fd = -1;
46 static struct utmp utmp_buf;
47
48 /*
49  * setutent - open or rewind the utmp file
50  */
51
52 void setutent (void)
53 {
54         if (utmp_fd == -1)
55                 if ((utmp_fd = open (_UTMP_FILE, O_RDWR)) == -1)
56                         utmp_fd = open (_UTMP_FILE, O_RDONLY);
57
58         if (utmp_fd != -1)
59                 lseek (utmp_fd, (off_t) 0L, SEEK_SET);
60 }
61
62 /*
63  * endutent - close the utmp file
64  */
65
66 void endutent (void)
67 {
68         if (utmp_fd != -1)
69                 close (utmp_fd);
70
71         utmp_fd = -1;
72 }
73
74 /*
75  * getutent - get the next record from the utmp file
76  */
77
78 struct utmp *getutent (void)
79 {
80         if (utmp_fd == -1)
81                 setutent ();
82
83         if (utmp_fd == -1)
84                 return 0;
85
86         if (read (utmp_fd, &utmp_buf, sizeof utmp_buf) != sizeof utmp_buf)
87                 return 0;
88
89         return &utmp_buf;
90 }
91
92 /*
93  * getutline - get the utmp entry matching ut_line
94  */
95
96 struct utmp *getutline (const struct utmp *utent)
97 {
98         struct utmp save;
99         struct utmp *new;
100
101         save = *utent;
102         while (new = getutent ())
103                 if (strncmp (new->ut_line, save.ut_line, sizeof new->ut_line))
104                         continue;
105                 else
106                         return new;
107
108         return (struct utmp *) 0;
109 }
110 #else
111 extern int errno;               /* warning: ANSI C forbids an empty source file */
112 #endif