Update.
[platform/upstream/glibc.git] / login / getutent_r.c
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>
4    and Paul Janzen <pcj@primenet.com>, 1996.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <assert.h>
22 #include <bits/libc-lock.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <utmp.h>
27
28 #include "utmp-private.h"
29
30
31 /* The various backends we have.  */
32 static int getutent_r_unknown (struct utmp *buffer, struct utmp **result);
33 static struct utmp *pututline_unknown (const struct utmp *data);
34 static int setutent_unknown (void);
35 static void endutent_unknown (void);
36
37 /* Initial Jump table.  */
38 struct utfuncs __libc_utmp_unknown_functions =
39 {
40   setutent_unknown,
41   getutent_r_unknown,
42   NULL,
43   NULL,
44   pututline_unknown,
45   endutent_unknown,
46   NULL
47 };
48
49 /* Currently selected backend.  */
50 struct utfuncs *__libc_utmp_jump_table = &__libc_utmp_unknown_functions;
51
52 /* We need to protect the opening of the file.  */
53 __libc_lock_define_initialized (, __libc_utmp_lock)
54
55
56 void
57 __setutent (void)
58 {
59   __libc_lock_lock (__libc_utmp_lock);
60
61   (*__libc_utmp_jump_table->setutent) ();
62
63   __libc_lock_unlock (__libc_utmp_lock);
64 }
65 weak_alias (__setutent, setutent)
66 weak_alias (__setutent, setutxent)
67
68
69 void
70 __endutent (void)
71 {
72   __libc_lock_lock (__libc_utmp_lock);
73
74   (*__libc_utmp_jump_table->endutent) ();
75
76   __libc_lock_unlock (__libc_utmp_lock);
77 }
78 weak_alias (__endutent, endutent)
79 weak_alias (__endutent, endutxent)
80
81
82 static int
83 setutent_unknown (void)
84 {
85   int result;
86
87   /* See whether utmpd is running.  */
88   result = (*__libc_utmp_daemon_functions.setutent) ();
89   if (result)
90     __libc_utmp_jump_table = &__libc_utmp_daemon_functions;
91   else
92     {
93       result = (*__libc_utmp_file_functions.setutent) ();
94       __libc_utmp_jump_table = &__libc_utmp_file_functions;
95     }
96
97   return result;
98 }
99
100
101 static void
102 endutent_unknown (void)
103 {
104   /* Huh, how do we came here?  Nothing to do.  */
105 }
106
107
108 int
109 __getutent_r (struct utmp *buffer, struct utmp **result)
110 {
111   int retval;
112
113   __libc_lock_lock (__libc_utmp_lock);
114
115   retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result);
116
117   __libc_lock_unlock (__libc_utmp_lock);
118
119   return retval;
120 }
121 weak_alias (__getutent_r, getutent_r)
122
123
124 static int
125 getutent_r_unknown (struct utmp *buffer, struct utmp **result)
126 {
127   /* It is not yet initialized.  */
128   __setutent ();
129
130   return (*__libc_utmp_jump_table->getutent_r) (buffer, result);
131 }
132
133
134 struct utmp *
135 __pututline (const struct utmp *data)
136 {
137   struct utmp *buffer;
138
139   __libc_lock_lock (__libc_utmp_lock);
140
141   buffer = (*__libc_utmp_jump_table->pututline) (data);
142
143   __libc_lock_unlock (__libc_utmp_lock);
144
145   return buffer;
146 }
147 weak_alias (__pututline, pututline)
148 weak_alias (__pututline, pututxline)
149
150
151 static struct utmp *
152 pututline_unknown (const struct utmp *data)
153 {
154   /* It is not yet initialized.  */
155   __setutent ();
156
157   return (*__libc_utmp_jump_table->pututline) (data);
158 }