2410linux 发表于 2007-3-6 11:35:52

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

带缓冲的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;            //storage
static intnum_recs;                           //num stored
static intcur_rec;                            //next to go
static intfd_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; ]//这只是取得缓冲中的一个(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);
}

2410linux 发表于 2007-3-6 11:38:33

:cry: 怎么没人回答啊?????
页: [1]
查看完整版本: 关于who命令的c语言实现问题(带缓冲)