getutent 读取utmp和wtmp,获得login account

这个是得到utmp的

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <utmp.h>

int main(int agrc, char* argv[])
{
    struct utmp* entry;
    int i = 0;
    setutent();

    while ((entry = getutent()) != NULL)
    {
        i++;
//        if(entry->ut_type == USER_PROCESS)
//            printf("%d %s %s %s /n",entry->ut_type,entry->ut_user,entry->ut_line,entry->ut_host);
        printf("user: %s, device: %s, remote host: %s, time: %s/n", entry->ut_user,
                entry->ut_line, entry->ut_host, ctime(&entry->ut_tv));
    }

    printf("number of users %d/n", i);
    printf("%s/n", UTMP_FILE);
    endutent();
    return 0;
}

 

下面这个是读取wtmp的

#include <utmp.h>
#include <utmpx.h>
#include <stdio.h>


int main()
{

        struct utmp record;
        FILE *fd = fopen(WTMP_FILENAME, "r");

        if (fd == NULL) {
                perror(WTMP_FILENAME);
                return 1;
        }
        while (fread(&record, sizeof record, 1, fd) != 0) {
                printf("%s: %s: %s /n", record.ut_user, record.ut_line,  record.ut_id);
        }
        return 0;
}

但是具体怎么看,我还不清楚。 比如 怎么区分什么时候登陆啊,什么时候下线啊。

 

你可能感兴趣的:(getutent 读取utmp和wtmp,获得login account)