]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1997-2021 Free Software Foundation, Inc. |
76b87c03 | 2 | This file is part of the GNU C Library. |
76b87c03 UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
76b87c03 UD |
8 | |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
76b87c03 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
76b87c03 | 17 | |
ec999b8e | 18 | #include <libc-lock.h> |
76b87c03 UD |
19 | #include <stdlib.h> |
20 | #include <string.h> | |
21 | #include <utmp.h> | |
22 | ||
23 | #include "utmp-private.h" | |
24 | ||
25 | ||
26 | /* This is the default name. */ | |
27 | static const char default_file_name[] = _PATH_UTMP; | |
28 | ||
29 | /* Current file name. */ | |
30 | const char *__libc_utmp_file_name = (const char *) default_file_name; | |
31 | ||
32 | /* We have to use the lock in getutent_r.c. */ | |
ab26a24a | 33 | __libc_lock_define (extern, __libc_utmp_lock attribute_hidden) |
76b87c03 UD |
34 | |
35 | ||
36 | int | |
37 | __utmpname (const char *file) | |
38 | { | |
39 | int result = -1; | |
40 | ||
41 | __libc_lock_lock (__libc_utmp_lock); | |
42 | ||
43 | /* Close the old file. */ | |
1a7fe2eb | 44 | __libc_endutent (); |
76b87c03 UD |
45 | |
46 | if (strcmp (file, __libc_utmp_file_name) != 0) | |
47 | { | |
48 | if (strcmp (file, default_file_name) == 0) | |
49 | { | |
fa3ac53f | 50 | free ((char *) __libc_utmp_file_name); |
76b87c03 UD |
51 | |
52 | __libc_utmp_file_name = default_file_name; | |
53 | } | |
54 | else | |
55 | { | |
56 | char *file_name = __strdup (file); | |
57 | if (file_name == NULL) | |
58 | /* Out of memory. */ | |
59 | goto done; | |
60 | ||
61 | if (__libc_utmp_file_name != default_file_name) | |
62 | free ((char *) __libc_utmp_file_name); | |
63 | ||
64 | __libc_utmp_file_name = file_name; | |
65 | } | |
66 | } | |
67 | ||
76b87c03 UD |
68 | result = 0; |
69 | ||
70 | done: | |
71 | __libc_lock_unlock (__libc_utmp_lock); | |
72 | return result; | |
73 | } | |
98164ba5 | 74 | libc_hidden_def (__utmpname) |
76b87c03 | 75 | weak_alias (__utmpname, utmpname) |