update from main archive 961126
[platform/upstream/glibc.git] / login / getutent_r.c
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.
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 <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>
32
33 #include "utmp-private.h"
34 #include "../elf/link.h"
35
36
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);
42
43
44 /* We have three jump tables: unknown, db, or file.  */
45 static struct utfuncs unknown_functions =
46 {
47   __setutent_unknown,
48   __getutent_r_unknown,
49   NULL,
50   NULL,
51   __pututline_unknown,
52   __endutent_unknown,
53   NULL
54 };
55
56 /* Currently selected backend.  */
57 struct utfuncs *__libc_utmp_jump_table = &unknown_functions;
58
59 /* The tables from the services.  */
60 extern struct utfuncs __libc_utmp_db_functions;
61 extern struct utfuncs __libc_utmp_file_functions;
62
63
64 /* We need to protect the opening of the file.  */
65 __libc_lock_define_initialized (, __libc_utmp_lock)
66
67 void
68 __setutent (void)
69 {
70   __libc_lock_lock (__libc_utmp_lock);
71
72   (void) (*__libc_utmp_jump_table->setutent) (1);
73
74   __libc_lock_unlock (__libc_utmp_lock);
75 }
76 weak_alias (__setutent, setutent)
77
78
79 static int
80 __setutent_unknown (int reset)
81 {
82   /* We have to test whether it is still not decided which backend to use.  */
83   assert (__libc_utmp_jump_table == &unknown_functions);
84
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
89     {
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;
94     }
95
96   return 0;
97 }
98
99
100 void
101 __endutent (void)
102 {
103   __libc_lock_lock (__libc_utmp_lock);
104
105   (*__libc_utmp_jump_table->endutent) ();
106
107   __libc_lock_unlock (__libc_utmp_lock);
108 }
109 weak_alias (__endutent, endutent)
110
111
112 static void
113 __endutent_unknown (void)
114 {
115   /* Huh, how do we came here?  Nothing to do.  */
116 }
117
118
119 int
120 __getutent_r (struct utmp *buffer, struct utmp **result)
121 {
122   int retval;
123
124   __libc_lock_lock (__libc_utmp_lock);
125
126   retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result);
127
128   __libc_lock_unlock (__libc_utmp_lock);
129
130   return retval;
131 }
132 weak_alias (__getutent_r, getutent_r)
133
134
135 static int
136 __getutent_r_unknown (struct utmp *buffer, struct utmp **result)
137 {
138   /* It is not yet initialized.  */
139   __setutent_unknown (0);
140
141   return (*__libc_utmp_jump_table->getutent_r) (buffer, result);
142 }
143
144
145 void
146 __pututline (const struct utmp *data)
147 {
148   __libc_lock_lock (__libc_utmp_lock);
149
150   (*__libc_utmp_jump_table->pututline) (data);
151
152   __libc_lock_unlock (__libc_utmp_lock);
153 }
154
155
156 static void
157 __pututline_unknown (const struct utmp *data)
158 {
159   /* It is not yet initialized.  */
160   __setutent_unknown (0);
161
162   (*__libc_utmp_jump_table->pututline) (data);
163 }
164
165
166 int
167 __utmpname (const char *file)
168 {
169   int result = -1;
170
171   __libc_lock_lock (__libc_utmp_lock);
172
173   /* Close the old file.  */
174   (*__libc_utmp_jump_table->endutent) ();
175
176   /* Store new names.  */
177   if ((*__libc_utmp_file_functions.utmpname) (file) == 0
178       && !(*__libc_utmp_db_functions.utmpname) (file) == 0)
179     {
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, '.');
183
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;
188
189       result = 0;
190     }
191
192   __libc_lock_unlock (__libc_utmp_lock);
193
194   return result;
195 }
196 weak_alias (__utmpname, utmpname)