1. 关于 man 命令
man 命令使用选项 -k 可以根据关键字搜索联机帮助,不过只支持一个关键字的查找。联机手册分为很多小结,man 后加上小结编号查看不同章节的帮助内容,如:man 3 read
2. 用到的系统调用
1> open
2> read
3> close
4> 关于UNIX时间
① time_t 数据类型
typedef long int time_t; 用来存储从1970年到现在经过了多少秒。
② timeval 结构体
struct timeval
{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
结构struct timeval,它精确到微妙。
③ tm 结构体
struct tm
{
int tm_sec; /*秒,正常范围0-59, 但允许至61*/
int tm_min; /*分钟,0-59*/
int tm_hour; /*小时, 0-23*/
int tm_mday; /*日,即一个月中的第几天,1-31*/
int tm_mon; /*月, 从一月算起,0-11*/ 1+p->tm_mon;
int tm_year; /*年, 从1900至今已经多少年*/ 1900+ p->tm_year;
int tm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
int tm_yday; /*从今年1月1日到目前的天数,范围0-365*/
int tm_isdst; /*日光节约时间的旗标*/
};
需要特别注意的是,年份是从1900年起至今多少年,而不是直接存储如2011年,月份从0开始的,0表示一月,星期也是从0开始的, 0表示星期日,1表示星期一。
{
int tm_sec; /*秒,正常范围0-59, 但允许至61*/
int tm_min; /*分钟,0-59*/
int tm_hour; /*小时, 0-23*/
int tm_mday; /*日,即一个月中的第几天,1-31*/
int tm_mon; /*月, 从一月算起,0-11*/ 1+p->tm_mon;
int tm_year; /*年, 从1900至今已经多少年*/ 1900+ p->tm_year;
int tm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
int tm_yday; /*从今年1月1日到目前的天数,范围0-365*/
int tm_isdst; /*日光节约时间的旗标*/
};
需要特别注意的是,年份是从1900年起至今多少年,而不是直接存储如2011年,月份从0开始的,0表示一月,星期也是从0开始的, 0表示星期日,1表示星期一。
④ 常用的时间函数:
#include <time.h>1. asctime
char *asctime(const struct tm* timeptr);
将结构中的信息转换为真实世界的时间,以字符串的形式显示2. ctime
char *ctime(const time_t* timep);
将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样,转换后的格式:"Wed Jun 30 21:49:08 1993\n"3. difftime
double difftime(time_t time1, time_t time2);
返回两个时间相差的秒数4. gettimeofday
int gettimeofday(struct timeval* tv,struct timezone* tz);
返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用
5. gmtime
struct tm* gmtime(const time_t* timep);
将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针6. localtime
struct tm* localtime(const time_t* timep);
和gmtime类似,但是它是经过时区转换的时间。7. mktime
time_t mktime(struct tm* timeptr);
将struct tm 结构的时间转换为从1970年至今的秒数8. time
time_t time(time_t* t);
取得从1970年1月1日至今的秒数。9. strftime
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
将 tm 结构体转换为字符串。
10. strptime
char *strptime(const char *s, const char *format, struct tm *tm);
将字符串转换为 tm 结构体。
⑤ 一段代码:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
struct tm tm;
char buf[255];
memset(&tm, 0, sizeof(struct tm));
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
puts(buf);
exit(EXIT_SUCCESS);
}
5> creat
6> write
7> lseek
3. who 的编写
/* who2.c - read /etc/utmp and list info therein
* - suppresses empty records
* - formats time nicely
*/
#include <stdio.h>
#include <unistd.h>
#include <utmp.h>
#include <fcntl.h>
#include <time.h>
/* #define SHOWHOST */
void showtime(long);
void show_info(struct utmp *);
int main()
{
struct utmp utbuf; /* read info into here */
int utmpfd; /* read from this descriptor */
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){
perror(UTMP_FILE);
exit(1);
}
while( read(utmpfd, &utbuf, sizeof(utbuf)) == sizeof(utbuf) )
show_info( &utbuf );
close(utmpfd);
return 0;
}
/*
* show info()
* displays the contents of the utmp struct
* in human readable form
* * displays nothing if record has no user name
*/
void show_info( struct utmp *utbufp )
{
if ( utbufp->ut_type != USER_PROCESS )
return;
printf("%-8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("%-8.8s", utbufp->ut_line); /* the tty */
printf(" "); /* a space */
showtime( utbufp->ut_time ); /* display time */
#ifdef SHOWHOST
if ( utbufp->ut_host[0] != '\0' )
printf(" (%s)", utbufp->ut_host);/* the host */
#endif
printf("\n"); /* newline */
}
void showtime( long timeval )
/*
* displays time in a format fit for human consumption
* uses ctime to build a string then picks parts out of it
* Note: %12.12s prints a string 12 chars wide and LIMITS
* it to 12chars.
*/
{
char *cp; /* to hold address of time */
cp = ctime(&timeval); /* convert time to string */
/* string looks like */
/* Mon Feb 4 00:46:40 EST 1991 */
/* 0123456789012345. */
printf("%12.12s", cp+4 ); /* pick 12 chars from pos 4 */
}
4. cp 的编写
/** cp1.c
* version 1 of cp - uses read and write with tunable buffer size
*
* usage: cp1 src dest
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
void oops(char *, char *);
main(int ac, char *av[])
{
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];
/* check args */
if ( ac != 3 ){
fprintf( stderr, "usage: %s source destination\n", *av);
exit(1);
}
/* open files */
if ( (in_fd=open(av[1], O_RDONLY)) == -1 )
oops("Cannot open ", av[1]);
if ( (out_fd=creat( av[2], COPYMODE)) == -1 )
oops( "Cannot creat", av[2]);
/* copy files */
while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
if ( write( out_fd, buf, n_chars ) != n_chars )
oops("Write error to ", av[2]);
if ( n_chars == -1 )
oops("Read error from ", av[1]);
/* close files */
if ( close(in_fd) == -1 || close(out_fd) == -1 )
oops("Error closing files","");
}
void oops(char *s1, char *s2)
{
fprintf(stderr,"Error: %s ", s1);
perror(s2);
exit(1);
}
5. 编写终端注销代码
#include <utmp.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
int logout_tty(char * line)
{
int fd;
struct utmp rec;
int len = sizeof(struct utmp);
int retval = -1;
if ( (fd = open(UTMP_FILE, O_RDWR)) == -1 )
return -1;
while (read(fd, &rec, len)==len )
if(strncmp(rec.ut_line, line, sizeof(rec.ut_line)) == 0)
{
rec.ut_type = DEAD_PROCESS;
if(lseek(fd, -len, SEEK_CUR) != -1)
if(write(fd, &rec, len) == len)
retval = 0;
break;
}
if(close(fd) == -1)
retval = -1;
return retval;
}