Annotation of researchv8dc/libc/gen/getlogin.c, revision 1.1.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 ((tp = cttyname()) == 0)
                     30:                goto trypasswd;
                     31:        if (strncmp(tp, "/dev/", 5) == 0)
                     32:                tp += 5;
                     33:        if ((ufd = open("/etc/utmp", 0)) < 0)
                     34:                goto trypasswd;
                     35:        while (read(ufd, (char *) &ub, sizeof(ub)) == sizeof(ub)) {
                     36:                if (strncmp(ub.ut_line, tp, sizeof(ub.ut_line)))
                     37:                        continue;
                     38:                strncpy(logbuf, ub.ut_name, sizeof(ub.ut_name));
                     39:                logbuf[sizeof(ub.ut_name)] = '\0';
                     40:                /* begin trim of padded blanks (old systems) */
                     41:                for (tp=logbuf; *tp != 0 && *tp != ' '; tp++)
                     42:                        ;
                     43:                *tp = '\0';
                     44:                /* end trim */
                     45:                break;
                     46:        }
                     47:        close(ufd);
                     48: trypasswd:
                     49:        uid = getuid();
                     50:        if (logbuf[0]) {
                     51:                pwp = getpwnam(logbuf);
                     52:                if (pwp && pwp->pw_uid==uid)
                     53:                        return(logbuf);
                     54:        }
                     55:        if ((pwp = getpwuid(uid)) == 0)
                     56:                return(0);
                     57:        strcpy(logbuf, pwp->pw_name);
                     58:        return(logbuf);
                     59: }

unix.superglobalmegacorp.com

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