|
|
1.1 root 1: #include <stdio.h>
2: #include "stddefs.h"
3: #include <sys/types.h>
4: #include <sys/stat.h>
5: #include <utmp.h>
6:
7: /* Returns a pointer to a character string containing the tty name of a
8: * logged-on user. If the user is logged on more than once, the tty
9: * returned is the one that has been used most recently. If the user is
10: * not presently logged on, the null string is returned. Each call to
11: * findtty returns a pointer to the same data area, so the string must
12: * be copied if it is to be used again.
13: *
14: * NOTE: if this code is compiled with the "-DMAIN" option, then a main
15: * program will be produced, instead of a function. The main program
16: * takes one user name as argument and outputs the ttyname on standard
17: * output.
18: *
19: * J. Leth, IH 6E-318, x6133.
20: */
21:
22: #define NAMELEN 8
23: /* Max. number of characters in a login name */
24: #define TTYLEN 8
25: /* Max. number of characters in a tty name */
26:
27: #ifndef MAIN
28: char *
29: findtty(user)
30: char *user; /* user login name */
31: {
32:
33: #else
34: main(argc, argv)
35: int argc;
36: char *argv[];
37: {
38: char *user;
39: #endif
40:
41: extern long ftime();
42: extern int strcmp();
43:
44: FILE *f;
45: struct utmp buf;
46: long ttytime = 0L;
47: /* Holds latest access time found for requested user */
48: static char ttyname[TTYLEN+1];
49: /* Holds name of tty with that access time */
50: long acc_time = 0L;
51: /* Access time of tty currently being considered */
52: char *utmp = "/etc/utmp";
53:
54: if((f=fopen(utmp,"r"))==NULL) {
55: errexit("findtty: can't open %s file.\n", utmp);
56: }
57:
58: ttyname[0] = NUL;
59:
60: #ifdef MAIN
61: if (argc != 2) {
62: usage("Usage:\tfindtty <login-id>\n\
63: Outputs the name of the most recently-used terminal on which\n\
64: user <login-id> is currently logged in.\n");
65: }
66:
67: user = argv[1];
68: #endif
69:
70: while(fread(&buf,sizeof(buf),1,f) == 1) {
71: /* Search utmp file for logins of the designated user.
72: * For each one found, see if the access time is later
73: * than the latest one so far; if it is, remember the
74: * ttyname and ttytime.
75: */
76:
77: if(strncmp(buf.ut_name, user, NAMELEN) != 0) continue;
78:
79: if((acc_time=ftime(buf.ut_line)) > ttytime) {
80: ttytime = acc_time;
81: strncpy(ttyname, buf.ut_line, TTYLEN);
82: ttyname[TTYLEN] = '\0';
83: /* just to be sure string is terminated */
84: }
85: }
86: #ifndef MAIN
87: return(ttyname);
88: #else
89: printf("%s\n", ttyname);
90: #endif
91: }
92:
93: long
94: ftime(s)
95: char *s;
96: {
97: /* Returns the time of last access for the named device.
98: * The name given as taken as the last component of the device
99: * pathname. The first component, "/dev/" is supplied by this
100: * routine. If the device doesn't exist, -1L is returned.
101: */
102:
103: char dev[20];
104: struct stat statb;
105:
106: strcpy(dev,"/dev/");
107: strcat(dev,s);
108: if( stat(dev,&statb)<0 )
109: return(-1L);
110: return( statb.st_atime );
111: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.