Linux编程-回忆录四

====时间====
程序计算时间有两种参考:
1.墙上的时间,也就是实际生活中使用的时间
2.处理的时间,也就是CPU执行程序使用了多少时间

#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
得到标准的calendar time时间,成功返回0,错误返回-1

#include <time.h>
time_t time(time_t *timep);
得到Epoch时间到目前为止的秒数,-1表示出错

#include <time.h>
char *ctime(const time_t *timep);
将当前时间转为简单的字符串描述,格式类似
Wed Jun  8 14:22:34 2011, 返回NULL表示出错

#include <time.h>
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
前者转换time_t类型的值为UTC标准的时间结构,后者转为本地化的时间结果
返回NULL表示处理出错

#include <time.h>
time_t mktime(struct tm *timeptr);
转换相应的时间结构为Epoch时间到目前为止的秒数,-1表示出错

#include <time.h>
size_t strftime(char *outstr, size_t maxsize, const char *format,
    const struct tm *timeptr);
将一个时间结构按照格式format,转换为一个字符串表示,写入outstr,成功
返回实际写入的字节数,-1表示出错

#define _XOPEN_SOURCE
#include <time.h>
char *strptime(const char *str, const char *format, struct tm *timeptr);
将一个满足时间表示格式format的字符串,转换为一个时间结构,成功返回下一个
没被处理的字符地址,NULL表示出错

====系统资源====
#include <unistd.h>
long sysconf(int name);
运行时获取系统各项配置信息,返回值由name决定,-1表示出错

#include <unistd.h>
long pathconf(const char *pathname, int name);
long fpathconf(int fd, int name);
获取文件路径的最大表示长度,-1表示出错

#include <sys/utsname.h>
int uname(struct utsname *utsbuf);
得到操作系统的相关信息,-1表示出错

====文件缓冲====
#include <stdio.h>
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
设置文件流的缓冲大小,-1表示出错
mode的值如下<默认是_IOFBF>
_IOFBF(满缓冲)  当缓冲区为空时,从流读入或写入数据
_IOLBF(行缓冲)  每次从流中读入一行数据或向流中写入一行数据
_IONBF(无缓冲)  直接读入/写入数据,无需缓冲,忽略buf, size参数

#include <stdio.h>
void setbuf(FILE *stream, char *buf);
buf为NULL,设置文件流不需要缓冲;否则设置满缓冲模式,buf的大小
必须为BUFSIZE的大小

#define _BSD_SOURCE
#include <stdio.h>
void setbuffer(FILE *stream, char *buf, size_t size);
同上的功能,不过可以指定缓冲大小为size

#include <stdio.h>
int fflush(FILE *stream);
刷新缓冲,同步缓冲内容到文件流stream中,EOF表示出错,0表示成功

#include <stdio.h>
int fileno(FILE *stream);
得到文件流的文件描述符,-1表示出错
FILE *fdopen(int fd, const char *mode);
得到文件描述符的文件流,NULL表示出错

#include <sys/mount.h>
int mount(const char *source, const char *target,
    const char *fstype, unsigned long mountflags, const void *data);
挂载文件系统,类似mount命令,source表示需要挂载的文件系统,target表示
挂载点,fstype表示挂载的source文件系统类型,mountflags,data是挂载文件
系统的相关选项参数,返回0表示成功,-1表示出错

#include <sys/mount.h>
int umount(const char *target);
卸除挂载点,返回0成功,-1失败

