#include <stdio.h>
#include <stdlib.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#define SHOWHOST
/* 引入内核缓冲技术提高读写效率*/
/* 从/etc/utmp中读取信息*/
int main(){
/*read info into here */
/*记录会读入到这里*/
struct utmp *utbufp,*utmp_next();
if(utmp_open(UTMP_FILE)==-1)
{
/*UTMP_FILE IS IN UTMP.H*/
perror(UTMP_FILE);
exit(1);
}
/*从utmpfd所指定的文件中读取一条纪录长度的记录到数据缓冲结构current中*/
/*如果读取的长度小于一条记录的长度,说明读取结束,已经到达文件末尾*/
while((utbufp=utmp_next())!=((struct utmp *)NULL)){
/*读取成功后,则向屏幕(标准输出)输出该条记录的信息*/
show_info(utbufp);
}
/*读取完成后关闭文件*/
utmp_close();
return 0;
}
/*显示每条记录中的各项信息*/
show_info(struct utmp *utbufp){
/*如果该条记录的类型不是用户类型则返回*/
/*因为我们真的只想输出登陆用户信息*/
if(utbufp->ut_type != USER_PROCESS)
{
return ;
}
/*如果是登陆用户*/
/*输出该记录的用户名ut_user*/
printf("%s",utbufp->ut_user);
printf(" ");
/*输出该记录的登陆终端类型ut_line*/
printf("%s",utbufp->ut_line);
printf(" ");
/*
printf("%10ld",utbufp->ut_time);
*/
/*通过处理后的显示时间方式ut_time*/
showtime(utbufp->ut_time);
printf(" ");
#ifdef SHOWHOST
/*输出用户的主机信息ut_host*/
if(utbufp->ut_host[0]!='\0')
printf("(%s)",utbufp->ut_host);
#endif
printf("\n");
}
/*显示时间*/
showtime(long timeval){
char *cp;
/*将time_t类型的时间转换成字符串形式*/
cp = ctime(&timeval);
/*显示字符串时间*/
printf("%12.12s",cp+4);
}
Shell命令编写——查看当前用户who
最新推荐文章于 2024-05-28 00:48:51 发布