hurd: Mark various conditions as unlikely
[glibc.git] / login / utmpname.c
blob99484b8f9239616f848ed1cec631d2b6c4d6b7c5
1 /* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <libc-lock.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <utmp.h>
23 #include "utmp-private.h"
26 /* This is the default name. */
27 static const char default_file_name[] = _PATH_UTMP;
29 /* Current file name. */
30 const char *__libc_utmp_file_name = (const char *) default_file_name;
32 /* We have to use the lock in getutent_r.c. */
33 __libc_lock_define (extern, __libc_utmp_lock attribute_hidden)
36 int
37 __utmpname (const char *file)
39 int result = -1;
41 __libc_lock_lock (__libc_utmp_lock);
43 /* Close the old file. */
44 __libc_endutent ();
46 if (strcmp (file, __libc_utmp_file_name) != 0)
48 if (strcmp (file, default_file_name) == 0)
50 free ((char *) __libc_utmp_file_name);
52 __libc_utmp_file_name = default_file_name;
54 else
56 char *file_name = __strdup (file);
57 if (file_name == NULL)
58 /* Out of memory. */
59 goto done;
61 if (__libc_utmp_file_name != default_file_name)
62 free ((char *) __libc_utmp_file_name);
64 __libc_utmp_file_name = file_name;
68 result = 0;
70 done:
71 __libc_lock_unlock (__libc_utmp_lock);
72 return result;
74 libc_hidden_def (__utmpname)
75 weak_alias (__utmpname, utmpname)