who.txt

来自「shell编程」· 文本 代码 · 共 51 行

TXT
51
字号
/*who.c*/
      2 #include <stdio.h>
      3 #include <utmp.h>
      4 #include <unistd.h>
      5 #include <fcntl.h>
      6 #include <time.h>
      7 
      8 #define SHOWHOST
      9 void show_info(struct utmp *);
     10 void showtime(long timeval);
     11 int main()
     12 {
     13     struct utmp current_record;
     14     int utmpfd;
     15     int reclen=sizeof(current_record);
     16 
     17     if((utmpfd=open(UTMP_FILE,O_RDONLY))==-1)
     18     {
     19         perror(UTMP_FILE);
     20         exit(1);
     21     }
     22 
     23     while(read(utmpfd,&current_record,reclen)==reclen)
     24         show_info(&current_record);
     25     close(utmpfd);
     26     return 0;
     27 }
     28 
     29 void show_info(struct utmp *utbufp)
	{
     31     //see difinitions for ut_type,只显示注册登陆用户
     32     if(utbufp->ut_type!=USER_PROCESS)
     33         return;
     34     printf("%-8s",utbufp->ut_name);
     35     printf(" ");
     36     printf("%-8s",utbufp->ut_line);
     37     printf(" ");
     38     showtime(utbufp->ut_time);
     39 
     40 #ifdef SHOWHOST
     41     printf("(%s)",utbufp->ut_host);
     42 #endif
     43     printf("\n");
     44 }
     45 
     46 void showtime(long timeval)
     47 {
     48     char *cp;
     49     cp=ctime(&timeval);
     50     printf("%12.12s",cp+4);//show from the fourth char
     51 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?