Back to index

glibc  2.9
getlogin.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <unistd.h>
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <limits.h>
00024 #include <fcntl.h>
00025 
00026 #include <utmp.h>
00027 
00028 /* Return the login name of the user, or NULL if it can't be determined.
00029    The returned pointer, if not NULL, is good only until the next call.  */
00030 
00031 char *
00032 getlogin (void)
00033 {
00034   char tty_pathname[2 + 2 * NAME_MAX];
00035   char *real_tty_path = tty_pathname;
00036   char *result = NULL;
00037   static char name[UT_NAMESIZE + 1];
00038   struct utmp *ut, line, buffer;
00039 
00040   /* Get name of tty connected to fd 0.  Return NULL if not a tty or
00041      if fd 0 isn't open.  Note that a lot of documentation says that
00042      getlogin() is based on the controlling terminal---what they
00043      really mean is "the terminal connected to standard input".  The
00044      getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
00045      return NULL if fd 0 has been closed, so this is the compatible
00046      thing to do.  Note that ttyname(open("/dev/tty")) on those
00047      systems returns /dev/tty, so that is not a possible solution for
00048      getlogin().  */
00049   if (__ttyname_r (0, real_tty_path, sizeof (tty_pathname)) != 0)
00050     return NULL;
00051 
00052   real_tty_path += 5;              /* Remove "/dev/".  */
00053 
00054   __setutent ();
00055   strncpy (line.ut_line, real_tty_path, sizeof line.ut_line);
00056   if (__getutline_r (&line, &buffer, &ut) < 0)
00057     {
00058       if (errno == ESRCH)
00059        /* The caller expects ENOENT if nothing is found.  */
00060        __set_errno (ENOENT);
00061       result = NULL;
00062     }
00063   else
00064     {
00065       strncpy (name, ut->ut_user, UT_NAMESIZE);
00066       name[UT_NAMESIZE] = '\0';
00067       result = name;
00068     }
00069 
00070   __endutent ();
00071 
00072   return result;
00073 }