#include
#include
#include
#include
#include
void show_info(struct utmp *utbufp);
int main()
{
struct utmp current_record;
int utmp_fd;
int reclen = sizeof(current_record);
utmp_fd = open(UTMP_FILE,O_RDONLY);
if(utmp_fd == -1)
return -1;
while( read(utmp_fd, ¤t_record,reclen) == reclen ){
show_info(¤t_record);
}
close(utmp_fd);
return 0;
}
void show_info(struct utmp *utbufp)
{
if(utbufp->ut_name!=NULL)
{
printf("%-8.8s",utbufp->ut_name);
//printf(" ");
printf("%-8.8s",utbufp->ut_line);
//printf(" ");
printf("%101d",utbufp->ut_time);
//printf(" ");
printf("(%s)",utbufp->ut_host);
//printf(" ");
printf("\n");
}
}
这个代码中存在bug,输出的时候不知道为什么打印的终端的名字会有多的。
但是自己明明就开了两个终端,但是实际打印了四个终端。先记下。后续的研究正在进行中~ing