曲径通幽论坛

标题: who 命令的基本实现 [打印本页]

作者: beyes    时间: 2008-11-16 23:16
标题: who 命令的基本实现
C语言: who 命令的实现   
  #include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

#define SHOWHOST

void show_info( struct utmp *utbufp );

int main(void)
{
    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;
}

void show_info( struct utmp *utbufp )
{
    printf("% -8.8s", utbufp->ut_user);
    printf(" ");
    printf("% -8.8s", utbufp->ut_line);
    printf(" ");
    printf("%d", utbufp->ut_tv);
    printf(" ");

#ifdef SHOWHOST
    printf("(%s))", utbufp->ut_host);
#endif
    printf("\n");
}

此初步的程序中,时间的显示还没有解决。
作者: beyes    时间: 2008-11-16 23:16
标题: who2.c 解决时间显示格式问题和仅显示当前登录用户
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

#define SHOWHOST

void show_info( struct utmp *utbufp );
void showtime( long timeval );

int main(void)
{
    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 )
//    {
//    if ((&current_record)->ut_type == USER_PROCESS){
        show_info(&current_record);
//    }
//    }
    close(utmpfd);
    return 0;
}

void show_info( struct utmp *utbufp )

{
    if( utbufp->ut_type != USER_PROCESS )       //放在这里判断更好,return 直接跳出函数
    return;

    printf("% -8.8s", utbufp->ut_user);
    printf(" ");
    printf("% -8.8s", utbufp->ut_line);
    printf(" ");
    showtime( (utbufp->ut_tv).tv_sec );
    printf(" ");

#ifdef SHOWHOST
    printf("(%s))", utbufp->ut_host);
#endif
    printf("\\n");
}

void showtime( long timeval )
{
    char *cp;
    cp = ctime(&timeval);
   
    printf("%12.12s",cp+4);
}
作者: beyes    时间: 2008-11-16 23:17
标题: 运用缓冲技术对 who 命令的改进
在上面的帖子中,使用 while( read(utmpfd, &current_record, reclen) == reclen )进行循环 read() 的调用,使系统有很大的开销,下面使用缓冲技术改进之。下面是主函数,主函数调用的子函数在另外一个文件 utmplib.c中实现:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <utmp.h>
#include <fcntl.h>
#include "utmplib.h"

#define SHOWHOST

void show_info( struct utmp *utbufp );
void showtime( long timeval );

int main()
{
    struct utmp *utbufp;
    struct utmp *utmp_next();

    if( utmp_open(UTMP_FILE) == -1 ){
    perror(UTMP_FILE);
    exit(1);
    }

    while( ( utbufp = utmp_next() ) != ((struct utmp *)NULL))
    show_info( utbufp);

    utmp_close();

    return 0;
}

void show_info( struct utmp *utbufp )

{
    if( utbufp->ut_type != USER_PROCESS )
    return;

    printf("% -8.8s", utbufp->ut_user);
    printf(" ");
    printf("% -8.8s", utbufp->ut_line);
    printf(" ");
    showtime( (utbufp->ut_tv).tv_sec );
    printf(" ");

#ifdef SHOWHOST
    printf("(%s))", utbufp->ut_host);
#endif
    printf("\\n");
}

void showtime( long timeval )
{
    char *cp;
    cp = ctime(&timeval);
   
    printf("%12.12s",cp+4);
}
作者: beyes    时间: 2008-11-16 23:17
标题: utmplib.c
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <utmp.h>

#define NRECS 16
#define NULLUT ((struct utmp *)NULL)
#define UTSIZE ( sizeof(struct utmp) )

static char utmpbuf[NRECS  * UTSIZE];
static int  num_recs;       /*缓冲区中数据个数*/
static int  cur_rec;       /*缓冲区已被使用的数据的个数*/
static int  fd_utmp = -1;

utmp_open( char *filename )
{
    fd_utmp =  open(filename,O_RDONLY);
   
    cur_rec = num_recs = 0;

    return fd_utmp;
}

struct utmp *utmp_next()
{
    struct utmp *recp;

    if( fd_utmp == -1 )
    return NULLUT;

    if( cur_rec == num_recs && utmp_reload() == 0 )
    return NULLUT;

    recp = ( struct utmp *)&utmpbuf[cur_rec * UTSIZE]; /*定位到下一个记录*/
    cur_rec++;        
    return recp;
}



/*
* 读下一束记录到缓冲区
*/
  int utmp_reload()
    {
    int amt_read;
    amt_read = read(fd_utmp, utmpbuf, NRECS* UTSIZE);
    num_recs = amt_read/UTSIZE;
    cur_rec = 0;
    return num_recs;
    }
/*
*关闭文件
*/

utmp_close()
{
    if( fd_utmp != -1 )
    close( fd_utmp );
}




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2