Linux命令实现(1) who

想学Linux c编程
借来的书都千篇一律
都是从读写文件到进程通讯和socket
感觉没有VC的书丰富
学起来也很枯燥
终于借到一本带实例的书
Understanding Unix/Linux Programming
A Guide to Theroy and Practice
一本在实例里教学的书
我的目标是自己把Linux的主要命令写一遍

先是简单的who
读取/var/run/utmp文件 显示出来就可以了

//who.c
#include <stdio.h>
#include <stdlib.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>

#define SHOWHOST
void show_time(long timeval)
{
    char *cp;
    cp=ctime(&timeval);
    printf("%12.12s",cp+4);
}

void show_info(struct utmp * utbufp)
{
    if(utbufp->ut_type!=USER_PROCESS)
        return;
    printf("% -8.8s ",utbufp->ut_name);
    printf("% -8.8s ",utbufp->ut_line);
    show_time((utbufp->ut_time));
   
#ifdef SHOWHOST
    printf("(%s)",utbufp->ut_host);
#endif
    printf("/n");

}

int main(int argc, char *argv[])
{
 // perror(UTMP_FILE);
 struct utmp current_record;
 int utmpfd;
 int reclen=sizeof(current_record);

 if((utmpfd=open(UTMP_FILE,O_RDONLY)) == - 1)
 {
   
     exit(1);
 }

 
while(read(utmpfd,&current_record,reclen)==reclen)
{

    show_info(&current_record);
}
    close(utmpfd);
   
  return EXIT_SUCCESS;
}

搞笑的是Unix使用
time_t (long int型)来保存时间
即1970年1月1日0时开始到现在的秒数
我靠  这会不会是另一个千年虫问题啊
一个long int能抗多少年啊?
要是那些Linux服务器出了问题可就天下大乱了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值