Back to index

glibc  2.9
getlogin_r.c
Go to the documentation of this file.
00001 /* Reentrant function to return the current login name.  Unix version.
00002    Copyright (C) 1991,92,96,97,98,2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
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 <errno.h>
00021 #include <unistd.h>
00022 #include <string.h>
00023 #include <stdio.h>
00024 #include <limits.h>
00025 #include <fcntl.h>
00026 
00027 #include <utmp.h>
00028 #include "../login/utmp-private.h"
00029 
00030 /* Return at most NAME_LEN characters of the login name of the user in NAME.
00031    If it cannot be determined or some other error occurred, return the error
00032    code.  Otherwise return 0.  */
00033 
00034 int
00035 getlogin_r (name, name_len)
00036      char *name;
00037      size_t name_len;
00038 {
00039   char tty_pathname[2 + 2 * NAME_MAX];
00040   char *real_tty_path = tty_pathname;
00041   int result;
00042   struct utmp *ut, line, buffer;
00043 
00044   /* Get name of tty connected to fd 0.  Return if not a tty or
00045      if fd 0 isn't open.  Note that a lot of documentation says that
00046      getlogin() is based on the controlling terminal---what they
00047      really mean is "the terminal connected to standard input".  The
00048      getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
00049      return NULL if fd 0 has been closed, so this is the compatible
00050      thing to do.  Note that ttyname(open("/dev/tty")) on those
00051      systems returns /dev/tty, so that is not a possible solution for
00052      getlogin().  */
00053 
00054   result = __ttyname_r (0, real_tty_path, sizeof (tty_pathname));
00055 
00056   if (result != 0)
00057     return result;
00058 
00059   real_tty_path += 5;              /* Remove "/dev/".  */
00060   strncpy (line.ut_line, real_tty_path, sizeof line.ut_line);
00061 
00062   /* We don't use the normal entry points __setutent et al, because we
00063      want setutent + getutline_r + endutent all to happen with the lock
00064      held so that our search is thread-safe.  */
00065 
00066   __libc_lock_lock (__libc_utmp_lock);
00067   (*__libc_utmp_jump_table->setutent) ();
00068   result = (*__libc_utmp_jump_table->getutline_r) (&line, &buffer, &ut);
00069   if (result < 0)
00070     {
00071       if (errno == ESRCH)
00072        /* The caller expects ENOENT if nothing is found.  */
00073        result = ENOENT;
00074       else
00075        result = errno;
00076     }
00077   (*__libc_utmp_jump_table->endutent) ();
00078   __libc_utmp_jump_table = &__libc_utmp_unknown_functions;
00079   __libc_lock_unlock (__libc_utmp_lock);
00080 
00081   if (result == 0)
00082     {
00083       size_t needed = strlen (ut->ut_user) + 1;
00084 
00085       if (needed > name_len)
00086        {
00087          __set_errno (ERANGE);
00088          result = ERANGE;
00089        }
00090       else
00091        {
00092          memcpy (name, ut->ut_user, needed);
00093          result = 0;
00094        }
00095     }
00096 
00097   return result;
00098 }
00099 libc_hidden_def (getlogin_r)