编写who命令
第一个版本:
/* 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");}

编写who命令的更多相关文章
- 编写linux 命令行实用工具 shell命令
今天我想以带着问题的方法学习新的技术. 问题1: 编写一个命令 语法: command options path expressions
- Linux系统编程【3.1】——编写ls命令
ls命令简介 老规矩,直接在终端输入:man ls (有关于man命令的简介可以参考笔者前期博客:Linux系统编程[1]--编写more命令) 可以看到,ls命令的作用是显示目录中的文件名,它带有可 ...
- Linux系统编程【2】——编写who命令
学到的知识点 通过实现who命令,学到了: 1.使用man命令寻找相关信息 2.基于文件编程 3.体会到c库函数与系统调用的不同 4.加深对缓冲技术的理解 who命令的作用 who命令的使用 在控制终 ...
- 编写who命令:文件操作,缓冲区与联机帮助
最近阅读UULP(Understanding Unix/Linux Programming),按照书中介绍对Unix/Linux系统编程进行学习梳理,总结如下. 1. who命令能做什么 who命令用 ...
- 编写批处理命令来执行JAVA文件
http://baobeituping.iteye.com/blog/802543 1. JAVA工程路径结构 Project ------lib ------src ------W ...
- 让你如绅士般基于描述编写 Python 命令行工具的开源项目:docopt
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- CMakeLists.txt编写常用命令
目录 1. 设置cmake最小版本 2. 设置项目名称 3. 设置编译目标类型 4. 指定编译包含的源文件 1. 明确指明包含的源文件 2. 搜索指定目录的所有的cpp文件 3. 自定义搜索规则 4. ...
- docker Dockerfile文件的编写部分命令
镜像的构建过程:编写dockerfile文件,执行docker build 进行构建镜像,最后docker run 运行容器镜像实例. docker build -t mycentos:1.01 . ...
- Linux系统编程【1】——编写more命令
背景介绍 笔者知识背景 笔者接触Linux快一年了.理论知识方面:学习了操作系统基础知识,了解进程调度.内存分配.文件管理.磁盘I/O这些基本的概念. 实操方面:会使用Linux简单命令,在嵌入式系统 ...
- django “如何”系列2:如何编写django-admin 命令
应用可以使用manage.py注册自己的动作,例如,你可能想要为你即将发布的应用添加一个manage.py 操作.这节我们将为polls应用添加一个closepoll的命令 添加一个managemen ...
随机推荐
- node.js下操作cookie
cookie,又是cookie.工作中与cookie打交道很多次,不过时间跨度也大,每总结多一次,就加深了解多一点. cookie,一定是放在浏览器中的,用于浏览器保存一些小额度的内容.每次我们去访问 ...
- XAML实例教程系列 - 事件(Event) 五
Kevin Fan分享开发经验,记录开发点滴 XAML实例教程系列 - 事件(Event) 2012-06-19 01:36 by jv9, 1727 阅读, 7 评论, 收藏, 编辑 Events, ...
- pandas删除满足特定列信息的行记录
#!/usr/bin/python import pandas as pd df = pd.read_excel('c:\data\zichan.xlsx') df_sn = pd.read_exce ...
- openstack instance resize
Error: No valid host was found. No valid host found for resize
- MSP430 PIN 操作寄存器
1.P口端口寄存器: (1).PxDIR 输入/输出方向寄存器 (0:输入模式 1:输出模式) (2).PxIN 输入寄存器 输入寄存器是只读寄存器,用户不能对其写入,只能通过读取该寄 ...
- String Successor(模拟)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3490 题意:给出一个字符串,一个操作次数,每次操作从当前字符串最右边的字符 ...
- codevs1225八数码难题(搜索·)
1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description Yours和zero在研究A*启 ...
- bzoj1433[ZJOI2009]假期的宿舍(匈牙利)
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2544 Solved: 1074 [Submit][St ...
- jquery中对于为一组标签赋予点击事件
可以用each,但是each不能对动态的元素进行事件的绑定, 不过,其实也很简单,只需要获取所有的标签集,然后用动态绑定的方法,比如live进行绑定就可以了. 有时候,其实不难,只是自己想的太过复杂. ...
- AcWing算法基础1.5
前缀和与差分 两个内容都比较少,就放一起写了 设数组 a 的前 n 项为a1 , a2 , a3 ... an 前缀和数组就是每一项是a数组的前i项和,比如前缀和数组res,res[ 1 ] = a[ ...