文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Linux命令实现(1) who

Linux命令实现(1) who

时间:2010-08-09  来源:haihui0705

想学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服务器出了问题可就天下大乱了
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载