Annotation of researchv9/libc/gen/ttyname.c, revision 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: char   *_ttyname();
        !            11: static char ttybuf[32];
        !            12: 
        !            13: char *
        !            14: ttyname(fd)
        !            15:        int fd;
        !            16: {
        !            17:        return(_ttyname(&ttybuf[0], fd));
        !            18: }
        !            19: 
        !            20: /*
        !            21:  * on success:
        !            22:  *     stores at "s" the pathname of the terminal with file descriptor fd,
        !            23:  *     and returns "s".
        !            24:  * on failure:
        !            25:  *     leaves "s" unchanged,
        !            26:  *     and returns 0.
        !            27:  */
        !            28: 
        !            29: #include <sys/types.h>
        !            30: #include <sys/dir.h>
        !            31: #include <sys/stat.h>
        !            32: 
        !            33: static char *dirlist[] = {
        !            34:        "/dev/",
        !            35:        "/dev/dk/",
        !            36:        "/dev/pt/",
        !            37:        0
        !            38: };
        !            39: 
        !            40: char   *strcpy();
        !            41: char   *strncat();
        !            42: 
        !            43: char *
        !            44: _ttyname(s, fd)
        !            45:        char *s;
        !            46:        register int fd;
        !            47: {
        !            48:        register char **dpp, *dp;
        !            49:        struct stat fstb, tsb;
        !            50:        struct direct db;
        !            51:        char tmps[32];
        !            52: 
        !            53:        if (fstat(fd, &fstb) < 0)
        !            54:                return(0);
        !            55:        for (dpp = dirlist; dp = *dpp++;) {
        !            56:                if ((fd = open(dp, 0)) < 0)
        !            57:                        continue;
        !            58:                while (read(fd, (char *) &db, sizeof(db)) == sizeof(db)) {
        !            59:                        if (db.d_ino == 0 || db.d_ino != fstb.st_ino)
        !            60:                                continue;
        !            61:                        strcpy(tmps, dp);
        !            62:                        strncat(tmps, db.d_name, sizeof(db.d_name));
        !            63:                        if (stat(tmps, &tsb) < 0)
        !            64:                                continue;
        !            65:                        if (tsb.st_dev != fstb.st_dev || tsb.st_ino != fstb.st_ino)
        !            66:                                continue;
        !            67:                        close(fd);
        !            68:                        strcpy(s, tmps);
        !            69:                        return(s);
        !            70:                }
        !            71:                close(fd);
        !            72:        }
        !            73:        return(0);
        !            74: }

unix.superglobalmegacorp.com

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