Linux/Unix常用底层库
unistd.h | unix standard | |
stdlib.h | standard library | |
fcntl.h | file control | |
sys.h | system | |
stdio.h | standard input/output | |
dirent.h | directory entries | |
string.h | string operation | |
errno.h | system error numbers | |
pwd.h | password structure | |
sys/resource.h | definitions for XSI resource operations | |
limits.h | implementation-dependent constants | |
termios.h | define values for termios | 定义POSIX规范中定义的的标准接口termios的数据结构和相关函数 |
文件操作
底层文件访问
write()
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
/**
fildes : file descriptor
buf : buffer
nbytes : number of bytes
**/
把缓冲区buf前的nbytes个字节写入与文件描述符fildes关联的文件中,并返回实际写入的字节,否则返回0或者-1。
read()
#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes);
/**
fildes : file descriptor
buf : buffer
nbytes : number of bytes
**/
open()
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
/**
*path : path of file or device
oflag : flag of action to open the file, detail in below
oflag list:
O_RDONLY : open with read only mode
O_WRONLY : opne with write only mode
O_RDWR : open with read and write mode
O_APPEND : append the content follow the tail of file
O_TRUNC : make the file content truncated
O_CREAT : create the file if need
O_EXCL :
mode : mode of invoke open() when arg oflags is O_CREAT, detail in below
S_IRUSR : access to read for file owner
S_IWUSR : access to write for file owner
S_IXUSR : access to execute for file owner
S_IRGRP : access to read for file group
S_IWGRP : access to write for file group
S_IXGRP : access to execute for file group
S_IROTH : access to read for file other user
S_IWOTH : access to write for file other user
S_IXOTH : access to execute for file other user
**/
close()
#include <unistd.h>
int close(int fildes);
/**
fildes : file descriptor
**/
ioctl()
#include <unistd.h>
int ioctl(int fildes, int cmd, ...)
/**
fildes : file descriptor
cmd : command
**/
lseek()
#include <unistd.h>
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
/**
fildes : file descriptor
offset : the offset position of read/write cursor relate to the cursor of fildes
whence : define the function of offset
whence list:
SEEK_SET :
SEEK_CUR :
SEEK_END :
**/
fstat(),stat(),lstat()
#include <unistat.h>
#include <sys/types.h>
#include <sys/stat.h>
int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
/**
fildes : file descriptor
buf : buffer
path : file path
members list in struct stat:
st_mode : file access and mode info
st_ino : inode relate to file
st_dev : device the file be stored
st_uid : user id of file owner
st_gid : group id of file owner
st_atime : latest access time
st_ctime : latest file attributes change time
st_mtime : latest content modify time
st_nlink : number of hard link
**/
dup(),dup2()
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);
/**
fildes : file descriptor
**/
标准I/O库
fopen()
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
/**
filename : file name
mode : mode open file
mode list:
"r" "rb" : read only
"w" "wb" : write only
"a" "ab" : append
"r+" "rb" "r+b" : refresh with read and write
"w+" "wb" "w+b" : refresh with write
"a+" "ab" "a+b" : refresh with append
**/
fread()
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
/**
*ptr : pointer which data buffer refer in file stream
size : length every record
nitems : number of transfer record
stream : file stream
**/
数据管理
内存管理
# include <stdlib.h>
void *malloc(size_t size);
# include <stdlib.h>
void free(void *ptr_to_memory);
#include <stdlib.h>
void *calloc(size_t number_of_elements, size_t element_size);
void *realloc(void *existing_memory, size_t new_size);
文件锁定
#include <fcntl.h>
int fcntl(int fildes, int command, struct flock *flock_structure);
/**
fildes : file description
command
flock_structure : structure of file lock
command list:
F_GETLK : get lock info of fildes
F_SETLK : set lock or unlock the file which fildes refer to
F_SETLKW : wait until getting availability to lock the fildes
**/
参考资料:
Linux程序设计 第4版(Beginning Linux Programming,4th Edition),Neil Matthew & RIchard Stones