第1章 UNIX基础知识
第2章 UNIX标准化及实现
第3章 文件I/O
1.
#include <fcntl.h>
int open(const char *pathname, int oflag, …/* mode_t mode */);
oflag:
O_RDONLY O_WRONLY O_RDWR (这三个常量必须有一个且互斥)
可选常量:O_APPEND O_CREAT O_EXCL O_TRUNC O_NOCTTY O_NONBLOCK
2.
#include <fcntl.h>
int creat(const char *pathname, mode_tmode);
等效于open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
3.
#include <unistd.h>
int close(int filedes);
4.
#include <unistd.h>
off_t lseek(int filedes, off_t offset, intwhence);
whence:
SEEK_SET SEEK_CUR SEEK_END
确定打开文件的当前偏移量:off_t currops = lseek(fd, 0, SEEK_CUR);
5.
#include <unistd.h>
ssize_t read(int filedes, void *buf, size_tnbytes);
6.
#include <unistd.h>
ssize_t write(int filedes, const void *buf,size_t nbytes);
7.
STDIN_FILENO STDOUT_FILENO
BUFFSIZE为块长st_blksize时效率最高(一般为4096)
8.
STDIN_FILENO 、STDOUT_FILENO
--------------------------------------------
Int main()
{
Int n;
Charbuf[BUFFSIZE];
While ((n =read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
If (write(STDOUT_FILENO, buf, n) != n)
printf(“write error!”);
exit(0);
}
-----------------------------------------------
9.
原子操作:
#include <unistd.h>
ssize_t pread(int filedes, void *buf,size_t nbytes, off_t offset);
ssize_t pwrite(int filedes, const void*buf, size_t nbytes, off_t offset);
10.
复制一个现存的文件描述符:
#include <unistd.h>
Int dup(int filedes); /* 返回的新文件描述符一定是当前可用文件描述符中的最小值 */
Int dup2(int filedes, int filedes2); /* 用filedes2指定新描述的数值,如果filedes2已打开,则先关闭;如果filedes= filedes2,返回filedes2 */
11.
防止延迟写造成的数据丢失:
#include <unistd.h>
Int fsync(int filedes); /* 等待写磁盘操作结束,除数据外,也更新文件属性 */
Int fdatasync(int filedes); /* 等待写磁盘操作结束,只更新文件数据部分 */
void sync(void); /* 只将所有修改过的块缓冲区排入写队列,然后就返回,不等待实际写磁盘操作结束。*/
12.
#include <fcntl.h>
Int fcntl(int filedes, int cmd, … /* intarg */);
Cmd: F_DUPFD // 复制文件描述符
F_GETFD or F_SETFD 获得/设置文件描述符标记
F_GETFL or F_SETFL 获得/设置文件状态标志(O_RDONLY、O_WRONLY、O_APPEND等)
F_GETOWN or F_SETOWN 获得/设置异步I/O所有权
F_GETLK F_SETLK or F_SETLKW 获得/设置记录锁
13.
#include <unistd.h> /* System V */
#include <sys/ioctl.h> /* BSD andLinux */
#include <stropts.h> /* XSI STREAMS */
Int ioctl(int filedes, int request, …);
第4章文件和目录
1.
获得与文件有关的信息结构(struct stat)
#include <sys/stat.h>
Int stat(const char *restrict pathname,struct stat *restrict buf);
Int fstat(int filedes, struct stat *buf);
Int lstat(const char *restrict pathname,struct stat *restrict buf); /* 类似stat,当文件是一个符号链接时,返回符号链接的有关信息,不是由该符号链接引用文件的信息。*/
Struct stat
{
Mode_tst_mode;
Ino_tst_ino;
Dev_tst_dev;
Dev_tst_rdev;
Nlink_tst_nlink;
Uid_tst_uid;
Gid_tst_gid;
Off_tst_size;
Time_tst_atime;
Time_tst_mtime;
Time_tst_ctime;
Blksize_tst_blksize;
Blkcnt_tst_blocks;
}
2.
<sys/stat.h>中的文件类型宏
S_ISREG() // 普通文件
S_ISDIR() // 目录文件
S_ISCHR() // 字符特殊文件
S_ISBLK() // 块特殊文件
S_ISFIFO() // 管道或FIFO
S_ISLNK() // 符号链接
S_ISSOCK() // 套接字
-----------------------------------------
Struct stat buf;
…
If (S_ISREG(buf.st_mode))
…
------------------------------------------
3.
<sys/stat.h>中的IPC类型宏, 参数为指向stat结构的指针:
S_TYPEISMQ() // 消息队列
S_TYPEISSEM() // 信号量
S_TYPEISSHM() // 共享存储对象
4.
访问权限测试:
#include <unistd.h>
Int access(const char *pathname, int mode);
Mode:
R_OK
W_OK
X_OK
F_OK // 测试文件是否存在
---------------------
…
If (access(argv[1], R_OK) < 0 )
Err_ret(“accesserror!”);
Else
Printf(“readaccess OK\n”);
If (open(argv[1], O_RDONLY) < 0 )
Err_ret(“openerror!”);
…
--------------------------------------
5.
设置文件模式创建屏蔽字,并返回以前的值
#include <sys/stat.h>
Mode_t umask(mode_t cmask);
---------------------
#define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP|S_IWGRP | S_IROTH | S_IWOTH)
…
Umask(0);
…
creat(“foo”, RWRWRW);
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
creat(“bar”, RWRWRW);
…
结果:
-rw------- bar
-rw-rw-rw- foo
------------------------------------------------------
6.
更改文件访问权限:
#include <sys/stat.h>
Int chmod(const char *pathname, mode_t mode);
Int fchmod(int filedes, mode_t mode);
mode:
S_ISUID
S_ISGID
S_ISVTX // 保存正文(粘住位)
S_IRWXU
S_IRUSR S_IWUSR S_IXUSR
S_IRWXG
S_IRGRP S_IWGRP S_IXGRP
S_IRWXO
S_IROTH S_IWOTH S_IXOTH
---------------
…
chmod(“foo”, (statbuf.st_mode &~S_IXGRP | S_ISGID) // 打开设置组ID, 关闭组执行位。
…
-----------------------------------
7.
更改文件的用户ID和组ID:
#include <unistd.h>
Int chown(const char *pathname, uid_towner, gid_t group);
Int fchown(int filedes, uid_t owner, gid_tgroup);
Int lchown(const char *pathname, uid_towner, gid_t group); /* 更改符号链接本身的所有者,而不是该符号链接所指向的文件 */
若owner或group中的任意一个是-1,则对应的ID不变。
8.
文件截短:
#include <unistd.h>
Int truncate(const char *pathname, off_tlength);
Int ftruncate(int filedes, off_t length);
把现有文件长度截短为length字节,若长度小于length,效果与系统有关,可能创建一个空洞。
9.
创建一个指向现有文件的链接:
#include <unistd.h>
Int link(const char *existingpath, constchar *newpath);
10.
删除一个现有的目录项:
Int unlink(const char *pathname); /* 将由pathname所引用文件的链接计数减1 */
#include <stdio.h>
Int remove(const char *pathname); /* 对于文件,功能与unlink相同,对于目录,与rmdir相同 */
11.
文件或目录更名:
#include <stdio.h>
Int rename(const char *oldname, const char*newname);
12.
#include <unistd.h>
Int symlink(const char *actualpath, constchar *sympath); /* 创建符号链接,并不要求actualpath已经存在,且actualpath与sympath不必在同一文件系统中*/
#include <unistd.h>
Ssize_t readlink(const char *restrictpathname, char *restrict buf, size_t bufsize); /* 打开链接本身,读该链接中的名字,返回读入buf的字节数*/
13.
文件访问和修改时间的更改:
#include <utime.h>
Int utime(const char *pathname, conststruct utimbuf *time);
Struct utimbuf
{
time_tactime; /* access time */
time_tmodtime; /* modification time */
}
14.
#include <sys/stat.h>
Int mkdir(const char *pathname, mode_t mode);
#include <unistd.h>
Int rmdir(const char *pathname);
15.
读目录:
#include <dirent.h>
DIR *opendir(const char *pathname);
Struct dirent *readdir(DIR *dp);
Void rewinddir(DIR *dp);
Int closedir(DIR *dp);
Long telldir(DIR *dp);
Void seekdir(DIR *dp, long loc);
Struct dirent
{
Ino_td_ino;
Chard_name[NAME_MAX + 1];
}
16.
更改当前工作目录:
#include <unistd.h>
int chdir(const char *pathname);
int fchdir(int filedes);
17.
获得当前工作目录完整的绝对路径:
#include <unistd.h>
char *getcwd(char *buf, size_t size);