Annotation of researchv9/libc/gen/getlogin.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * on success:
        !             3:  *     returns the login name of the invoker.
        !             4:  *     bug: returns pointer to static area.
        !             5:  * on failure:
        !             6:  *     returns 0.
        !             7:  */
        !             8: 
        !             9: #include <utmp.h>
        !            10: #include <pwd.h>
        !            11: 
        !            12: char   *strcpy();
        !            13: char   *strncpy();
        !            14: char   *cttyname();
        !            15: struct passwd *getpwuid();
        !            16: struct passwd *getpwnam();
        !            17: 
        !            18: char *
        !            19: getlogin()
        !            20: {
        !            21:        register int ufd;
        !            22:        register char *tp;
        !            23:        register struct passwd *pwp;
        !            24:        struct utmp ub;
        !            25:        static char logbuf[sizeof(ub.ut_name)+1];
        !            26:        int uid;
        !            27: 
        !            28:        logbuf[0] = '\0';
        !            29:        if (getlogname(logbuf) >= 0 && logbuf[0] != '\0')
        !            30:                return (logbuf);
        !            31:        if ((tp = cttyname()) == 0)
        !            32:                goto trypasswd;
        !            33:        if (strncmp(tp, "/dev/", 5) == 0)
        !            34:                tp += 5;
        !            35:        if ((ufd = open("/etc/utmp", 0)) < 0)
        !            36:                goto trypasswd;
        !            37:        while (read(ufd, (char *) &ub, sizeof(ub)) == sizeof(ub)) {
        !            38:                if (strncmp(ub.ut_line, tp, sizeof(ub.ut_line)))
        !            39:                        continue;
        !            40:                strncpy(logbuf, ub.ut_name, sizeof(ub.ut_name));
        !            41:                logbuf[sizeof(ub.ut_name)] = '\0';
        !            42:                /* begin trim of padded blanks (old systems) */
        !            43:                for (tp=logbuf; *tp != 0 && *tp != ' '; tp++)
        !            44:                        ;
        !            45:                *tp = '\0';
        !            46:                /* end trim */
        !            47:                break;
        !            48:        }
        !            49:        close(ufd);
        !            50: trypasswd:
        !            51:        uid = getuid();
        !            52:        if (logbuf[0]) {
        !            53:                pwp = getpwnam(logbuf);
        !            54:                if (pwp && pwp->pw_uid==uid)
        !            55:                        return(logbuf);
        !            56:        }
        !            57:        if ((pwp = getpwuid(uid)) == 0)
        !            58:                return(0);
        !            59:        strcpy(logbuf, pwp->pw_name);
        !            60:        return(logbuf);
        !            61: }

unix.superglobalmegacorp.com

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