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. */
24 #include <libc-lock.h>
30 #include <gnu/lib-names.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
=
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
)
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
)
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
;
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
;
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
)
113 __endutent_unknown (void)
115 /* Huh, how do we came here? Nothing to do. */
120 __getutent_r (struct utmp
*buffer
, struct utmp
**result
)
124 __libc_lock_lock (__libc_utmp_lock
);
126 retval
= (*__libc_utmp_jump_table
->getutent_r
) (buffer
, result
);
128 __libc_lock_unlock (__libc_utmp_lock
);
132 weak_alias (__getutent_r
, getutent_r
)
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
);
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
);
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
)
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
;
187 __libc_utmp_jump_table
= &unknown_functions
;
192 __libc_lock_unlock (__libc_utmp_lock
);
196 weak_alias (__utmpname
, utmpname
)