#include <sys/stat.h>
int stat(const char *pathname, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
获取文件的属性信息,返回0表示成功,-1失败;lstat,stat的区别在于
如果文件是一个链接文件时,lstat获取链接文件本身信息,而非链接的对象文件

#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
#define _XOPEN_SOURCE 500 /* Or: #define _BSD_SOURCE */
#include <unistd.h>
int lchown(const char *pathname, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
设置文件的权限,属于哪个用户,哪个组,返回0成功,-1失败。lchown,chow的区别
在于文件为链接文件时,lchown设置链接文件本身权限,而非链接的对象文件

#include <unistd.h>
int access(const char *pathname, int mode);
测试是否可以拥有mode权限访问文件,返回0表示可以,-1表示不行
mode的权限信息如下:
F_OK    文件存在么
R_OK    文件可读么
W_OK    文件可写么
X_OK    文件可执行么

#include <sys/stat.h>
mode_t umask(mode_t mask);
设置文件的权限掩码,返回上一次设置的掩码值。一般可以先保存上一次的
掩码值,等到必要的时候恢复

include <unistd.h>
int link(const char *oldpath, const char *newpath);
创建一个文件的硬链接,返回0成功,-1失败

#include <unistd.h>
int symlink(const char *filepath, const char *linkpath);
创建一个文件的软链接,返回0成功,-1失败

#include <unistd.h>
int unlink(const char *pathname);
删除一个文件的链接,如果是文件的最后一个硬链接,则删除文件
返回0成功,-1失败

注解:如果一个打开的文件被删除,只能是引用它的所有文件描述符关闭
才会真正的删除

#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
重命名一个文件,返回0成功,-1失败

nclude <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
创建一个给定权限的文件夹,返回0成功,-1失败

#include <unistd.h>
int rmdir(const char *pathname);
删除一个空文件夹,返回0成功,-1失败;需要注意这个函数
不能删除有子文件夹,或是含有文件的目录

#include <stdio.h>
int remove(const char *pathname);
删除一个文件(unlink),或是一个空文件夹(rmdir),返回0成功,-1失败
主要是为了实现C标准的库函数

#include <dirent.h>
DIR *opendir(const char *dirpath);
#include <dirent.h>
DIR *fdopendir(int fd);
打开一个目录,返回目录结构,返回NULL表示失败

#include <dirent.h>
struct dirent *readdir(DIR *dirp);
一个个读取目录里面的内容,返回一个内部的目录
条目信息,返回NULL表示失败或者结束

#include <dirent.h>
void rewinddir(DIR *dirp);
重置目录结构的读取偏移,指向第一个目录条目信息

#include <dirent.h>
int closedir(DIR *dirp);
关闭目录,返回0成功,-1失败

#include <dirent.h>
int dirfd(DIR *dirp);
得到目录流的文件描述符,返回0成功,-1失败

#include <unistd.h>
char *getcwd(char *cwdbuf, size_t size);
获取当前目录信息,成功返回cwdbuf,错误返回NULL

#include <unistd.h>
int chdir(const char *pathname);
#define _XOPEN_SOURCE 500 /* Or: #define _BSD_SOURCE */
#include <unistd.h>
int fchdir(int fd);
改变当前目录,成功返回0,-1失败

#define _BSD_SOURCE
#include <unistd.h>
int chroot(const char *pathname);
改变root目录,返回0成功,-1失败

#include <stdlib.h>
char *realpath(const char *pathname, char *resolved_path);
得到软链接的实际文件路径,resolved_path必须至少是PATH_MAX的大小

#include <libgen.h>
char *dirname(char *pathname);
char *basename(char *pathname);
得到一个路径的目录信息或是文件名信息

====监控文件系统====
目前使用inotify相关的API监控文件系统的操作事件
1.使用inotify_init函数创建inotify实例
2.使用inotify_add_watch函数添加需要监控的文件,或是目录,该函数
返回一个inotify文件描述符
3.从inotify文件描述符中读取时间进行处理
4.处理完毕后,移除所有的监控对象便可

注解:
a)inotify不是递归的,监控目录,不会监控目录下的各种子
目录,或是文件
b)inotify可以使用select(), poll(), epoll()等函数进行IO获取
监控的事件信息

#include <sys/inotify.h>
int inotify_init(void);
初始化inotify,返回inotify文件描述符,-1表示出错

#include <sys/inotify.h>
int inotify_add_watch(int fd, const char *pathname, uint32_t mask);
添加监控的文件,或是目录对象,返回监控的文件描述符,-1表示出错

#include <sys/inotify.h>
int inotify_rm_watch(int fd, uint32_t wd);
删除监控的对象,返回0成功,-1失败

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。