| 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 | |
| 8 | char *ttyname(); |
| 9 | char *getttys(); |
| 10 | char *rindex(); |
| 11 | static char ttys[] = "/etc/ttys"; |
| 12 | |
| 13 | #define NULL 0 |
| 14 | |
| 15 | ttyslot() |
| 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 | |
| 40 | static char * |
| 41 | getttys(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 | } |