Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302744
  • 博文数量: 79
  • 博客积分: 3458
  • 博客等级: 中校
  • 技术积分: 921
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 17:09
个人简介

自、管

文章分类

全部博文(79)

文章存档

2013年(7)

2012年(20)

2011年(18)

2010年(34)

分类: LINUX

2010-08-09 18:08:06

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

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

//who.c
#include
#include
#include
#include
#include

#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,¤t_record,reclen)==reclen)
{

    show_info(¤t_record);
}
    close(utmpfd);
   
return EXIT_SUCCESS;
}

搞笑的是Unix使用
time_t (long int型)来保存时间
即1970年1月1日0时开始到现在的秒数
我靠 这会不会是另一个千年虫问题啊
一个long int能抗多少年啊?
要是那些Linux服务器出了问题可就天下大乱了
阅读(620) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~