9 uint8_t fds
[MAX_FD
] = {0,0,0};
10 struct file
*files
[MAX_FD
] = {NULL
, };
12 static int get_unused_fd(void)
16 for (i
= 0; i
< MAX_FD
; i
++) {
27 int sys_open(const char *pathname
, uint32_t flags
)
29 struct inode
*inode
= namei(pathname
, flags
);
33 if (IS_ERR_OR_NULL(inode
))
34 return inode
? PTR_ERR(inode
) : -ENOENT
;
36 FS_DEBUG("%s: namei returned successfully!\n", __FUNCTION__
);
38 file
= malloc(sizeof(*file
));
42 file
->fs
= inode
->i_fs
;
44 file
->f_op
= inode
->i_fop
;
48 printk("You opened two much files!\n");
57 static inline struct file
*fget(int fd
)
62 int sys_read(int fd
, void *buf
, int size
)
70 if (fd
< 0 || fd
> MAX_FD
)
78 bytes_need
= MIN(size
, inode
->i_size
- file
->offset
);
79 blocks
= (bytes_need
+ inode
->i_blksize
- 1) >> file
->fs
->block_shift
;
81 bytes_read
= file
->f_op
->read(file
, buf
, blocks
);
82 bytes_read
= MIN(bytes_need
, bytes_read
);
84 memmove(buf
, buf
+ (file
->offset
& (inode
->i_blksize
- 1)), bytes_read
);
85 file
->offset
+= bytes_read
;
91 int sys_write(int fd
, void *buf
, int size
)
97 if (fd
< 0 || fd
> MAX_FD
)
103 blocks
= (size
+ file
->inode
->i_blksize
- 1) >> file
->fs
->block_shift
;
105 bytes_written
= file
->f_op
->write(file
, buf
, blocks
);
106 bytes_written
= MIN(size
, bytes_written
);
107 if (bytes_written
> 0)
108 file
->offset
+= bytes_written
;
110 return bytes_written
;
113 int sys_close(int fd
)
117 if (fd
< 0 || fd
> MAX_FD
)
124 free_inode(file
->inode
);
131 int sys_lseek(int fd
, int off
, int mode
)
135 if (fd
< 0 || fd
> MAX_FD
)
142 if (mode
== SEEK_CUR
)
144 else if (mode
== SEEK_END
)
145 file
->offset
= file
->inode
->i_size
+ off
;
146 else if (mode
== SEEK_SET
)