Annotation of researchv9/cmd/wall.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *
        !             3:  * This program is not related to David Wall, whose Stanford Ph.D. thesis
        !             4:  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
        !             5:  */
        !             6: 
        !             7: #include <stdio.h>
        !             8: #include <utmp.h>
        !             9: #include <time.h>
        !            10: #include <sys/utsname.h>
        !            11: #include <signal.h>
        !            12: #include <sys/types.h>
        !            13: #include <sys/stat.h>
        !            14: 
        !            15: char   *strcpy(), *strncpy();
        !            16: char   *strcat(), *strncat();
        !            17: long   time();
        !            18: struct tm *localtime();
        !            19: 
        !            20: main()
        !            21: {
        !            22:        register int c;
        !            23:        register char *p;
        !            24:        struct utmp utmp;
        !            25:        struct tm *tm;
        !            26:        long clock;
        !            27:        FILE *f;
        !            28:        char *who, *mytty;
        !            29:        struct utsname node;
        !            30:        char mesg[3000];
        !            31:        char *getlogin(), *ttyname();
        !            32:        long time();
        !            33:        struct tm *localtime();
        !            34: 
        !            35:        if((f = fopen("/etc/utmp", "r")) == NULL) {
        !            36:                fprintf(stderr, "Cannot open /etc/utmp\n");
        !            37:                exit(1);
        !            38:        }
        !            39:        if ((who = getlogin()) == NULL)
        !            40:                who = "greg";
        !            41:        if ((mytty = ttyname(2)) == NULL)
        !            42:                mytty = "???";
        !            43:        else if (strncmp(mytty, "/dev/", 5) == 0)
        !            44:                mytty += 5;             /* for prettiness */
        !            45:        uname(&node);
        !            46:        clock = time((long *)0);
        !            47:        tm = localtime(&clock);
        !            48:        sprintf(mesg,  "\nBroadcast Message from %s!%s (%s) at %d:%02d ...\r\n",
        !            49:                node.nodename, who, mytty, tm->tm_hour, tm->tm_min);
        !            50:        p = &mesg[strlen(mesg)];
        !            51:        while((c = getchar()) != EOF) {
        !            52:                if (p >= &mesg[sizeof(mesg)]) {
        !            53:                        fprintf(stderr, "Message too long\n");
        !            54:                        exit(1);
        !            55:                }
        !            56:                if (c == '\n')
        !            57:                        *p++ = '\r';
        !            58:                *p++ = c;
        !            59:        }
        !            60:        while (fread((char *)&utmp, sizeof(utmp), 1, f) == 1) {
        !            61:                if (utmp.ut_name[0] == 0)
        !            62:                        continue;
        !            63:                /* reject people not really logged in */
        !            64:                for (c = 0; c < sizeof(utmp.ut_name); c++)
        !            65:                        if (utmp.ut_name[c] == '*')
        !            66:                                break;
        !            67:                if (c < sizeof(utmp.ut_name))
        !            68:                        continue;
        !            69:                sendmes(utmp.ut_line, sizeof(utmp.ut_line), mesg, p - mesg);
        !            70:        }
        !            71:        exit(0);
        !            72: }
        !            73: 
        !            74: sendmes(tty, len, mesg, mlen)
        !            75: char *tty, *mesg;
        !            76: int len, mlen;
        !            77: {
        !            78:        int i;
        !            79:        char t[50];
        !            80:        int fd;
        !            81: 
        !            82:        while ((i = fork()) == -1)
        !            83:                if (wait((int *)0) == -1) {
        !            84:                        fprintf(stderr, "Try again\n");
        !            85:                        return;
        !            86:                }
        !            87:        if(i)
        !            88:                return;
        !            89:        sprintf(t, "/dev/%.*s", len, tty);
        !            90:        if (ckmode(t) == 0)
        !            91:                exit(0);
        !            92:        signal(SIGALRM, SIG_DFL);       /* blow away if open hangs */
        !            93:        alarm(10);
        !            94:        if((fd = open(t, 1)) < 0) {
        !            95:                fprintf(stderr,"cannot open %s\n", t);
        !            96:                exit(1);
        !            97:        }
        !            98:        write(fd, mesg, mlen);
        !            99:        close(fd);
        !           100: 
        !           101:        /*
        !           102:         * Bitchin'.
        !           103:         */
        !           104: 
        !           105:        exit(0);
        !           106: }
        !           107: 
        !           108: /*
        !           109:  * don't write on ttys whose mode is 0
        !           110:  * hack to prevent hanging ndcon
        !           111:  */
        !           112: 
        !           113: ckmode(tty)
        !           114: char *tty;
        !           115: {
        !           116:        struct stat sb;
        !           117: 
        !           118:        if (stat(tty, &sb) < 0)
        !           119:                return (1);     /* eh? */
        !           120:        if (sb.st_mode & ~S_IFMT)
        !           121:                return (1);
        !           122:        return (0);
        !           123: }

unix.superglobalmegacorp.com

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