1 Star 0 Fork 0

20145221高其 / linux-besti-is-2016-2017-1-20145221

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
who.c 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
20145221高其 提交于 2016-11-17 20:46 . 实现文件复制
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define SHOWHOST
void showtime(long timeval){
char* cp;
cp = ctime(&timeval);
cp[strlen(cp)-1] = '\0';
printf("%s",cp+4);
}
void show_info(struct utmp* utbufp){
if(utbufp->ut_type != USER_PROCESS){
return ;
}
printf("%-8.8s",utbufp->ut_name);
printf(" ");
printf("%-8.8s",utbufp->ut_line);
printf(" ");
showtime(utbufp->ut_time);
printf(" ");
#ifdef SHOWHOST
if(utbufp->ut_host[0] != '\0'){
printf("(%s)",utbufp->ut_host);
}
#endif
printf("\n");
}
int main(){
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);
if((utmpfd = open(UTMP_FILE,O_RDONLY)) == -1){
perror(UTMP_FILE);
return 1;
}
while( read(utmpfd,&current_record,reclen) == reclen ){
show_info(&current_record);
}
close(utmpfd);
return 0;
}
C
1
https://gitee.com/20145221/linux-besti-is-2016-2017-1-20145221.git
git@gitee.com:20145221/linux-besti-is-2016-2017-1-20145221.git
20145221
linux-besti-is-2016-2017-1-20145221
linux-besti-is-2016-2017-1-20145221
master

搜索帮助