xref: /original-bsd/lib/libc/gen/ttyslot.c (revision c35f7ea3)
1 /* @(#)ttyslot.c	4.1 (Berkeley) 12/21/80 */
2 /*
3  * Return the number of the slot in the utmp file
4  * corresponding to the current user: try for file 0, 1, 2.
5  * Definition is the line number in the /etc/ttys file.
6  */
7 
8 
9 char	*ttyname();
10 char	*getttys();
11 char	*rindex();
12 static	char	ttys[]	= "/etc/ttys";
13 
14 #define	NULL	0
15 
16 ttyslot()
17 {
18 	register char *tp, *p;
19 	register s, tf;
20 
21 	if ((tp=ttyname(0))==NULL && (tp=ttyname(1))==NULL && (tp=ttyname(2))==NULL)
22 		return(0);
23 	if ((p = rindex(tp, '/')) == NULL)
24 		p = tp;
25 	else
26 		p++;
27 	if ((tf=open(ttys, 0)) < 0)
28 		return(0);
29 	s = 0;
30 	while (tp = getttys(tf)) {
31 		s++;
32 		if (strcmp(p, tp)==0) {
33 			close(tf);
34 			return(s);
35 		}
36 	}
37 	close(tf);
38 	return(0);
39 }
40 
41 static char *
42 getttys(f)
43 {
44 	static char line[32];
45 	register char *lp;
46 
47 	lp = line;
48 	for (;;) {
49 		if (read(f, lp, 1) != 1)
50 			return(NULL);
51 		if (*lp =='\n') {
52 			*lp = '\0';
53 			return(line+2);
54 		}
55 		if (lp >= &line[32])
56 			return(line+2);
57 		lp++;
58 	}
59 }
60