ptg10805159
698 Te rminal I/O Chapter 18
strcmp(pathname, "/dev/stdout") == 0 ||
strcmp(pathname, "/dev/stderr") == 0)
continue;
if (stat(pathname, &devstat) < 0)
continue;
if (S_ISDIR(devstat.st_mode)) {
add(pathname);
continue;
}
if (devstat.st_ino == fdstatp->st_ino &&
devstat.st_dev == fdstatp->st_dev) { /* found a match */
closedir(dp);
return(pathname);
}
}
closedir(dp);
return(NULL);
}
char *
ttyname(int fd)
{
struct stat fdstat;
struct devdir *ddp;
char *rval;
if (isatty(fd) == 0)
return(NULL);
if (fstat(fd, &fdstat) < 0)
return(NULL);
if (S_ISCHR(fdstat.st_mode) == 0)
return(NULL);
rval = searchdir("/dev", &fdstat);
if (rval == NULL) {
for (ddp = head; ddp != NULL; ddp = ddp->d_next)
if ((rval = searchdir(ddp->d_name, &fdstat)) != NULL)
break;
}
cleanup();
return(rval);
}
Figure 18.15Implementation of POSIX.1ttynamefunction
The technique is to read the/devdirectory,looking for an entry with the same
device number and i-node number.Recall from Section 4.24 that each file system has a
unique device number (thest_devfield in thestatstructure, from Section 4.2), and
each directory entry in that file system has a unique i-node number (thest_inofield in
thestatstructure). Weassume in this function that when we hit a matching device