# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include <utmp.h>
# include <time.h>
# include <sys/time.h>
# define SHOWHOST
void show_info(struct utmp *ut_buf);
void show_time(struct timeval *time);
int main(void)
{
struct utmp utbuf;
int ut_fp;
if((ut_fp = open(UTMP_FILE, O_RDONLY)) < 0)
{
perror(UTMP_FILE);
exit(1);
}
while(read(ut_fp, &utbuf, sizeof(utbuf)) == sizeof(utbuf))
{
show_info(&utbuf);
}
close(ut_fp);
return 0;
}
void show_info(struct utmp *ut_buf)
{
if(ut_buf->ut_type != USER_PROCESS)
return;
printf("%s ", ut_buf->ut_user);
printf("%8.8s ", ut_buf->ut_line);
show_time(&ut_buf->ut_tv);
#ifdef SHOWHOST
if(ut_buf->ut_host[0] != '\0')
printf("(%s)", ut_buf->ut_host);
#endif
printf("\n");
}
void show_time(struct timeval *time) /*in "redhat4.0" test, so the logintime is timeval structure*/
{
char *fp;
fp = ctime(&time->tv_sec);
printf("%12.12s", fp+4);
}
类似who命令程序
最新推荐文章于 2023-06-27 09:30:05 发布