第一个版本:

  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_info(¤t_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_info(¤t_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. }

最终的效果如下:

编写who命令的更多相关文章

  1. 编写linux 命令行实用工具 shell命令

    今天我想以带着问题的方法学习新的技术. 问题1: 编写一个命令 语法: command  options  path  expressions

  2. Linux系统编程【3.1】——编写ls命令

    ls命令简介 老规矩,直接在终端输入:man ls (有关于man命令的简介可以参考笔者前期博客:Linux系统编程[1]--编写more命令) 可以看到,ls命令的作用是显示目录中的文件名,它带有可 ...

  3. Linux系统编程【2】——编写who命令

    学到的知识点 通过实现who命令,学到了: 1.使用man命令寻找相关信息 2.基于文件编程 3.体会到c库函数与系统调用的不同 4.加深对缓冲技术的理解 who命令的作用 who命令的使用 在控制终 ...

  4. 编写who命令:文件操作,缓冲区与联机帮助

    最近阅读UULP(Understanding Unix/Linux Programming),按照书中介绍对Unix/Linux系统编程进行学习梳理,总结如下. 1. who命令能做什么 who命令用 ...

  5. 编写批处理命令来执行JAVA文件

    http://baobeituping.iteye.com/blog/802543 1. JAVA工程路径结构 Project    ------lib    ------src    ------W ...

  6. 让你如绅士般基于描述编写 Python 命令行工具的开源项目:docopt

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  7. CMakeLists.txt编写常用命令

    目录 1. 设置cmake最小版本 2. 设置项目名称 3. 设置编译目标类型 4. 指定编译包含的源文件 1. 明确指明包含的源文件 2. 搜索指定目录的所有的cpp文件 3. 自定义搜索规则 4. ...

  8. docker Dockerfile文件的编写部分命令

    镜像的构建过程:编写dockerfile文件,执行docker build 进行构建镜像,最后docker run 运行容器镜像实例. docker build -t mycentos:1.01 . ...

  9. Linux系统编程【1】——编写more命令

    背景介绍 笔者知识背景 笔者接触Linux快一年了.理论知识方面:学习了操作系统基础知识,了解进程调度.内存分配.文件管理.磁盘I/O这些基本的概念. 实操方面:会使用Linux简单命令,在嵌入式系统 ...

  10. django “如何”系列2:如何编写django-admin 命令

    应用可以使用manage.py注册自己的动作,例如,你可能想要为你即将发布的应用添加一个manage.py 操作.这节我们将为polls应用添加一个closepoll的命令 添加一个managemen ...

随机推荐

  1. changing permissions of Read-only file system in linux

      up vote 2 down vote favorite 1 i use this command to make a bootable flash disk of linux mint sudo ...

  2. CNN 文本分类模型优化经验——关键点:加卷积层和FC可以提高精度,在FC前加BN可以加快收敛,有时候可以提高精度,FC后加dropout,conv_1d的input维度加大可以提高精度,但是到256会出现OOM。

    network = tflearn.input_data(shape=[None, max_len], name='input') network = tflearn.embedding(networ ...

  3. go语言笔记——是c开发的 lex yacc进行词法和语法分析,go不支持函数和运算符重载,不支持类型继承,也不支持断言,还有泛型

    从 Go 1.0.3 版本开始,不再使用 8g,8l 之类的指令进行程序的构建,取而代之的是统一的 go build 和 go install 等命令,而这些指令会自动调用相关的编译器或链接器. 如果 ...

  4. 怎么看待MYSQL的性能

    MySQL在单实例性能方面和Oracle相比还有一些差距,我们通过规范和技术手段来降低这些性能差距带来的问题. 首先,大量甚至海量数据的增删改.查询.聚合查询的性能还有待提高.为了规避这些问题,我们在 ...

  5. TS流解析 四

    一 从TS流开始 数字电视机顶盒接收到的是一段段的码流,我们称之为TS(Transport Stream,传输流),每个TS流都携带一些信息,如Video.Audio以及我们需要学习的PAT.PMT等 ...

  6. 40. combo的displayField和valueField属性

    转自:https://xsl2007.iteye.com/blog/773464 下拉框combo可以设置displayField和valueField属性,这两个值值相当于Java中的map,一个键 ...

  7. 基于Hive的手机应用信息统计分析系统

    目录 项目概要 具体实现 HIVE查询 项目概要 需求 手机应用日志 定期离线分析手机应用新增用户.活跃用户.沉默用户.启动次数.版本分布和留存用户等业务指标. 工作流程 手机APP启动时,上报启动日 ...

  8. A. Power Consumption Calculation

    http://codeforces.com/problemset/problem/10/A 题很简单,就是题意难懂啊... #include <stdio.h> #include < ...

  9. El Dorado(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=2372 题意:给出n个数,求长度为m的递增子序列的数目. 思路:状态转移方程 dp[i][j] = sum(dp[ ...

  10. go 学习成长之路

    一.go的搭建 二.初识go 三.混个脸熟--go 四.go的语言结构 五.go的常量与变量 六.go基础数据类型 七.go 条件语句 八.go 运算符 九.go条件语句switch 十.go循环语句 ...