【发布时间】:2013-12-25 00:43:09
【问题描述】:
在使用 wtmp 时,我能够获取用户登录的 tty、登录时间和更多信息。但是,我希望能够知道用户在网上花费了多少时间。有没有办法获取注销时间?
谢谢,
#define WTMP "/var/log/wtmp"
#define K 1024
int main()
{
struct utmp w;
FILE *fd;
time_t t;
struct tm *timeinfo;
char buffer[K];
fd = fopen(WTMP, "r");
if(fd == NULL) {
fprintf(stderr, "cannot open %s\n", WTMP);
exit(0);
}
while(fread(&w, sizeof(struct utmp), 1, fd) == 1) {
if(w.ut_type == USER_PROCESS) {
if( strcmp(w.ut_user, "user1") == 0) {
/* get time in seconds, convert to struct tm, make printable formate*/
t = w.ut_tv.tv_sec;
timeinfo = localtime (&t);
strftime (buffer, K, "%a %b %e %R (EST)", timeinfo);
printf("time: %s Login tty: %s\n", buffer, w.ut_line);
}
}
}
fclose(fd);
}
【问题讨论】: