who1.c

#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>

#define SHOWHOST /*include remote machine on output*/

show_info(struct utmp *utbufp)
{
printf("% -8.8s", utbufp->ut_name);//the logname
printf(" ");//a space
printf("% -8.8s", utbufp->ut_line);//the tty
printf(" ");//a space
printf("% -10ld", utbufp->ut_time);//login time
printf(" ");//a space
#ifdef SHOWHOST
printf("(%s)", utbufp->ut_host);//the host
#endif
printf("\n");//newline
}
int main()
{
struct utmp current_record; /*read info into here*/
int utmpfd; /*read from this descriptor*/
int reclen = sizeof(current_record);

if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
{
perror(UTMP_FILE);/*UTMP_FILE is utmp.h*/
exit(1);
}

while(read(utmpfd, &current_record, reclen) == reclen)
show_info(&current_record);
close(utmpfd);
return 0;/*went ok*/

}

who2.c

#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
//who2 added
#include <time.h>

#define SHOWHOST /*include remote machine on output*/

void show_info(struct utmp *utbufp);
//who2 added
void showtime(long);

int main()
{
struct utmp current_record; /*read info into here*/
int utmpfd; /*read from this descriptor*/
int reclen = sizeof(current_record);

if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
{
perror(UTMP_FILE);/*UTMP_FILE is utmp.h*/
exit(1);
}

while(read(utmpfd, &current_record, reclen) == reclen)
show_info(&current_record);
close(utmpfd);
return 0;/*went ok*/

}

show_info(struct utmp *utbufp)
{
//who2 added
if (utbufp->ut_type != USER_PROCESS)
{
return;
}
//who2 end
printf("% -8.8s", utbufp->ut_name);//the logname
printf(" ");//a space
printf("% -8.8s", utbufp->ut_line);//the tty
printf(" ");//a space
//printf("% -10ld", utbufp->ut_time);//login time
//printf(" ");//a space
showtime(utbufp->ut_time);
#ifdef SHOWHOST
printf("(%s)", utbufp->ut_host);//the host
#endif
printf("\n");//newline
}

//who2 added
void showtime(long timeval)
{
char *cp;//to hold address of time
cp = ctime(&timeval);
printf("%12.12s", cp+4);//pick 12 chars from pos 4
}
//who2 end

who命令的更多相关文章

  1. Cmder--Windows下命令行利器

    cmder cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 安装包 安装包链接 下载后,直接解压即用. 修改命令提示符λ为 ...

  2. 【每日一linux命令4】常用参数:

     下面所列的是常见的参数(选项)义: --help,-h                              显示帮助信息 --version,-V                        ...

  3. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门

    2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...

  4. MVVM模式解析和在WPF中的实现(三)命令绑定

    MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  5. SQLServer执行命令出现“目录无效的提示”

    异常处理汇总-数据库系列  http://www.cnblogs.com/dunitian/p/4522990.html 一般都是清理垃圾清理过头了,把不该删的目录删了 网上说法: 问题描述: 1.s ...

  6. SQLServer文件收缩-图形化+命令

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 收缩前 图形化演示: 不仅仅可以收缩日记文件,数据库文件也是可以收缩的,只不过日记收缩比 ...

  7. 让 windows 下的命令行程序 cmd.exe 用起来更顺手

    在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...

  8. [版本控制之道] Git 常用的命令总结(欢迎收藏备用)

    坚持每天学习,坚持每天复习,技术永远学不完,自己永远要前进 总结日常开发生产中常用的Git版本控制命令 ------------------------------main-------------- ...

  9. git 命令

    切换仓库地址: git remote set-url origin xxx.git切换分支:git checkout name撤销修改:git checkout -- file删除文件:git rm  ...

  10. svn 常用命令总结

    svn 命令篇 svn pget svn:ignore // 查看忽略项 svn commit -m "提交说明" // 提交修改 svn up(update) // 获取最新版本 ...

随机推荐

  1. 换一种思维看待PHP VS Node.js

    php和javascript都是非常流行的编程语言,刚刚开始一个服务于服务端,一个服务于前端,长久以来,它们都能够和睦相处,直到有一天,一个叫做node.js的JavaScript运行环境诞生后,再加 ...

  2. TOJ4203: Domino Piece

    4203: Domino Piece  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 5 ...

  3. DS博客作业05——树

    1.本周学习总结 1.1思维导图 1.2学习体会 学习:相比于之前的数据结构,树多了很多性质,相应的也多了很多计算题,不得不说,专有名词也是颇多.觉得树最独特的地方就是它的兄弟.孩子结点,用以组成了它 ...

  4. Spark2.1.0之源码分析——事件总线

    阅读提示:阅读本文前,最好先阅读<Spark2.1.0之源码分析——事件总线>.<Spark2.1.0事件总线分析——ListenerBus的继承体系>及<Spark2. ...

  5. Topcoder SRMCards ——贪心

    选择一个数x会删去x+1和x-1,问可以最多选多少次. 显然,对于一段连续的数列,贪心的从左向右选取是最优的. 然后就可以贪心的统计答案了. #include <map> #include ...

  6. bzoj1076【SCOI2008】奖励关

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1076 有n种物品等概率落下,求期望最优收益 sol:  一眼看上去就是状压dp吧QAQ数据范围 ...

  7. kali2 install Nessus

    注册: https://www.tenable.com/products/nessus-home 安装: 设置登录用户名,密码,输入注册码:

  8. java面试题之volatile的工作原理

    volatile的特性: volatile可见性:对一个volatile的读,总可以看到对这个变量最终的写: volatile原子性:volatile对单个读/写具有原子性(32位Long.Doubl ...

  9. pytesseract的使用

    首先,先安装好Tesseract软件,pytesseract模块 问题 1 :FileNotFoundError: [WinError 2] 系统找不到指定的文件. 加上这行代码 pytesserac ...

  10. bzoj 2792 [Poi2012]Well 二分+dp+two_pointer

    题目大意 给出n个正整数X1,X2,...Xn,可以进行不超过m次操作,每次操作选择一个非零的Xi,并将它减一. 最终要求存在某个k满足Xk=0,并且z=max{|Xi - Xi+1|}最小. 输出最 ...