sysfile.c 6.93 KiB
#include "types.h"
#include "stat.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "traps.h"
#include "syscall.h"
#include "spinlock.h"
#include "buf.h"
#include "fs.h"
#include "fsvar.h"
#include "file.h"
#include "fcntl.h"
// Fetch the nth word-sized system call argument as a file descriptor
// and return both the descriptor and the corresponding struct file.
static int
argfd(int argno, int *pfd, struct file **pf)
{
int fd;
struct file *f;
if(argint(argno, &fd) < 0)
return -1;
if(fd < 0 || fd >= NOFILE || (f=cp->ofile[fd]) == 0)
return -1;
if(pfd)
*pfd = fd;
if(pf)
*pf = f;
return 0;
}
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
int fd;
for(fd = 0; fd < NOFILE; fd++){
if(cp->ofile[fd] == 0){
cp->ofile[fd] = f;
return fd;
}
}
return -1;
}
int
sys_read(void)
{
struct file *f;
int n;
char *cp;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
return -1;
return fileread(f, cp, n);
}
int
sys_write(void)
{
struct file *f;
int n;
char *cp;