who命令

作者: 荷叶的莲藕 | 来源:发表于2019-01-25 19:34 被阅读0次
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

#define SHOWHOST

int main()
{
    struct utmp current_record;
    int     utmpfd;
    int     reclen = sizeof(current_record);
    
    if( ( utmpfd = open(UTMP_FILE,O_RDONLY ) ) == -1){
        perror(UTMP_FILE);
        exit(1);
    }
    
    while( read ( utmpfd,&current_record,reclen ) == reclen)
        show_info(&current_record);
    close(utmpfd);
    return 0;
}
show_info( struct utmp * utbufp)
{
    //printf("%u \n",utbufp ->ut_type);
    if ( utbufp ->ut_type != USER_PROCESS)
        return;
    printf("% -4.4s", utbufp->ut_name);
    printf(" ");
    printf(" % -8.8s" , utbufp ->ut_line);
    printf(" ");
    //printf(" % 50d", utbufp ->ut_time);
    showtime( utbufp->ut_time );
    printf(" ");
    #ifdef SHOWHOST
    printf( "( %s)", utbufp->ut_host);
    #endif
    printf( "\n") ;
}
showtime(long timeval)
{
    char * cp;
    cp = ctime(&timeval);
    printf("%12.12s",cp+4);
}

相关文章

网友评论

      本文标题:who命令

      本文链接:https://www.haomeiwen.com/subject/slpvjqtx.html