utmpname.c 1.9 KB

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