xref: /titanic_50/usr/src/lib/libbc/libc/gen/common/ttyslot.c (revision 554ff184129088135ad2643c1c9832174a17be88)
1 /*
2  * Copyright 1991 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from UCB 5.2 3/9/86 */
7 
8 /*
9  * Copyright (c) 1984 Regents of the University of California.
10  * All rights reserved.  The Berkeley software License Agreement
11  * specifies the terms and conditions for redistribution.
12  */
13 
14 /*
15  * Return the number of the slot in the utmp file
16  * corresponding to the current user: try for file 0, 1, 2.
17  * To mimic the behavior of getttyent, we loop through utmp
18  * and try to find an entry with a matching line number.
19  * If we don't find one we return the index of the end of
20  * the file, so that the record can be added to the end of
21  * the file.
22  */
23 #include "../../sys/common/compat.h"
24 #include <sys/syscall.h>
25 #include <sys/fcntl.h>
26 #include <stdio.h>
27 
28 char	*ttyname();
29 char	*rindex();
30 
31 #define	NULL	0
32 
33 ttyslot()
34 {
35 	register char *tp, *p;
36 	register s;
37 	int fd;
38 	struct utmpx utx;
39 
40 
41 	if ((tp = ttyname(0)) == NULL &&
42 	    (tp = ttyname(1)) == NULL &&
43 	    (tp = ttyname(2)) == NULL)
44 		return(0);
45 	if ((p = rindex(tp, '/')) == NULL)
46 		p = tp;
47 	else
48 		p++;
49 
50 	if ((fd = _syscall(SYS_open, "/etc/utmpx", O_RDONLY)) == -1) {
51 		perror("ttyslot: open of /etc/utmpx failed:");
52 		return(0);
53 	}
54 
55 	s = 0;
56 	while (_read(fd, &utx, sizeof(struct utmpx)) > 0) {
57 		s++;
58 		if (strncmp(utx.ut_line, p, sizeof(utx.ut_line)) == 0) {
59 			_syscall(SYS_close, fd);
60 			return(s);
61 		}
62 	}
63 	_syscall(SYS_close, fd);
64 	return (s);
65 }
66