Annotation of 40BSD/cmd/who.c, revision 1.1

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

unix.superglobalmegacorp.com

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