编写who命令

第一个版本:
  1. /* who1.c - a first version of the who program
  2. * open, read UTMP file, and show results.
  3. */
  4. #include <stdio.h>
  5. #include <utmp.h>
  6. #include <fcntl.h>
  7. #include <utmp.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #define SHOWHOST /* include remote machine on output */
  11. void show_info( struct utmp *utbufp );
  12. int main(int argc, char *argv[])
  13. {
  14. struct utmp current_record; /* read info into here */
  15. int utmpfd; /* read from this file descriptor */
  16. int reclen = sizeof(current_record);
  17. if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 )
  18. {
  19. perror( UTMP_FILE ); /* UTMP_FILE is in utmp.h */
  20. exit(1);
  21. }
  22. while( read(utmpfd, ¤t_record, reclen) == reclen)
  23. {
  24. show_infot_record);
  25. }
  26. close(utmpfd);
  27. return 0;
  28. }
  29. /* show_info()
  30. * displays contents of the utmp struct in human readable form.
  31. * *note* these sizes should not be hardwird.
  32. */
  33. void show_info( struct utmp *utbufp )
  34. {
  35. printf("%-8.8s", utbufp->ut_name); /* the logname */
  36. printf("\t");
  37. printf("%-8.8s", utbufp->ut_line); /* the tty */
  38. printf("\t");
  39. printf("%-10ld", utbufp->ut_time); /* login time */
  40. printf("\t");
  41. #ifdef SHOWHOST
  42. printf("(%s)", utbufp->ut_host); /* the host */
  43. #endif
  44. printf("\n");
  45. }
第一个版本的效果如下:


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

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


纠正时间显示问题------转换时间到human readable。
了解到 unix 保存时间为 time_t 类型,是一个type long int time_t。
那么把时间转换为 human readable 的函数式 ctime():
  1. char* ctime(const time_t *timep)
结合我们需要显示的格式,那么正确的使用方法是这样的:
  1. printf("%12.12s",ctime(&t)+4);
最终的源文件如下:
  1. /* who1.c - a first version of the who program
  2. * open, read UTMP file, and show results.
  3. */
  4. #include <stdio.h>
  5. #include <utmp.h>
  6. #include <fcntl.h>
  7. #include <utmp.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #define SHOWHOST /* include remote machine on output */
  11. void show_info( struct utmp *utbufp );
  12. void show_time(const time_t *timep);
  13. int main(int argc, char *argv[])
  14. {
  15. struct utmp current_record; /* read info into here */
  16. int utmpfd; /* read from this file descriptor */
  17. int reclen = sizeof(current_record);
  18. if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 )
  19. {
  20. perror( UTMP_FILE ); /* UTMP_FILE is in utmp.h */
  21. exit(1);
  22. }
  23. while( read(utmpfd, ¤t_record, reclen) == reclen)
  24. {
  25. show_infot_record);
  26. }
  27. close(utmpfd);
  28. return 0;
  29. }
  30. /* show_time() - transform long time to human readable.
  31. */
  32. void show_time(const time_t *timep)
  33. {
  34. printf("%14.14s", ctime(timep) + 4);
  35. }
  36. /* show_info()
  37. * displays contents of the utmp struct in human readable form.
  38. * *note* these sizes should not be hardwird.
  39. */
  40. void show_info( struct utmp *utbufp )
  41. {
  42. if (utbufp->ut_type != USER_PROCESS)
  43. return;
  44. printf("%-8.8s", utbufp->ut_name); /* the logname */
  45. printf("\t");
  46. printf("%-8.8s", utbufp->ut_line); /* the tty */
  47. printf("\t");
  48. //printf("%-10ld", utbufp->ut_time); /* login time */
  49. show_time(&(utbufp->ut_time));
  50. printf("\t");
  51. #ifdef SHOWHOST
  52. printf("(%s)", utbufp->ut_host); /* the host */
  53. #endif
  54. printf("\n");
  55. }

最终的效果如下:





原文地址:https://www.cnblogs.com/LinTeX9527/p/3994795.html