update from main archive 961126
[glibc.git] / login / getutent_r.c
blobdf9a7977ab017786ea9386c87f3a6dbb2b182309
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 <gnu/lib-names.h>
31 #include <sys/stat.h>
33 #include "utmp-private.h"
34 #include "../elf/link.h"
37 /* The various backends we have. */
38 static int __setutent_unknown (int reset);
39 static int __getutent_r_unknown (struct utmp *buffer, struct utmp **result);
40 static void __pututline_unknown (const struct utmp *data);
41 static void __endutent_unknown (void);
44 /* We have three jump tables: unknown, db, or file. */
45 static struct utfuncs unknown_functions =
47 __setutent_unknown,
48 __getutent_r_unknown,
49 NULL,
50 NULL,
51 __pututline_unknown,
52 __endutent_unknown,
53 NULL
56 /* Currently selected backend. */
57 struct utfuncs *__libc_utmp_jump_table = &unknown_functions;
59 /* The tables from the services. */
60 extern struct utfuncs __libc_utmp_db_functions;
61 extern struct utfuncs __libc_utmp_file_functions;
64 /* We need to protect the opening of the file. */
65 __libc_lock_define_initialized (, __libc_utmp_lock)
67 void
68 __setutent (void)
70 __libc_lock_lock (__libc_utmp_lock);
72 (void) (*__libc_utmp_jump_table->setutent) (1);
74 __libc_lock_unlock (__libc_utmp_lock);
76 weak_alias (__setutent, setutent)
79 static int
80 __setutent_unknown (int reset)
82 /* We have to test whether it is still not decided which backend to use. */
83 assert (__libc_utmp_jump_table == &unknown_functions);
85 /* See whether utmp db file exists. */
86 if ((*__libc_utmp_db_functions.setutent) (reset))
87 __libc_utmp_jump_table = &__libc_utmp_db_functions;
88 else
90 /* Either the db file does not exist or we have other
91 problems. So use the normal file. */
92 (*__libc_utmp_file_functions.setutent) (reset);
93 __libc_utmp_jump_table = &__libc_utmp_file_functions;
96 return 0;
100 void
101 __endutent (void)
103 __libc_lock_lock (__libc_utmp_lock);
105 (*__libc_utmp_jump_table->endutent) ();
107 __libc_lock_unlock (__libc_utmp_lock);
109 weak_alias (__endutent, endutent)
112 static void
113 __endutent_unknown (void)
115 /* Huh, how do we came here? Nothing to do. */
120 __getutent_r (struct utmp *buffer, struct utmp **result)
122 int retval;
124 __libc_lock_lock (__libc_utmp_lock);
126 retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result);
128 __libc_lock_unlock (__libc_utmp_lock);
130 return retval;
132 weak_alias (__getutent_r, getutent_r)
135 static int
136 __getutent_r_unknown (struct utmp *buffer, struct utmp **result)
138 /* It is not yet initialized. */
139 __setutent_unknown (0);
141 return (*__libc_utmp_jump_table->getutent_r) (buffer, result);
145 void
146 __pututline (const struct utmp *data)
148 __libc_lock_lock (__libc_utmp_lock);
150 (*__libc_utmp_jump_table->pututline) (data);
152 __libc_lock_unlock (__libc_utmp_lock);
156 static void
157 __pututline_unknown (const struct utmp *data)
159 /* It is not yet initialized. */
160 __setutent_unknown (0);
162 (*__libc_utmp_jump_table->pututline) (data);
167 __utmpname (const char *file)
169 int result = -1;
171 __libc_lock_lock (__libc_utmp_lock);
173 /* Close the old file. */
174 (*__libc_utmp_jump_table->endutent) ();
176 /* Store new names. */
177 if ((*__libc_utmp_file_functions.utmpname) (file) == 0
178 && !(*__libc_utmp_db_functions.utmpname) (file) == 0)
180 /* Try to find out whether we are supposed to work with a db
181 file or not. Do this by looking for the extension ".db". */
182 const char *ext = strrchr (file, '.');
184 if (ext != NULL && strcmp (ext, ".db") == 0)
185 __libc_utmp_jump_table = &__libc_utmp_db_functions;
186 else
187 __libc_utmp_jump_table = &unknown_functions;
189 result = 0;
192 __libc_lock_unlock (__libc_utmp_lock);
194 return result;
196 weak_alias (__utmpname, utmpname)