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