1 /* @(#)ttyslot.c 4.2 (Berkeley) 04/03/84 */ 2 3 /* 4 * Return the number of the slot in the utmp file 5 * corresponding to the current user: try for file 0, 1, 2. 6 * Definition is the line number in the /etc/ttys file. 7 */ 8 #include <sys/file.h> 9 10 char *ttyname(); 11 char *getttys(); 12 char *rindex(); 13 static char ttys[] = "/etc/ttys"; 14 15 #define NULL 0 16 17 ttyslot() 18 { 19 register char *tp, *p; 20 register s, tf; 21 22 if ((tp = ttyname(0)) == NULL && 23 (tp = ttyname(1)) == NULL && 24 (tp = ttyname(2)) == NULL) 25 return(0); 26 if ((p = rindex(tp, '/')) == NULL) 27 p = tp; 28 else 29 p++; 30 if ((tf = open(ttys, O_RDONLY)) < 0) 31 return (0); 32 s = 0; 33 while (tp = getttys(tf)) { 34 s++; 35 if (strcmp(p, tp) == 0) { 36 close(tf); 37 return (s); 38 } 39 } 40 close(tf); 41 return (0); 42 } 43 44 #define BUFSIZ 1024 45 46 static char * 47 getttys(f) 48 { 49 static char buf[BUFSIZ + 1], *next = &buf[BUFSIZ + 1]; 50 register char *lp; 51 char *start; 52 53 for (;;) { 54 if (next >= &buf[BUFSIZ]) { 55 int n = read(f, buf, BUFSIZ); 56 57 if (n <= 0) 58 return (NULL); 59 buf[n] = '\0'; 60 next = &buf[0]; 61 } 62 for (lp = next; *lp && *lp != '\n'; lp++) 63 ; 64 if (*lp == '\n') { 65 *lp++ = '\0'; 66 start = next; 67 next = lp; 68 return (start + 2); 69 } 70 lseek(f, next - lp, L_INCR); 71 next = lp; 72 } 73 } 74