Annotation of researchv10no/libc/gen/ttyname.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * on success:
                      3:  *     returns the pathname ("/dev/...") of the terminal
                      4:  *     with file descriptor "fd".
                      5:  *     bug: returns pointer to static area.
                      6:  * on failure:
                      7:  *     returns 0.
                      8:  */
                      9: 
                     10: #include <sys/types.h>
                     11: #include <ndir.h>
                     12: #include <sys/stat.h>
                     13: #include <string.h>
                     14: 
                     15: #define        ARB     64      /* arbitrary and not checked safely */
                     16: 
                     17: char   *_ttyname();
                     18: static char ttybuf[ARB];
                     19: 
                     20: char *
                     21: ttyname(fd)
                     22:        int fd;
                     23: {
                     24:        return(_ttyname(&ttybuf[0], fd));
                     25: }
                     26: 
                     27: /*
                     28:  * on success:
                     29:  *     stores at "s" the pathname of the terminal with file descriptor fd,
                     30:  *     and returns "s".
                     31:  * on failure:
                     32:  *     leaves "s" unchanged,
                     33:  *     and returns 0.
                     34:  */
                     35: 
                     36: static char *ttymatch();
                     37: 
                     38: char *
                     39: _ttyname(s, fd)
                     40:        char *s;
                     41:        int fd;
                     42: {
                     43:        int ff;
                     44:        register char *p, *q;
                     45:        register int n;
                     46:        struct stat fstb;
                     47:        char buf[ARB+1];
                     48: 
                     49:        if (fstat(fd, &fstb) < 0)
                     50:                return(0);
                     51:        if ((ff = open("/lib/ttydevs", 0)) < 0)
                     52:                return (ttymatch(&fstb, "/dev", s));
                     53:        while ((n = read(ff, buf, ARB)) > 0) {
                     54:                buf[n] = 0;
                     55:                for (q = buf; p = strchr(q, '\n'); q = p) {
                     56:                        *p++ = 0;
                     57:                        if ((q = ttymatch(&fstb, q, s)) != NULL) {
                     58:                                close(ff);
                     59:                                return (q);
                     60:                        }
                     61:                }
                     62:                if (q < &buf[n])
                     63:                        memcpy(buf, q, n - (q - buf));
                     64:        }
                     65:        close(ff);
                     66:        return (NULL);
                     67: }
                     68: 
                     69: static char *
                     70: ttymatch(stp, dp, s)
                     71: register struct stat *stp;
                     72: char *dp;
                     73: char *s;
                     74: {
                     75:        struct stat tsb;
                     76:        DIR *dfp;
                     77:        register struct direct *dir;
                     78:        char tmps[ARB];
                     79: 
                     80:        if ((dfp = opendir(dp)) == NULL)
                     81:                return (NULL);
                     82:        while ((dir = readdir(dfp)) != NULL) {
                     83:                if (dir->d_ino != stp->st_ino)
                     84:                        continue;
                     85:                strcpy(tmps, dp);
                     86:                strcat(tmps, "/");
                     87:                strcat(tmps, dir->d_name);
                     88:                if (stat(tmps, &tsb) < 0)
                     89:                        continue;
                     90:                if (stp->st_dev != tsb.st_dev || stp->st_ino != tsb.st_ino)
                     91:                        continue;
                     92:                closedir(dfp);
                     93:                strcpy(s, tmps);
                     94:                return(s);
                     95:        }
                     96:        closedir(dfp);
                     97:        return(0);
                     98: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.