Remove dead code from x86-32 SSSE3 strncmp.
[glibc.git] / sysdeps / unix / getlogin.c
blob1fb70733fb59dff8932555f4204c11b9dcb9f4f1
1 /* Copyright (C) 1991, 1992, 1996, 1997, 2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <limits.h>
24 #include <fcntl.h>
26 #include <utmp.h>
28 static char name[UT_NAMESIZE + 1];
30 /* Return the login name of the user, or NULL if it can't be determined.
31 The returned pointer, if not NULL, is good only until the next call. */
33 #ifdef STATIC
34 STATIC
35 #endif
36 char *
37 getlogin (void)
39 char tty_pathname[2 + 2 * NAME_MAX];
40 char *real_tty_path = tty_pathname;
41 int err;
42 char *result = NULL;
43 struct utmp *ut, line, buffer;
45 /* Get name of tty connected to fd 0. Return NULL if not a tty or
46 if fd 0 isn't open. Note that a lot of documentation says that
47 getlogin() is based on the controlling terminal---what they
48 really mean is "the terminal connected to standard input". The
49 getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
50 return NULL if fd 0 has been closed, so this is the compatible
51 thing to do. Note that ttyname(open("/dev/tty")) on those
52 systems returns /dev/tty, so that is not a possible solution for
53 getlogin(). */
54 err = __ttyname_r (0, real_tty_path, sizeof (tty_pathname));
55 if (err != 0)
57 __set_errno (err);
58 return NULL;
61 real_tty_path += 5; /* Remove "/dev/". */
63 __setutent ();
64 strncpy (line.ut_line, real_tty_path, sizeof line.ut_line);
65 if (__getutline_r (&line, &buffer, &ut) < 0)
67 if (errno == ESRCH)
68 /* The caller expects ENOENT if nothing is found. */
69 __set_errno (ENOENT);
70 result = NULL;
72 else
74 strncpy (name, ut->ut_user, UT_NAMESIZE);
75 name[UT_NAMESIZE] = '\0';
76 result = name;
79 __endutent ();
81 return result;