当前位置:网站首页>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
边栏推荐
- Matrix and coordinates
- Ubuntu system installation and MySQL configuration
- Network counting 01 physical layer
- 这样理解mmap,挺有意思!
- Comparison between Oracle JDK and openjdk
- Differences between JS valueof and toString
- How to understand JS promise
- Mikrotik Routeros Internet access settings
- Flinkv1.13实现金融反诈骗案例
- 电脑USB、HDMI、DP各种接口及速度
猜你喜欢

渗透常用工具-Goby

MapReduce programming basics
![[untitled]](/img/1a/e18918cc09db9b072759409a5f39a1.png)
[untitled]

Packetdrill script analysis guide

Floyd repeat

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”

Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录

Flinkv1.13 implementation of financial anti fraud cases

IPv6 learning notes

Dotnet console uses microsoft Maui. Getting started with graphics and skia
随机推荐
mysql截取_mysql截取字符串的方法[通俗易懂]
新数据库时代,不要只学 Oracle、MySQL
Sleeping second brother...
Apple amplification! It's done so well
Centos 配置discuz 提示请检查 mysql 模块是否正确加载
树莓派4B系统搭建(超详细版)
Who has the vision to cross the cycle?
Mikrotik Routeros Internet access settings
渗透常用工具-Goby
JS use toString to distinguish between object and array
Huawei accounts work together at multiple ends to create a better internet life
Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
Matrix and coordinates
Upload labs for file upload - white box audit
Swag init error: cannot find type definition: response Response
Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
Flinkv1.13 implementation of financial anti fraud cases
SQL学习笔记(01)——数据库基本知识
[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States
Clickhouse: Test on query speed of A-share minute data [Part 2]