📄 fcntl.c
字号:
/* * linux/fs/fcntl.c * * (C) 1991 Linus Torvalds */#include <string.h>#include <errno.h>#include <linux/sched.h>#include <linux/kernel.h>#include <asm/segment.h>#include <fcntl.h>#include <sys/stat.h>extern int sys_close(int fd);static int dupfd(unsigned int fd, unsigned int arg)/*复制文件句柄(即,使另一个新句柄也指向该文件结构项),arg为新句柄的最小值,对应文件结构项引用计数加1*/{ if (fd >= NR_OPEN || !current->filp[fd]) return -EBADF; if (arg >= NR_OPEN) return -EINVAL; while (arg < NR_OPEN) if (current->filp[arg]) arg++; else break; if (arg >= NR_OPEN) return -EMFILE; current->close_on_exec &= ~(1<<arg);/*复位该句柄位图,即以后执行exec时不会关闭该句柄所对应文件*/ (current->filp[arg] = current->filp[fd])->f_count++; return arg;}int sys_dup2(unsigned int oldfd, unsigned int newfd)/*复制文件句柄,若newfd非空,则先释放之,然后将原句柄复制到newfd*/{ sys_close(newfd);/*关闭文件(参数为文件句柄)*/ return dupfd(oldfd,newfd);/*复制文件句柄*/}int sys_dup(unsigned int fildes)/*复制文件句柄,新句柄为最小的空句柄*/{ return dupfd(fildes,0);}int sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)/*文件控制系统调用,fd为文件句柄,cmd为控制命令,arg对应不同cmd有不同含义(p465)*/{ struct file * filp; if (fd >= NR_OPEN || !(filp = current->filp[fd])) return -EBADF; switch (cmd) { case F_DUPFD:/*复制文件句柄*/ return dupfd(fd,arg); case F_GETFD:/*取文件句柄的close_on_exec标志*/ return (current->close_on_exec>>fd)&1; case F_SETFD:/*设置close_on_exec标志*/ if (arg&1) current->close_on_exec |= (1<<fd); else current->close_on_exec &= ~(1<<fd); return 0; case F_GETFL:/*取文件状态*/ return filp->f_flags; case F_SETFL:/*设置文件状态*/ filp->f_flags &= ~(O_APPEND | O_NONBLOCK); filp->f_flags |= arg & (O_APPEND | O_NONBLOCK); return 0; case F_GETLK: case F_SETLK: case F_SETLKW: return -1; default: return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -