第一个版本:![]()
![]()
![]()
![]()
/* who1.c - a first version of the who program* open, read UTMP file, and show results.*/#include <stdio.h>#include <utmp.h>#include <fcntl.h>#include <utmp.h>#include <stdlib.h>#include <unistd.h>#define SHOWHOST /* include remote machine on output */void show_info( struct utmp *utbufp );int main(int argc, char *argv[]){struct utmp current_record; /* read info into here */int utmpfd; /* read from this file descriptor */int reclen = sizeof(current_record);if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){perror( UTMP_FILE ); /* UTMP_FILE is in utmp.h */exit(1);}while( read(utmpfd, ¤t_record, reclen) == reclen){show_info(¤t_record);}close(utmpfd);return 0;}/* show_info()* displays contents of the utmp struct in human readable form.* *note* these sizes should not be hardwird.*/void show_info( struct utmp *utbufp ){printf("%-8.8s", utbufp->ut_name); /* the logname */printf("\t");printf("%-8.8s", utbufp->ut_line); /* the tty */printf("\t");printf("%-10ld", utbufp->ut_time); /* login time */printf("\t");#ifdef SHOWHOSTprintf("(%s)", utbufp->ut_host); /* the host */#endifprintf("\n");}
第一个版本的效果如下:

总结:能够分栏按照格式显示4栏用户信息,但是不能区分非实际用户,不能纠正时间显示问题。
解决方法:
实际用户------在 "/usr/include /bits/utmp.h"中有相关的宏定义,能够用此来区分实际用户。

在显示信息函数 show_info() 中如此修改:

纠正时间显示问题------转换时间到human readable。
了解到 unix 保存时间为 time_t 类型,是一个type long int time_t。
那么把时间转换为 human readable 的函数式 ctime():
char* ctime(const time_t *timep)
结合我们需要显示的格式,那么正确的使用方法是这样的:
printf("%12.12s",ctime(&t)+4);
最终的源文件如下:
/* who1.c - a first version of the who program* open, read UTMP file, and show results.*/#include <stdio.h>#include <utmp.h>#include <fcntl.h>#include <utmp.h>#include <stdlib.h>#include <unistd.h>#define SHOWHOST /* include remote machine on output */void show_info( struct utmp *utbufp );void show_time(const time_t *timep);int main(int argc, char *argv[]){struct utmp current_record; /* read info into here */int utmpfd; /* read from this file descriptor */int reclen = sizeof(current_record);if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){perror( UTMP_FILE ); /* UTMP_FILE is in utmp.h */exit(1);}while( read(utmpfd, ¤t_record, reclen) == reclen){show_info(¤t_record);}close(utmpfd);return 0;}/* show_time() - transform long time to human readable.*/void show_time(const time_t *timep){printf("%14.14s", ctime(timep) + 4);}/* show_info()* displays contents of the utmp struct in human readable form.* *note* these sizes should not be hardwird.*/void show_info( struct utmp *utbufp ){if (utbufp->ut_type != USER_PROCESS)return;printf("%-8.8s", utbufp->ut_name); /* the logname */printf("\t");printf("%-8.8s", utbufp->ut_line); /* the tty */printf("\t");//printf("%-10ld", utbufp->ut_time); /* login time */show_time(&(utbufp->ut_time));printf("\t");#ifdef SHOWHOSTprintf("(%s)", utbufp->ut_host); /* the host */#endifprintf("\n");}
最终的效果如下:

浙公网安备 33010602011771号