#include<stdio.h>
#include<time.h>
#include<utmp.h>
#include<fcntl.h>
#include<unistd.h>
void show_info(struct utmp*);
void show_time(time_t);
void show_host(char*);
int main()
{
struct utmp current_record;
int fd_utmp;
int len_of_utmp;
len_of_utmp=sizeof(current_record);
if((fd_utmp=open(UTMP_FILE,O_RDONLY))==-1){
perror(UTMP_FILE);
exit(1);
}
while(read(fd_utmp,¤t_record,len_of_utmp)==len_of_utmp){
if(current_record.ut_type==USER_PROCESS){
show_info(¤t_record);
}
}
close(fd_utmp);
return 0;
}
void show_info(struct utmp* current_record)
{
printf("%-9.9s",current_record->ut_name);
printf("%-13.13s",current_record->ut_line);
show_time(current_record->ut_time);
show_host(current_record->ut_host);
}
void show_time(time_t long_time)
{
struct tm* tm_time;
tm_time=gmtime(&long_time);
printf("%d-%.2d-%-3.2d",tm_time->tm_year+1900,tm_time->tm_mon+1,tm_time->tm_mday);
printf("%.2d:%-3.2d",tm_time->tm_hour+8,tm_time->tm_min);
}
void show_host(char* ut_host)
{
int len_of_host=strlen(ut_host);
char host[len_of_host+2];
host[0]='(';
int i;
for(i=0;i<=len_of_host;i++){
host[i+1]=ut_host[i];
}
host[i]=')';
printf("%-13.13s\n",host);
}