标签:des style blog http color io os 使用 ar
第一个版本:
/* 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");}



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");}

标签:des style blog http color io os 使用 ar
原文地址:http://www.cnblogs.com/LinTeX9527/p/3994795.html