当前位置:网站首页>Write your own who commands
Write your own who commands
2022-07-01 09:52:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Today, I followed the book step by step who Implementation of commands . Some books written by foreigners are just good , Inspire you step by step and tell you how to think , How to query online help according to existing clues , How to solve the problem step by step . That's good . Now I will follow the ideas in the book , Let's review this 2 Hours of work .
1.who What can an order do 2.who How commands work 3. How to write who command
1.who What can an order do
We can type... On the command line who command , give the result as follows :[email protected]:~ whocaoli tty2 2010-11-23 15:41caoli tty7 2010-11-23 15:30 (:0)caoli pts/0 2010-11-23 16:12 (:0.0)caoli pts/1 2010-11-23 16:51 (:0.0)[email protected]:~
Through online help man who
: Print information about users who are currently logged in.
who It can display the information of the logged in users in the current system . You can probably know who The function of and its description and some options used ....
2.who How commands work step1: according to man who in : If FILE is not specified, use /var/run/utmp.
You can know who It's from /var/run/utmp To obtain user login information .
step2: What we know now is a directory , The next step is to find information about him . man -k Options can be found according to keywords . therefore man -k utmp You can find utmp(4) Information about ,::: I am direct man utmp Of .
step3: Check again utmp Details in : more /usr/include/utmp.h Now , We will find that utmp Inside is the structure array . Recorded information about user login .
step4: According to the above steps, it can be inferred that who How it works : Read out the contents of the file recording the user's login information one by one . Then we can realize it according to this idea .
3. How to write who command Key points : How to read data structures from files Here we need to use some knowledge related to file operation ( Here is the system call and what I learned in my freshman year fopen Wait is not the same thing , Actually, I think the functions are similar )
Then we start coding The main function part : #include <utmp.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h>
#define SHOWHOST void show_info(struct utmp *utbufp);
int main(int argc, char** argv) { struct utmp current_record;// A buffer int utmpfd; // File descriptor int reclen=sizeof(current_record); if((utmpfd=open(UTMP_FILE,O_RDONLY))==-1) { perror(UTMP_FILE); return 1; } while(read(utmpfd,¤t_record,reclen)==reclen) show_info(¤t_record); // Show utmp The content of close(utmpfd); return 0; }
Display the login information section : void show_info(struct utmp *utbufp) { if(utbufp->ut_type!=USER_PROCESS) // Clear Blank Records return; printf(“%-8.8s/t”,utbufp->ut_name); printf(“%-8.8s/t”,utbufp->ut_line); // printf(“%10ld/t”,utbufp->ut_time); show_time(utbufp->ut_time); #ifdef SHOWHOST if(utbufp->ut_host[0]!=’/0′) printf(“(%s)”,utbufp->ut_host); #endif printf(“/n”); } Running results : [email protected]:~/workspace/test$ ./who1 reboot ~ 1290497376 (2.6.32-26-generic) runlevel ~ 1290497376 (2.6.32-26-generic) LOGIN tty4 1290497376 () LOGIN tty5 1290497376 () caoli tty2 1290498098 () LOGIN tty3 1290497376 () LOGIN tty6 1290497376 () LOGIN tty1 1290497384 () caoli tty7 1290497409 (:0) caoli pts/0 1290499920 (:0.0) caoli pts/1 1290501832 (:0.0) caoli pts/2 1290501833 () caoli pts/1 1290502298 (:0.0) caoli pts/2 1290506795 (:0.0) The result here is the same as that at the beginning who Compare the results of the command , We will find that One , We need to filter out names that are not users Two , To display the time correctly 3、 ... and , The host name that does not need to be displayed can be omitted
According to the first 1 spot : Make the following changes utmp One of the items in the structure is ut_type, When his value is 7 when , Indicates a logged in user , So the display function show_info The display in the user slightly modified void show_info(struct utmp *utbufp)utmp { if(utbufp->ut_type!=USER_PROCESS) // Clear Blank Records return; printf(“%-8.8s/t”,utbufp->ut_name);
According to the first 2 spot : We according to the time.h Change the content in , About the time function , I am already in Linux Programming part 4 I learned in chapter , So I just jumped over here . ctime(##) You need a point in time_t The pointer to . The returned time string is similar to the following Wed Jun 30 21:49:09 1993/n We need to 4 Bit start output 12 Characters printf(“%12.12s”,ctime(&t)+4); So add the show_time Function as follows void show_time(long timeval) { char *cp; cp=ctime(&timeval); printf(“%12.12s”,cp+4); } Be careful ctime Is in time.h Inside . So add a header file at the beginning of the program .
The first 3 spot Here is a definition of preprocessing , I don't know much about it , If I see it , Just comment ! if(utbufp->ut_host[0]!=’/0′) printf(“(%s)”,utbufp->ut_host);
The final code is as follows :
#include <utmp.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <time.h>#define SHOWHOST void show_info(struct utmp *utbufp);void show_time(long);int main(int argc, char** argv){struct utmp current_record;// A buffer int utmpfd; // File descriptor int reclen=sizeof(current_record);if((utmpfd=open(UTMP_FILE,O_RDONLY))==-1){perror(UTMP_FILE);return 1;}while(read(utmpfd,¤t_record,reclen)==reclen) show_info(¤t_record); // Show utmp The content of close(utmpfd);return 0;}void show_info(struct utmp *utbufp){ if(utbufp->ut_type!=USER_PROCESS) // Clear Blank Records return; printf(“%-8.8s/t”,utbufp->ut_name);printf(“%-8.8s/t”,utbufp->ut_line);show_time(utbufp->ut_time);#ifdef SHOWHOST if(utbufp->ut_host[0]!=’/0′) printf(“(%s)”,utbufp->ut_host);#endifprintf(“/n”);}void show_time(long timeval){char *cp;cp=ctime(&timeval);printf(“%12.12s”,cp+4);}
give the result as follows : [email protected]:~/workspace/test$ ./who1 caoli tty2 Nov 23 15:41 caoli tty7 Nov 23 15:30(:0) caoli pts/0 Nov 23 16:12(:0.0) caoli pts/1 Nov 23 16:51(:0.0) Come here , Already with the original who The orders are almost the same , ha-ha .
Through this practice , My biggest gain is that I can't ask others everything , If you can find the answer by yourself, try to find it by yourself , It's also an ability . In this regard , I think my husband did a good job , Worship him ! Good brother , Pay homage to you . Love your wife .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/131751.html Link to the original text :https://javaforall.cn
边栏推荐
- Sd-wan notes
- Centos 配置discuz 提示请检查 mysql 模块是否正确加载
- Flinkv1.13 implementation of financial anti fraud cases
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- 云原生到底是什么?它会是未来发展的趋势吗?
- Thread Basics
- TC8:UDP_USER_INTERFACE_01-08
- Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
- Closure implementation iterator effect
- 122. Thread class thread method summary; Why is the thread start method start () not run ()?
猜你喜欢

Swag init error: cannot find type definition: response Response

Finally, someone made it clear what DRAM and NAND flash are

Module 9: design e-commerce seckill system

CSDN一站式云服务开放内测,诚邀新老用户来抢鲜

苹果放大招!这件事干的太漂亮了……

SQL学习笔记(01)——数据库基本知识

电脑USB、HDMI、DP各种接口及速度

uniapp微信小程序组件按需引入

遇到女司机业余开滴滴,日入500!

Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
随机推荐
sql语句修改字段类型「建议收藏」
Who has the vision to cross the cycle?
Ubuntu system installation and MySQL configuration
BSN long story 10: how to ensure the safety of NFT
一个悄然崛起的国产软件,低调又强大!
Hardware midrange project
BSN长话短说之十:如何保证NFT的安全
嵌入式开发用到的一些工具
JS rewrite their own functions
Flinkv1.13实现金融反诈骗案例
JS prototype chain
TC8:UDP_USER_INTERFACE_01-08
A 419 error occurred in the laravel postman submission form. July 6th, 2020 diary.
About widthstep of images in opencv
Module 9: design e-commerce seckill system
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
Differences between JS valueof and toString
7-Zip 遭抵制?呼吁者定下“三宗罪”:伪开源、不安全、作者来自俄罗斯!
The programmer was beaten.