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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 /* Return the result of ttyname in the buffer pointed to by TTY, which should
28 be of length BUF_LEN. If it is too long to fit in this buffer, a
29 sufficiently long buffer is allocated using malloc, and returned in TTY.
30 0 is returned upon success, -1 otherwise. */
32 tty_name (int fd
, char **tty
, size_t buf_len
)
34 int rv
; /* Return value. 0 = success. */
35 char *buf
= *tty
; /* Buffer for ttyname, initially the user's. */
43 rv
= ttyname_r (fd
, buf
, buf_len
);
45 if (rv
< 0 || memchr (buf
, '\0', buf_len
))
46 /* We either got an error, or we succeeded and the returned name fit
50 /* Try again with a longer buffer. */
51 buf_len
+= buf_len
; /* Double it */
54 /* No initial buffer; start out by mallocing one. */
55 buf_len
= 128; /* First time guess. */
58 /* We've already malloced another buffer at least once. */
59 new_buf
= realloc (buf
, buf_len
);
61 new_buf
= malloc (buf_len
);
71 *tty
= buf
; /* Return buffer to the user. */
73 free (buf
); /* Free what we malloced when returning an error. */
79 login (const struct utmp
*ut
)
82 char _tty
[PATH_MAX
+ UT_LINESIZE
];
84 char _tty
[512 + UT_LINESIZE
];
89 struct utmp_data data
= { -1 };
90 struct utmp copy
= *ut
;
92 /* Fill in those fields we supply. */
94 copy
.ut_type
= USER_PROCESS
;
97 copy
.ut_pid
= getpid ();
101 found_tty
= tty_name (STDIN_FILENO
, &tty
, sizeof (_tty
));
103 found_tty
= tty_name (STDOUT_FILENO
, &tty
, sizeof (_tty
));
105 found_tty
= tty_name (STDERR_FILENO
, &tty
, sizeof (_tty
));
109 /* We only want to insert the name of the tty without path. */
110 ttyp
= basename (tty
);
112 /* Position to record for this tty. */
113 strncpy (copy
.ut_line
, ttyp
, UT_LINESIZE
);
115 /* Tell that we want to use the UTMP file. */
116 if (utmpname (_PATH_UTMP
) != 0)
120 /* Open UTMP file. */
123 /* Read the record. */
124 if (getutline_r (©
, &old
, &data
) >= 0)
126 #if _HAVE_UT_TYPE - 0
127 /* We have to fake the old entry because this `login'
128 function does not fit well into the UTMP file
130 old
->ut_type
= copy
.ut_type
;
132 pututline_r (©
, &data
);
134 else if (errno
== ESRCH
)
135 /* We didn't find anything. pututline_r will add UT at the end
136 of the file in this case. */
137 pututline_r (©
, &data
);
139 /* Close UTMP file. */
144 free (tty
); /* Free buffer malloced by tty_name. */
147 /* Update the WTMP file. Here we have to add a new entry. */
148 if (utmpname (_PATH_WTMP
) != 0)
150 /* Open the WTMP file. */
153 /* Position at end of file. */
154 data
.loc_utmp
= lseek (data
.ut_fd
, 0, SEEK_END
);
155 if (data
.loc_utmp
!= -1)
157 #if _HAVE_UT_TYPE - 0
158 /* We have to fake the old entry because this `login'
159 function does not fit well into the UTMP file handling
161 data
.ubuf
.ut_type
= copy
.ut_type
;
163 pututline_r (©
, &data
);
166 /* Close WTMP file. */