QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1873|回复: 1

关于who命令的c语言实现问题(带缓冲)

[复制链接]
发表于 2007-3-6 11:35:52 | 显示全部楼层 |阅读模式
带缓冲的who2.c 代码如下:

/*
*  we add a buffer area in who2.c.
*/
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

/*#define SHOWHOST*/          //include remote machine on output

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

static char utmpbuf[NRECS * UTSIZE];            //storage
static int  num_recs;                           //num stored
static int  cur_rec;                            //next to go
static int  fd_utmp = -1;                       //read from

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

int utmp_reload()
{
        int amt_read;
           amt_read = read(fd_utmp, utmpbuf, NRECS * UTSIZE);
        num_recs = amt_read/UTSIZE;                        //how many did we get?
           cur_rec = 0;                                       //reset pointer
   
           return num_recs;
}

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]; ]//这只是取得缓冲中的一个(struct utmp *)结构数据吧,
//之后就调用show_info();显示出此条信息了吧,那如何实现依次读取并显示出buff中的所有信息的?感觉是读满一个buff后只显示了
//一条就又去read了,应该不是这样吧,请高手指点,多谢!!!!!
         cur_rec++;
        return recp;
}

void utmp_close()
{
        if(fd_utmp != -1)
                close(fd_utmp);  //don't close if not open
}

//-------------------------------------------------------------------------

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

int main()
{
        struct utmp * utbufp;       //holds pointer to next rec
        struct utmp * utmp_next();  //return pointer to 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;
   
}

/*
* display contents of the utmp struct
*/
void show_info( struct utmp * utbufp)
{
/*
* remove the blank record
*  the entry ut_type in struct utmp indicate the types
*   USER_PROCESS is the auser process
*/
/*        if(utbufp-> ut_type != USER_PROCESS)
        return;*/
   
        printf("% -8.8s", utbufp->ut_name);
           printf(" ");
           printf("% -12.12s", utbufp->ut_line);
           printf(" ");
           //printf("%101d", utbufp->ut_time);
           showtime(utbufp->ut_time);
           printf(" ");
#ifdef SHOWHOST
     printf("(%s)", utbufp->ut_host);
#endif
           printf("\n");  
}

void showtime(long timeval)
{
/*
* display time
*/
        char *cp;
           cp = ctime(&timeval);
            printf("%12.12s", cp+4);
}
 楼主| 发表于 2007-3-6 11:38:33 | 显示全部楼层
怎么没人回答啊?????
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-4 03:12 , Processed in 0.194814 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表