Annotation of researchv9/jerq/src/sysmon/utmp.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <utmp.h>
        !             3: 
        !             4: /* imported */
        !             5: extern char *getlogin();
        !             6: extern char *cttyname();
        !             7: extern char *strncpy();
        !             8: extern int strncmp();
        !             9: extern long lseek(), time();
        !            10: 
        !            11: /* global */
        !            12: static struct utmp utmp;
        !            13: static struct utmp empty;
        !            14: static int slot;
        !            15: 
        !            16: /* predeclared */
        !            17: static int utmpslot();
        !            18: static int chutmp();
        !            19: 
        !            20: /* get the information for a utmp entry */
        !            21: extern int
        !            22: getutmp()
        !            23: {
        !            24:        char *getlogin(), *cttyname();
        !            25:        char *logname, *ttyname;
        !            26: 
        !            27:        /* get tty name */
        !            28:        if ((ttyname = cttyname()) == NULL)
        !            29:                return -1;
        !            30:        strncpy (utmp.ut_line, ttyname+5, sizeof(utmp.ut_line));
        !            31: 
        !            32:        /* get user name */
        !            33:        if ((logname = getlogin()) == NULL)
        !            34:                return -1;
        !            35:        strncpy (utmp.ut_name, logname, sizeof(utmp.ut_name));
        !            36:        return 0;
        !            37: }
        !            38: 
        !            39: /* put the entry into utmp */
        !            40: extern int
        !            41: setutmp()
        !            42: {
        !            43:        slot = ttyslot(utmp.ut_line);
        !            44:        if (slot <= 0)
        !            45:                return -1;
        !            46:        return chutmp (slot, &utmp);
        !            47: }
        !            48: 
        !            49: /* take the entry out of utmp */
        !            50: extern void
        !            51: resetutmp()
        !            52: {
        !            53:        if (slot > 0) {
        !            54:                strncpy(utmp.ut_name, empty.ut_name, sizeof(utmp.ut_name));
        !            55:                chutmp (slot, &utmp);
        !            56:        }
        !            57: }
        !            58: 
        !            59: static int
        !            60: chutmp(slot, up)
        !            61:        int slot;
        !            62:        struct utmp *up;
        !            63: {
        !            64:        int fd;
        !            65: 
        !            66:        if ((fd = open("/etc/utmp", 2)) < 0)
        !            67:                return -1;
        !            68:        if (lseek(fd, (long)(slot*sizeof(utmp)), 0) < 0) {
        !            69:                close(fd);
        !            70:                return -1;
        !            71:        }
        !            72:        up->ut_time = time((long *)0);
        !            73:        if (write(fd, (char *)up, sizeof(struct utmp)) != sizeof(struct utmp)) {
        !            74:                close(fd);
        !            75:                return -1;
        !            76:        } else {
        !            77:                close(fd);
        !            78:                return 0;
        !            79:        }
        !            80: }

unix.superglobalmegacorp.com

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