Update.
[glibc.git] / login / getutent_r.c
blob8391331cd551d2b0562c7325dc69da7c90d7367b
1 /* Copyright (C) 1996, 1997, 1998 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.
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.
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.
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. */
21 #include <bits/libc-lock.h>
22 #include <utmp.h>
24 #include "utmp-private.h"
27 /* Functions defined here. */
28 static int setutent_unknown (void);
29 static int getutent_r_unknown (struct utmp *buffer, struct utmp **result);
30 static int getutid_r_unknown (const struct utmp *line, struct utmp *buffer,
31 struct utmp **result);
32 static int getutline_r_unknown (const struct utmp *id, struct utmp *buffer,
33 struct utmp **result);
34 static struct utmp *pututline_unknown (const struct utmp *data);
35 static void endutent_unknown (void);
37 /* Initial Jump table. */
38 struct utfuncs __libc_utmp_unknown_functions =
40 setutent_unknown,
41 getutent_r_unknown,
42 getutid_r_unknown,
43 getutline_r_unknown,
44 pututline_unknown,
45 endutent_unknown,
46 NULL
49 /* Currently selected backend. */
50 struct utfuncs *__libc_utmp_jump_table = &__libc_utmp_unknown_functions;
52 /* We need to protect the opening of the file. */
53 __libc_lock_define_initialized (, __libc_utmp_lock)
56 static int
57 setutent_unknown (void)
59 int result;
61 /* See whether utmpd is running. */
62 result = (*__libc_utmp_daemon_functions.setutent) ();
63 if (result)
64 __libc_utmp_jump_table = &__libc_utmp_daemon_functions;
65 else
67 result = (*__libc_utmp_file_functions.setutent) ();
68 if (result)
69 __libc_utmp_jump_table = &__libc_utmp_file_functions;
72 return result;
76 static int
77 getutent_r_unknown (struct utmp *buffer, struct utmp **result)
79 /* The backend was not yet initialized. */
80 if (setutent_unknown ())
81 return (*__libc_utmp_jump_table->getutent_r) (buffer, result);
83 /* Not available. */
84 *result = NULL;
85 return -1;
89 static int
90 getutid_r_unknown (const struct utmp *id, struct utmp *buffer,
91 struct utmp **result)
93 /* The backend was not yet initialized. */
94 if (setutent_unknown ())
95 return (*__libc_utmp_jump_table->getutid_r) (id, buffer, result);
97 /* Not available. */
98 *result = NULL;
99 return -1;
103 static int
104 getutline_r_unknown (const struct utmp *line, struct utmp *buffer,
105 struct utmp **result)
107 /* The backend was not yet initialized. */
108 if (setutent_unknown ())
109 return (*__libc_utmp_jump_table->getutline_r) (line, buffer, result);
111 /* Not available. */
112 *result = NULL;
113 return -1;
117 static struct utmp *
118 pututline_unknown (const struct utmp *data)
120 /* The backend was not yet initialized. */
121 if (setutent_unknown ())
122 return (*__libc_utmp_jump_table->pututline) (data);
124 /* Not available. */
125 return NULL;
129 static void
130 endutent_unknown (void)
132 /* Nothing to do. */
136 void
137 __setutent (void)
139 __libc_lock_lock (__libc_utmp_lock);
141 (*__libc_utmp_jump_table->setutent) ();
143 __libc_lock_unlock (__libc_utmp_lock);
145 weak_alias (__setutent, setutent)
149 __getutent_r (struct utmp *buffer, struct utmp **result)
151 int retval;
153 __libc_lock_lock (__libc_utmp_lock);
155 retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result);
157 __libc_lock_unlock (__libc_utmp_lock);
159 return retval;
161 weak_alias (__getutent_r, getutent_r)
164 struct utmp *
165 __pututline (const struct utmp *data)
167 struct utmp *buffer;
169 __libc_lock_lock (__libc_utmp_lock);
171 buffer = (*__libc_utmp_jump_table->pututline) (data);
173 __libc_lock_unlock (__libc_utmp_lock);
175 return buffer;
177 weak_alias (__pututline, pututline)
180 void
181 __endutent (void)
183 __libc_lock_lock (__libc_utmp_lock);
185 (*__libc_utmp_jump_table->endutent) ();
186 __libc_utmp_jump_table = &__libc_utmp_unknown_functions;
188 __libc_lock_unlock (__libc_utmp_lock);
190 weak_alias (__endutent, endutent)