1 /* Copyright (C) 1997-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
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.
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
12    Lesser General Public License for more details.
13 
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/>.  */
17 
18 #include <libc-lock.h>
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.  */
__libc_lock_define(extern,__libc_utmp_lock attribute_hidden)33 __libc_lock_define (extern, __libc_utmp_lock attribute_hidden)
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.  */
44   __libc_endutent ();
45 
46   if (strcmp (file, __libc_utmp_file_name) != 0)
47     {
48       if (strcmp (file, default_file_name) == 0)
49 	{
50 	  free ((char *) __libc_utmp_file_name);
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 
68   result = 0;
69 
70 done:
71   __libc_lock_unlock (__libc_utmp_lock);
72   return result;
73 }
74 libc_hidden_def (__utmpname)
75 weak_alias (__utmpname, utmpname)
76