|
沙发

楼主 |
发表于 2008-11-16 23:16:56
|
只看该作者
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, ¤t_record, reclen) == reclen )
// {
// if ((¤t_record)->ut_type == USER_PROCESS){
show_info(¤t_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);
} |
|