?? who3.c
字號:
/*utmplib.c - functions to buffer reads from utmp file
*
*functions are
* utmp_open(filename) --openfile
* returns -1 on error
* utmp_next( ) -return pointer to next struct
* returns NULL on eof
* utmp_close( ) - close file
*
* reads NRECS per read then doles them out from the buffer
*/
#include<stdio.h>
#include<utmp.h>
#include<sys/types.h>
#include<fcntl.h>
#define NRECS 16
#define NULLUT ((struct utmp * )NULL)
#define UTSIZE (sizeof(struct utmp)) //zhi ding mei ci du ru de zi jie shu
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
utmp_open(char * filename)
{
fd_utmp=open(filename,O_RDONLY); //open it
cur_rec=num_recs=0; // no recs yet
return fd_utmp; //report
}
struct utmp * utmp_next( )
{
struct utmp * recp ;
if( fd_utmp == -1) //error
return NULLUT ;
if( cur_rec == num_recs && utmp_reload ( ) == 0) //any more
return NULLUT ;
// get address of next record
recp = ( struct utmp * ) & utmpbuf [ cur_rec * UTSIZE ] ;
cur_rec ++ ;
return recp ;
}
int utmp_reload( )
/*
* read next bunch of records into buffer
*/
{
int amt_read; //read them in
amt_read=read(fd_utmp,utmpbuf,NRECS * UTSIZE); // how many did we get ?
num_recs=amt_read/UTSIZE;
cur_rec=0;
return num_recs;
}
utmp_close( )
{
if(fd_utmp!=-1) //don't close if not
close(fd_utmp); //open
}
#include<stdio.h>
#include<time.h>
#include<utmp.h>
#include<fcntl.h>
#define SHOWHOST
void showtime(time_t);
void show_info(struct utmp * );
int main()
{
struct utmp * utbufp; //read info into here
* utmp_next; //returns pointer to next
if( (utmp_open( UTMP_FILE ))==-1)
{
perror(UTMP_FILE);//UTMP_FILE is in utmp.h
exit(1);
}
while((utbufp = utmp_next())!= ((struct utmp * )NULL) )
show_info(utbufp);
utmp_close();
return 0; //went ok
}
/*
* show info()
* displays contents of the utmp struct
* in human readable form
* *displays nothing if record has no user name
*/
void show_info(struct utmp * utbufp)
{
if(utbufp->ut_type!=USER_PROCESS) //users only
return;
printf("% -8.8s",utbufp->ut_name); //the logname
printf(""); // a space
printf("% -8.8s",utbufp->ut_line); //the tty
printf(""); // a space
showtime(utbufp->ut_time); //display time
//printf("% 101d",utbufp->ut_time);//login time
//printf(""); // a space
#ifdef SHOWHOST
if(utbufp->ut_host[0]!='\0')
printf("(% s)",utbufp->ut_host); // the host
#endif
printf("\n"); // newline
}
void showtime(long timeval)
/*
* displays time in a format fit for human consumption
*uses ctime to build a string then packs parts out of it
*Note:%12.12s prints s string 12 chars wide and LIMITS
*it to 12 chars
*/
{
char *cp ;// to hold address of time
cp=ctime(&timeval);//convert time to string
// string looks like
//Mon Feb 4 00:46:40 EST 1991
//0213456789012345.
printf("%12.12s",cp + 4);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -