借來的書都千篇一律
都是從讀寫文件到進程通訊和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,¤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服務器出了問題可就天下大亂了