Annotation of 42BSD/bin/who.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char *sccsid = "@(#)who.c       4.6 (Berkeley) 8/19/83";
        !             3: #endif
        !             4: /*
        !             5:  * who
        !             6:  */
        !             7: 
        !             8: #include <stdio.h>
        !             9: #include <utmp.h>
        !            10: #include <pwd.h>
        !            11: #include <ctype.h>
        !            12: 
        !            13: #define NMAX sizeof(utmp.ut_name)
        !            14: #define LMAX sizeof(utmp.ut_line)
        !            15: #define        HMAX sizeof(utmp.ut_host)
        !            16: 
        !            17: struct utmp utmp;
        !            18: struct passwd *pw;
        !            19: struct passwd *getpwuid();
        !            20: char   hostname[32];
        !            21: 
        !            22: char   *ttyname(), *rindex(), *ctime(), *strcpy();
        !            23: 
        !            24: main(argc, argv)
        !            25:        int argc;
        !            26:        char **argv;
        !            27: {
        !            28:        register char *tp, *s;
        !            29:        register FILE *fi;
        !            30:        extern char _sobuf[];
        !            31: 
        !            32:        setbuf(stdout, _sobuf);
        !            33:        s = "/etc/utmp";
        !            34:        if(argc == 2)
        !            35:                s = argv[1];
        !            36:        if (argc == 3) {
        !            37:                tp = ttyname(0);
        !            38:                if (tp)
        !            39:                        tp = rindex(tp, '/') + 1;
        !            40:                else {  /* no tty - use best guess from passwd file */
        !            41:                        pw = getpwuid(getuid());
        !            42:                        strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
        !            43:                        strcpy(utmp.ut_line, "tty??");
        !            44:                        time(&utmp.ut_time);
        !            45:                        putline();
        !            46:                        exit(0);
        !            47:                }
        !            48:        }
        !            49:        if ((fi = fopen(s, "r")) == NULL) {
        !            50:                puts("who: cannot open utmp");
        !            51:                exit(1);
        !            52:        }
        !            53:        while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
        !            54:                if (argc == 3) {
        !            55:                        gethostname(hostname, sizeof (hostname));
        !            56:                        if (strcmp(utmp.ut_line, tp))
        !            57:                                continue;
        !            58:                        printf("%s!", hostname);
        !            59:                        putline();
        !            60:                        exit(0);
        !            61:                }
        !            62:                if (utmp.ut_name[0] == '\0' && argc == 1)
        !            63:                        continue;
        !            64:                putline();
        !            65:        }
        !            66: }
        !            67: 
        !            68: putline()
        !            69: {
        !            70:        register char *cbuf;
        !            71: 
        !            72:        printf("%-*.*s %-*.*s",
        !            73:                NMAX, NMAX, utmp.ut_name,
        !            74:                LMAX, LMAX, utmp.ut_line);
        !            75:        cbuf = ctime(&utmp.ut_time);
        !            76:        printf("%.12s", cbuf+4);
        !            77:        if (utmp.ut_host[0])
        !            78:                printf("\t(%.*s)", HMAX, utmp.ut_host);
        !            79:        putchar('\n');
        !            80: }

unix.superglobalmegacorp.com

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