编写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 ...
随机推荐
- 143 - ZOJ Monthly, October 2015 I Prime Query 线段树
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- Linux gadget驱动分析3------复合设备驱动
windows上面对usb复合设备的识别需要下面条件. “ 如果设备满足下列要求,则总线驱动程序还会报告 USB\COMPOSITE 的兼容标识符: 设备描述符的设备类字段 (bDeviceClass ...
- YTU 2769: 结构体--成绩统计
2769: 结构体--成绩统计 时间限制: 1 Sec 内存限制: 128 MB 提交: 1021 解决: 530 题目描述 建立一个简单的学生信息表,包括:姓名.性别.年龄及一门课程的成绩,统计 ...
- Java异常&&RuntimeException异常
Java异常可分为3种: (1)编译时异常:Java.lang.Exception (2)运行期异常:Java.lang.RuntimeException (3)错误:Java.lang.Error ...
- POJ3090 巧用欧拉函数 phi(x)
POJ3090 给定一个坐标系范围 求不同的整数方向个数 分析: 除了三个特殊方向(y轴方向 x轴方向 (1,1)方向)其他方向的最小向量表示(x,y)必然互质 所以对欧拉函数前N项求和 乘2(关于( ...
- 题目收藏夹(啥时候一遍A啥时候删)
以下题目为没有思路或代码离谱错误或看了题解才会的,间隔一周以上再做一遍A掉就删. bzoj1500 bzoj2287 codevs1358 bzoj1725
- 通过类库ChineseChar实现将汉字转化为拼音
//封装dllusing Microsoft.International.Converters.PinYinConverter;using System.Text;namespace Utils{ p ...
- 根据JSON创建对应的HIVE表
本文提供一种用SCALA把JSON串转换为HIVE表的方法,由于比较简单,只贴代码,不做解释.有问题可以留言探讨 package com.gabry.hiveimport org.json4s._im ...
- linux安装/卸载mysql
其实安装mysql差不多有10次了吧, 但是每次都有坑,各种百度,太麻烦了,所以这次把坑给记录下来,下次直接用. 1. 去官网下载mysql.这里可以使用wget下载.先去官方网站,找到mysql5. ...
- nginx入门学习
1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了 yum install gcc patch libffi-devel python-devel zlib-devel bzip ...