update from main archive 970619
[glibc/history.git] / login / login.c
blobc148893c8882a09e4a62fe20d7f075857ca30367
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <utmp.h>
28 /* Return the result of ttyname in the buffer pointed to by TTY, which should
29 be of length BUF_LEN. If it is too long to fit in this buffer, a
30 sufficiently long buffer is allocated using malloc, and returned in TTY.
31 0 is returned upon success, -1 otherwise. */
32 static int
33 tty_name (int fd, char **tty, size_t buf_len)
35 int rv; /* Return value. 0 = success. */
36 char *buf = *tty; /* Buffer for ttyname, initially the user's. */
38 for (;;)
40 char *new_buf;
42 if (buf_len)
44 rv = ttyname_r (fd, buf, buf_len);
46 if (rv < 0 || memchr (buf, '\0', buf_len))
47 /* We either got an error, or we succeeded and the
48 returned name fit in the buffer. */
49 break;
51 /* Try again with a longer buffer. */
52 buf_len += buf_len; /* Double it */
54 else
55 /* No initial buffer; start out by mallocing one. */
56 buf_len = 128; /* First time guess. */
58 if (buf != *tty)
59 /* We've already malloced another buffer at least once. */
60 new_buf = realloc (buf, buf_len);
61 else
62 new_buf = malloc (buf_len);
63 if (! new_buf)
65 rv = -1;
66 __set_errno (ENOMEM);
67 break;
71 if (rv == 0)
72 *tty = buf; /* Return buffer to the user. */
73 else if (buf != *tty)
74 free (buf); /* Free what we malloced when returning an error. */
76 return rv;
79 void
80 login (const struct utmp *ut)
82 #ifdef PATH_MAX
83 char _tty[PATH_MAX + UT_LINESIZE];
84 #else
85 char _tty[512 + UT_LINESIZE];
86 #endif
87 char *tty = _tty;
88 int found_tty;
89 const char *ttyp;
90 struct utmp copy = *ut;
91 struct utmp utbuf;
93 /* Fill in those fields we supply. */
94 #if _HAVE_UT_TYPE - 0
95 copy.ut_type = USER_PROCESS;
96 #endif
97 #if _HAVE_UT_PID - 0
98 copy.ut_pid = getpid ();
99 #endif
101 /* Seek tty. */
102 found_tty = tty_name (STDIN_FILENO, &tty, sizeof (_tty));
103 if (found_tty < 0)
104 found_tty = tty_name (STDOUT_FILENO, &tty, sizeof (_tty));
105 if (found_tty < 0)
106 found_tty = tty_name (STDERR_FILENO, &tty, sizeof (_tty));
108 if (found_tty >= 0)
110 /* We only want to insert the name of the tty without path. */
111 ttyp = basename (tty);
113 /* Position to record for this tty. */
114 strncpy (copy.ut_line, ttyp, UT_LINESIZE);
116 /* Tell that we want to use the UTMP file. */
117 if (utmpname (_PATH_UTMP) != 0)
119 struct utmp *old;
121 /* Open UTMP file. */
122 setutent ();
124 /* Read the record. */
125 getutline_r (&copy, &utbuf, &old);
127 /* Write the entry. */
128 pututline (&copy);
130 /* Close UTMP file. */
131 endutent ();
134 if (tty != _tty)
135 free (tty); /* Free buffer malloced by tty_name. */
138 /* Update the WTMP file. Here we have to add a new entry. */
139 updwtmp (_PATH_WTMP, &copy);