Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / unix / getlogin.c
blobe646bcb089e6ec958d1949b427cb8936cb003b6c
1 #ifndef _NO_GETLOGIN
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <utmp.h>
7 #include <fcntl.h>
8 #include <_syslist.h>
10 char *
11 getlogin ()
13 int utmp_fd;
14 struct utmp utmp_buf;
15 static char buf[10];
16 extern char *ttyname ();
17 char *tty;
19 if ((tty = ttyname (0)) == 0)
20 return 0;
22 if ((utmp_fd = open (UTMP_FILE, O_RDONLY)) == -1)
23 return 0;
25 if (!strncmp (tty, "/dev/", 5))
26 tty += 5;
28 while (read (utmp_fd, &utmp_buf, sizeof (utmp_buf)) == sizeof (utmp_buf))
30 if (!strncmp (tty, utmp_buf.ut_line, sizeof (utmp_buf.ut_line))
31 && utmp_buf.ut_type == USER_PROCESS)
33 close (utmp_fd);
34 memset (buf, 0, sizeof (buf));
35 strncpy (buf, utmp_buf.ut_user, sizeof (utmp_buf.ut_user));
36 return buf;
40 close (utmp_fd);
41 return 0;
43 #endif /* !_NO_GETLOGIN */