1997-05-21 17:53 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / login / getutent_r.c
blob580dcffb21b06d4bc6ac305ba0ae85f125bc70d8
1 /* Copyright (C) 1996 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 <assert.h>
22 #include <db.h>
23 #include <fcntl.h>
24 #include <libc-lock.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <utmp.h>
30 #include <sys/stat.h>
32 #include "utmp-private.h"
35 /* The various backends we have. */
36 static int __setutent_unknown (int reset);
37 static int __getutent_r_unknown (struct utmp *buffer, struct utmp **result);
38 static struct utmp *__pututline_unknown (const struct utmp *data);
39 static void __endutent_unknown (void);
42 /* We have three jump tables: unknown, db, or file. */
43 static struct utfuncs unknown_functions =
45 __setutent_unknown,
46 __getutent_r_unknown,
47 NULL,
48 NULL,
49 __pututline_unknown,
50 __endutent_unknown,
51 NULL
54 /* Currently selected backend. */
55 struct utfuncs *__libc_utmp_jump_table = &unknown_functions;
57 /* The tables from the services. */
58 extern struct utfuncs __libc_utmp_db_functions;
59 extern struct utfuncs __libc_utmp_file_functions;
62 /* We need to protect the opening of the file. */
63 __libc_lock_define_initialized (, __libc_utmp_lock)
65 void
66 __setutent (void)
68 __libc_lock_lock (__libc_utmp_lock);
70 (void) (*__libc_utmp_jump_table->setutent) (1);
72 __libc_lock_unlock (__libc_utmp_lock);
74 weak_alias (__setutent, setutent)
77 static int
78 __setutent_unknown (int reset)
80 /* We have to test whether it is still not decided which backend to use. */
81 assert (__libc_utmp_jump_table == &unknown_functions);
83 /* See whether utmp db file exists. */
84 if ((*__libc_utmp_db_functions.setutent) (reset))
85 __libc_utmp_jump_table = &__libc_utmp_db_functions;
86 else
88 /* Either the db file does not exist or we have other
89 problems. So use the normal file. */
90 (*__libc_utmp_file_functions.setutent) (reset);
91 __libc_utmp_jump_table = &__libc_utmp_file_functions;
94 return 0;
98 void
99 __endutent (void)
101 __libc_lock_lock (__libc_utmp_lock);
103 (*__libc_utmp_jump_table->endutent) ();
105 __libc_lock_unlock (__libc_utmp_lock);
107 weak_alias (__endutent, endutent)
110 static void
111 __endutent_unknown (void)
113 /* Huh, how do we came here? Nothing to do. */
118 __getutent_r (struct utmp *buffer, struct utmp **result)
120 int retval;
122 __libc_lock_lock (__libc_utmp_lock);
124 retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result);
126 __libc_lock_unlock (__libc_utmp_lock);
128 return retval;
130 weak_alias (__getutent_r, getutent_r)
133 static int
134 __getutent_r_unknown (struct utmp *buffer, struct utmp **result)
136 /* It is not yet initialized. */
137 __setutent_unknown (0);
139 return (*__libc_utmp_jump_table->getutent_r) (buffer, result);
143 struct utmp *
144 __pututline (const struct utmp *data)
146 struct utmp *buffer;
148 __libc_lock_lock (__libc_utmp_lock);
150 buffer = (*__libc_utmp_jump_table->pututline) (data);
152 __libc_lock_unlock (__libc_utmp_lock);
154 return buffer;
156 weak_alias (__pututline, pututline)
159 static struct utmp *
160 __pututline_unknown (const struct utmp *data)
162 /* It is not yet initialized. */
163 __setutent_unknown (0);
165 return (*__libc_utmp_jump_table->pututline) (data);
170 __utmpname (const char *file)
172 int result = -1;
174 __libc_lock_lock (__libc_utmp_lock);
176 /* Close the old file. */
177 (*__libc_utmp_jump_table->endutent) ();
179 /* Store new names. */
180 if ((*__libc_utmp_file_functions.utmpname) (file) == 0
181 && !(*__libc_utmp_db_functions.utmpname) (file) == 0)
183 /* Try to find out whether we are supposed to work with a db
184 file or not. Do this by looking for the extension ".db". */
185 const char *ext = strrchr (file, '.');
187 if (ext != NULL && strcmp (ext, ".db") == 0)
188 __libc_utmp_jump_table = &__libc_utmp_db_functions;
189 else
190 __libc_utmp_jump_table = &unknown_functions;
192 result = 0;
195 __libc_lock_unlock (__libc_utmp_lock);
197 return result;
199 weak_alias (__utmpname, utmpname)