防who命令

#include <stdio.h>
#include <stdlib.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>

#define SHOWHOST

void showtime(long);                /*时间秒数转换成用户可读的数*/
void show_info(struct utmp *);      /*打印*/

int main()
{
    struct utmp utbuf;      /*用户数据结构*/
    int utmpfd;             /*文件描述符*/
    
    if((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1){     /*UTMP_FILE 在 utmp.h 里*/
        perror(UTMP_FILE);
        exit(1);
    }
    
    while(read(utmpfd, &utbuf, sizeof(utbuf)) == sizeof(utbuf)){
        show_info(&utbuf);
    }
    
    close(utmpfd);
    return 0;
}

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);              /*打印时间*/
    
    #ifdef SHOWHOST
        if(utbufp->ut_host[0] != '\0'){
            printf("(%s)", utbufp->ut_host);
        }
    #endif
    printf("\n");
}

void showtime(long timeval)     /*时间转换*/
{
    char *cp;
    cp = ctime(&timeval);
    printf("%12.12s", cp+4);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容