Back to index

glibc  2.9
utmpname.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 2002, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <bits/libc-lock.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <utmp.h>
00024 
00025 #include "utmp-private.h"
00026 
00027 
00028 /* This is the default name.  */
00029 static const char default_file_name[] = _PATH_UTMP;
00030 
00031 /* Current file name.  */
00032 const char *__libc_utmp_file_name = (const char *) default_file_name;
00033 
00034 /* We have to use the lock in getutent_r.c.  */
00035 __libc_lock_define (extern, __libc_utmp_lock attribute_hidden)
00036 
00037 
00038 int
00039 __utmpname (const char *file)
00040 {
00041   int result = -1;
00042 
00043   __libc_lock_lock (__libc_utmp_lock);
00044 
00045   /* Close the old file.  */
00046   (*__libc_utmp_jump_table->endutent) ();
00047   __libc_utmp_jump_table = &__libc_utmp_unknown_functions;
00048 
00049   if (strcmp (file, __libc_utmp_file_name) != 0)
00050     {
00051       if (strcmp (file, default_file_name) == 0)
00052        {
00053          free ((char *) __libc_utmp_file_name);
00054 
00055          __libc_utmp_file_name = default_file_name;
00056        }
00057       else
00058        {
00059          char *file_name = __strdup (file);
00060          if (file_name == NULL)
00061            /* Out of memory.  */
00062            goto done;
00063 
00064          if (__libc_utmp_file_name != default_file_name)
00065            free ((char *) __libc_utmp_file_name);
00066 
00067          __libc_utmp_file_name = file_name;
00068        }
00069     }
00070 
00071   result = 0;
00072 
00073 done:
00074   __libc_lock_unlock (__libc_utmp_lock);
00075   return result;
00076 }
00077 weak_alias (__utmpname, utmpname)