posix: Remove dynamic memory allocation from execl{e,p}
[glibc.git] / login / getutent_r.c
blob94143fd676eaa826853f60f2999ec575befe2f6f
1 /* Copyright (C) 1996-2016 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <libc-lock.h>
21 #include <stdlib.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 const 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 const 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 attribute_hidden)
56 static int
57 setutent_unknown (void)
59 int result;
61 result = (*__libc_utmp_file_functions.setutent) ();
62 if (result)
63 __libc_utmp_jump_table = &__libc_utmp_file_functions;
65 return result;
69 static int
70 getutent_r_unknown (struct utmp *buffer, struct utmp **result)
72 /* The backend was not yet initialized. */
73 if (setutent_unknown ())
74 return (*__libc_utmp_jump_table->getutent_r) (buffer, result);
76 /* Not available. */
77 *result = NULL;
78 return -1;
82 static int
83 getutid_r_unknown (const struct utmp *id, struct utmp *buffer,
84 struct utmp **result)
86 /* The backend was not yet initialized. */
87 if (setutent_unknown ())
88 return (*__libc_utmp_jump_table->getutid_r) (id, buffer, result);
90 /* Not available. */
91 *result = NULL;
92 return -1;
96 static int
97 getutline_r_unknown (const struct utmp *line, struct utmp *buffer,
98 struct utmp **result)
100 /* The backend was not yet initialized. */
101 if (setutent_unknown ())
102 return (*__libc_utmp_jump_table->getutline_r) (line, buffer, result);
104 /* Not available. */
105 *result = NULL;
106 return -1;
110 static struct utmp *
111 pututline_unknown (const struct utmp *data)
113 /* The backend was not yet initialized. */
114 if (setutent_unknown ())
115 return (*__libc_utmp_jump_table->pututline) (data);
117 /* Not available. */
118 return NULL;
122 static void
123 endutent_unknown (void)
125 /* Nothing to do. */
129 void
130 __setutent (void)
132 __libc_lock_lock (__libc_utmp_lock);
134 (*__libc_utmp_jump_table->setutent) ();
136 __libc_lock_unlock (__libc_utmp_lock);
138 weak_alias (__setutent, setutent)
142 __getutent_r (struct utmp *buffer, struct utmp **result)
144 int retval;
146 __libc_lock_lock (__libc_utmp_lock);
148 retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result);
150 __libc_lock_unlock (__libc_utmp_lock);
152 return retval;
154 weak_alias (__getutent_r, getutent_r)
157 struct utmp *
158 __pututline (const struct utmp *data)
160 struct utmp *buffer;
162 __libc_lock_lock (__libc_utmp_lock);
164 buffer = (*__libc_utmp_jump_table->pututline) (data);
166 __libc_lock_unlock (__libc_utmp_lock);
168 return buffer;
170 weak_alias (__pututline, pututline)
173 void
174 __endutent (void)
176 __libc_lock_lock (__libc_utmp_lock);
178 (*__libc_utmp_jump_table->endutent) ();
179 __libc_utmp_jump_table = &__libc_utmp_unknown_functions;
181 __libc_lock_unlock (__libc_utmp_lock);
183 weak_alias (__endutent, endutent)