who命令

#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);
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容