Annotation of coherent/b/lib/libc/XSTDIO/other/ttyname.c, revision 1.1

1.1     ! root        1: #define        NEWTTYS 1
        !             2: /*
        !             3:  * libc/gen/ttyname.c
        !             4:  * Coherent I/O Library.
        !             5:  * Return the name of the terminal device
        !             6:  * associated with the given file descriptor.
        !             7:  * The names in /etc/ttys define the domain.
        !             8:  */
        !             9: 
        !            10: #include <stdio.h>
        !            11: #include <sys/stat.h>
        !            12: #include <sys/dir.h>
        !            13: 
        !            14: extern char *index();
        !            15: 
        !            16: static char dev_buff[DIRSIZ+8] = "/dev/console";
        !            17: 
        !            18: char *
        !            19: ttyname(fd) int fd;
        !            20: {
        !            21:        if (_ttyslot(fd) >= 0)
        !            22:                return dev_buff;
        !            23:        return NULL;
        !            24: }
        !            25: 
        !            26: /*
        !            27:  * Return the /etc/ttys slot for fd, -1 on error.
        !            28:  * The /dev/... name is left in dev_buff.
        !            29:  * Used by ttyname() and ttyslot().
        !            30:  */
        !            31: int
        !            32: _ttyslot(fd) int fd;
        !            33: {
        !            34:        register FILE *fp;
        !            35:        register short fd_mode;
        !            36:        register dev_t fd_rdev;
        !            37:        register int slot;
        !            38:        struct stat sb;
        !            39: 
        !            40:        if (fstat(fd, &sb) < 0
        !            41:         || (fp = fopen("/etc/ttys", "r")) == NULL)
        !            42:                return -1;
        !            43:        fd_mode = sb.st_mode;
        !            44:        fd_rdev = sb.st_rdev;
        !            45:        for (slot = 0; ; slot += 1) {
        !            46:                if (getc(fp) == '\n'    /* Missing status */
        !            47: #if NEWTTYS
        !            48:                 || getc(fp) == '\n'    /* Missing line type, ie loc or rem */
        !            49: #endif
        !            50:                 || getc(fp) == '\n'    /* Missing getty arg */
        !            51:                 || fgets(dev_buff+5, DIRSIZ, fp) == NULL
        !            52:                 || index(dev_buff, '\n') == NULL) {
        !            53:                        slot = -1;
        !            54:                        break;
        !            55:                }
        !            56:                *index(dev_buff, '\n') = 0;
        !            57:                if (stat(dev_buff, &sb) >= 0
        !            58:                 && sb.st_mode == fd_mode
        !            59:                 && sb.st_rdev == fd_rdev)
        !            60:                        break;
        !            61:        }
        !            62:        fclose(fp);
        !            63:        return slot;
        !            64: }
        !            65: 
        !            66: /* end of libc/gen/ttyname.c */

unix.superglobalmegacorp.com

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