#include <stdio.h>
#include <utmp.h>
#include <string.h>
#include <time.h>
int main(void) {
struct utmp *line;
time_t timestamp;
utmpname("/var/log/wtmp");
setutent();
while( (line = getutent()) != NULL) {
if (line->ut_type == USER_PROCESS ) {
timestamp = line->ut_tv.tv_sec;
printf("%s %s %s %s", line->ut_user, line->ut_line,line->ut_host, asctime(localtime(×tamp)));
}
}
endutent();
return 0;
}
编译生成执行命令
gcc -Wall last.c -o last
执行
./last