自己动手写shell命令之who
来源:互联网 发布:阶级和阶层 知乎 编辑:程序博客网 时间:2024/05/21 19:39
思路:在unix系统中有一个名为utmp的文件,里面存着当前登录到系统中用户的信息。utmp这个文件里面保存的是结构数组,数组元素是utmp类型的结构。实现who命令,只要依次从utmp文件中读出utmp类型的结构体,然后通过合适的方式将它显示出来即可。如果每一次只从文件里面读一个结构体数据,那么每一个结构体都要进行依次系统调用。而又因为系统调用比较耗时(涉及到用户态到核心态的切换),所以这样会造成程序低效。我们使用缓冲技术,每次从文件中读若干结构体数据到内存中,当这些数据都已经显示在终端后,我们再从文件中读数据,这样能有效的提高程序的效率。
代码:
utmplib.c
#include <fcntl.h>#include <sys/types.h>#include <utmp.h>#define NRECS 16#define UTSIZE (sizeof(struct utmp))#define UTNULL ((struct utmp *)NULL)static char utmp_buffer[NRECS * UTSIZE];static int utmp_num;static int cur_num;static int utmp_fd = -1;int utmp_open(char * filename){ utmp_fd = open(filename,O_RDONLY); cur_num = 0; utmp_num = 0; return utmp_fd;}struct utmp * utmp_next(){ struct utmp * next; if(utmp_fd == -1) return UTNULL; if(cur_num == utmp_num && utmp_reload() == 0) return UTNULL; next = (struct utmp *)&utmp_buffer[UTSIZE*cur_num]; cur_num++; return next;}int utmp_reload(){ int len = read(utmp_fd,utmp_buffer,UTSIZE*NRECS); utmp_num = len / UTSIZE; cur_num = 0; return utmp_num;}void utmp_close(){ if(utmp_fd != -1) close(utmp_fd);
#include "utmplib.c"#include <sys/types.h>#include <utmp.h>#include <fcntl.h>#include <time.h>void show_info(struct utmp *);void showtime(time_t);int main(){printf("%s",UTMP_FILE);struct utmp * utmp_buffer;int fd;if((fd = utmp_open(UTMP_FILE)) == -1)return -1;//utmp_reload();while((utmp_buffer = utmp_next()) != UTNULL)show_info(utmp_buffer);utmp_close();return 0;}void show_info(struct utmp * buffer){if(buffer->ut_type != USER_PROCESS)//utmp结构中有一个ut_type成员,当其为USER_PROCESS时,表明这是一个已经登陆的用户return;printf("%-8.8s",buffer->ut_name);printf(" ");printf("%-8.8s",buffer->ut_line);printf(" ");showtime(buffer->ut_time);#ifdef SHOWHOSTif(buffer->ut_host[0] != '\0')printf("(%s)",buffer->ut_host);#endifprintf("\n");}void showtime(time_t time){char * cp;cp = ctime(&time);printf("%12.12s",cp+4);}
0 0
- 自己动手写shell命令之who
- 自己动手写shell命令之more
- 自己动手写shell命令之ls
- 自己动手写shell命令之ls -R1fF
- 自己动手写shell命令之pwd
- 自己动手写shell命令之du
- 自己动手写shell命令之write
- SHELL命令之who、whoami、id
- 自己动手写shell之chgrp,chown,chmod
- 自己动手写shell脚本
- 自己动手写PWD命令
- 自己动手写cp命令
- 自己动手写一个简单的Shell之二:运行程序
- 自己动手写一个简单的Shell之三:命令行解析
- 自己动手写一个简单的Shell之四:流程控制
- linux命令之who命令
- Linux命令详解之who
- 《自己动手写操作系统》之ReadSector
- 赛珍珠大地读书小感
- 百度快照更新与排名的关系
- Python For Data Analysis笔记
- JAVA wait(), notify(),sleep详解
- poj 2151 Check the difficulty of problems (概率dp)
- 自己动手写shell命令之who
- 修改title标题对于老站的影响
- 说说静态URL与动态URL
- 括号配对问题
- 浅谈百度比较讨厌的作弊方式
- 万事开头难 新站优化要用心
- zoj 3822 Domination
- Mysql 不同版本 说明
- 各种字符编码方式详解及由来(ANSI,GB2312,GBK,UNICODE,UTF-8)