网站伪静态如何配置文件,成都网站改版优化,广州网页设计网站建设,做网站的优惠广告8种机械键盘轴体对比本人程序员#xff0c;要买一个写代码的键盘#xff0c;请问红轴和茶轴怎么选#xff1f;又到了周四分享环节#xff0c;鉴于最近在看linux编程实践#xff0c;所以就的讲一下如何编写一个简单的who命令。PPTManual PageManual Page 也就是大家常用的m…8种机械键盘轴体对比本人程序员要买一个写代码的键盘请问红轴和茶轴怎么选又到了周四分享环节鉴于最近在看linux编程实践所以就的讲一下如何编写一个简单的who命令。PPTManual PageManual Page 也就是大家常用的man命令是unix、类unix系统常用的程序文档。1234Usage:$ man $ man -k [apropos options] regexp这种形式我们可以通过关键字来匹配手册中的descriptions。man man:1 Executable programs or shell commands2 System calls (functions provided by the kernel)3 Library calls (functions within program libraries)4 Special files (usually found in /dev)5 File formats and conventions eg /etc/passwd6 Games7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)8 System administration commands (usually only for root)9 Kernel routines [Non standard]可以看出来我们主要用2 和 3WHO1234$ whokcilcarus tty1 2018-07-06 06:06 (:0)用户名 终端 时间who命令对应的输出如上所示那么我们猜下who的工作原理用户登录登出的时候会将信息保存在某个文件who命令打开这个文件读取文件信息输入到控制台恩 逻辑很清晰。 下面就是如何做了。如何知道who命令读取的那个文件呢123456789101112$ man whouse /var/run/utmp注意到这句话$ man -k utmputmp (5) - login records$ man 5 utmpThe utmp file allows one to discover information about who is currently using the system.那肯定是他了而且还提供了相应的结构体信息当然我们也可以在/usr/include/下面找到标准头文件到这里我们知道了只要读取utmp文件就可以了。那么如何读取文件信息呢很自然我们想到了 man -k file, 只要知道了用哪个命令就好了, 当然也可以google12345678$ man -k file | grep readread (2) - read from a file descriptor其中这一行引起了我们的注意。哈哈 皮卡丘就是你了。$ man 2 readssize_t read(int fd, void *buf, size_t count);文件描述符 缓冲区 字节数通过阅读文档 我们了解到read有3个参数返回值是成功读取的字节数并讲读取的字节存入缓冲区。那应该就是他了但是文件描述符又是什么鬼我们继续往下看在see also 里 我们看到有个open(2)1234$ man 2 openint open(const char *pathname, int flags);路径 进入模式 只读只写读写返回值是文件描述符。那么整理一下。open 打开文件, 返回文件描述符read 根据文件描述符读取文件内容一次读取struct utmp 大小即可输出到控制台close 关闭文件1234567891011121314151617181920212223242526272829303132333435#include #include #include #include void (struct utmp * record);int main(){struct utmp current_record;int fd;int len sizeof(current_record);if ((fd open(/var/run/utmp, O_RDONLY)) -1){perror(/var/run/utmp);exit(1);}while (read(fd, current_record, len) len){show_record(current_record);}close(fd);exit(0);}void (struct utmp * record){printf(%8s , record-ut_user);printf(%8s, record-ut_line);printf(n);}恩 我们执行一下1234$ gcc test.c -o test$ ./testreboot ~kcilcarus tty1基本上可以了不过reboot是啥时间也没有有空了在优化下。那么一个简单的who命令就到此结束啦