⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 open.c

📁 一个用于学习的操作系统
💻 C
字号:
/*
 *  linux/fs/open.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

//#include <fairysky/vfs.h>
#include <fairysky/types.h>
//#include <fairysky/utime.h>
#include <fairysky/errno.h>
#include <fairysky/fcntl.h>
//#include <fairysky/stat.h>
#include <fairysky/string.h>
#include <fairysky/scheduler.h>
#include <fairysky/kernel.h>
#include <fairysky/signal.h>

#include <asm/segment.h>

struct file_operations * chrdev_fops = NULL;

struct file *get_empty_file_pointer(void);

/*临时用于tty的数据*/
struct inode chdev_op_inode;
struct inode_operations chdev_inode_op;


int sys_open(const char *filename, int flag, int mode)
{
    struct inode *inode;
    struct file *f;
    int i, fd;

    for (fd = 0 ; fd < NR_OPEN ; fd++) {
        if (!current->file_pointer[fd]) {
            break;
        }
    }
    if (fd >= NR_OPEN) {
        return -EMFILE;
    }
    current->close_on_exec &= ~(1 << fd);
    f = get_empty_file_pointer();
    if (!f) {
        return -ENFILE;
    }
    current->file_pointer[fd] = f;
    /*
    if ((i = open_namei(filename, flag, mode, &inode)) < 0) {
        current->file_pointer[fd] = NULL;
        f->f_count--;
        return i;
    }*/
    if (strcmp("/dev/tty", filename) == 0) {
        inode = &chdev_op_inode;

        inode->i_op = &chdev_inode_op;
        inode->i_op->default_file_ops = chrdev_fops;
    } else {
        panic("must be tty");
    }


    f->f_mode = "\001\002\003\000"[flag & O_ACCMODE];
    f->f_flags = flag;
    f->f_inode = inode;
    f->f_pos = 0;
    f->f_op = NULL;
    if (inode->i_op) {
        f->f_op = inode->i_op->default_file_ops;
    }
    if (f->f_op && f->f_op->open) {
        if (i = f->f_op->open(inode, f)) {
            //iput(inode);
            f->f_count--;
            current->file_pointer[fd] = NULL;
            return i;
        }
    }
    return (fd);
}

int sys_creat(const char *pathname, int mode)
{
    return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
}

int sys_close(unsigned int fd)
{
    struct file *file_pointer;
    struct inode *inode;

    if (fd >= NR_OPEN) {
        return -EINVAL;
    }
    current->close_on_exec &= ~(1 << fd);
    if (!(file_pointer = current->file_pointer[fd])) {
        return -EINVAL;
    }
    current->file_pointer[fd] = NULL;
    if (file_pointer->f_count == 0) {
        printk("Close: file count is 0\n");
        return 0;
    }
    if (file_pointer->f_count > 1) {
        file_pointer->f_count--;
        return 0;
    }
    inode = file_pointer->f_inode;
    if (file_pointer->f_op && file_pointer->f_op->release) {
        file_pointer->f_op->release(inode, file_pointer);
    }
    file_pointer->f_count--;
    file_pointer->f_inode = NULL;
    //iput(inode);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -