// fd.c
struct stat;
-void fd_init(void);
-int fd_ualloc(void);
-struct file* fd_alloc(void);
-void fd_close(struct file*);
-int fd_read(struct file*, char*, int n);
-int fd_write(struct file*, char*, int n);
-int fd_stat(struct file*, struct stat*);
-void fd_incref(struct file*);
+void fileinit(void);
+int fdalloc(void);
+struct file* filealloc(void);
+void fileclose(struct file*);
+int fileread(struct file*, char*, int n);
+int filewrite(struct file*, char*, int n);
+int filestat(struct file*, struct stat*);
+void fileincref(struct file*);
// ide.c
void ide_init(void);
struct file file[NFILE];
void
-fd_init(void)
+fileinit(void)
{
initlock(&fd_table_lock, "fd_table");
}
// Allocate a file descriptor number for curproc.
int
-fd_ualloc(void)
+fdalloc(void)
{
int fd;
struct proc *p = curproc[cpu()];
// Allocate a file descriptor structure
struct file*
-fd_alloc(void)
+filealloc(void)
{
int i;
// Write to file descriptor;
// addr is a kernel address, pointing into some process's p->mem.
int
-fd_write(struct file *fd, char *addr, int n)
+filewrite(struct file *fd, char *addr, int n)
{
if(fd->writable == 0)
return -1;
iunlock(fd->ip);
return r;
} else {
- panic("fd_write");
+ panic("filewrite");
return -1;
}
}
// Read from file descriptor.
int
-fd_read(struct file *fd, char *addr, int n)
+fileread(struct file *fd, char *addr, int n)
{
if(fd->readable == 0)
return -1;
iunlock(fd->ip);
return cc;
} else {
- panic("fd_read");
+ panic("fileread");
return -1;
}
}
// Close file descriptor.
void
-fd_close(struct file *fd)
+fileclose(struct file *fd)
{
acquire(&fd_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED)
- panic("fd_close");
+ panic("fileclose");
if(--fd->ref == 0){
struct file dummy = *fd;
} else if(dummy.type == FD_FILE){
idecref(dummy.ip);
} else {
- panic("fd_close");
+ panic("fileclose");
}
} else {
release(&fd_table_lock);
// Get metadata about file descriptor.
int
-fd_stat(struct file *fd, struct stat *st)
+filestat(struct file *fd, struct stat *st)
{
if(fd->type == FD_FILE){
ilock(fd->ip);
// Increment file descriptor reference count.
void
-fd_incref(struct file *fd)
+fileincref(struct file *fd)
{
acquire(&fd_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED)
- panic("fd_incref");
+ panic("fileincref");
fd->ref++;
release(&fd_table_lock);
}
kinit(); // physical memory allocator
tvinit(); // trap vectors
idtinit(); // this CPU's interrupt descriptor table
- fd_init();
+ fileinit();
iinit(); // i-node table
// initialize process 0
*fd1 = *fd2 = 0;
struct pipe *p = 0;
- if((*fd1 = fd_alloc()) == 0)
+ if((*fd1 = filealloc()) == 0)
goto oops;
- if((*fd2 = fd_alloc()) == 0)
+ if((*fd2 = filealloc()) == 0)
goto oops;
if((p = (struct pipe*) kalloc(PAGE)) == 0)
goto oops;
kfree((char*) p, PAGE);
if(*fd1){
(*fd1)->type = FD_NONE;
- fd_close(*fd1);
+ fileclose(*fd1);
}
if(*fd2){
(*fd2)->type = FD_NONE;
- fd_close(*fd2);
+ fileclose(*fd2);
}
return -1;
}
for(i = 0; i < NOFILE; i++){
np->ofile[i] = p->ofile[i];
if(np->ofile[i])
- fd_incref(np->ofile[i]);
+ fileincref(np->ofile[i]);
}
np->cwd = p->cwd;
// Close all open files.
for(fd = 0; fd < NOFILE; fd++){
if(cp->ofile[fd]){
- fd_close(cp->ofile[fd]);
+ fileclose(cp->ofile[fd]);
cp->ofile[fd] = 0;
}
}
if(pipe_alloc(&rfd, &wfd) < 0)
goto oops;
- if((f1 = fd_ualloc()) < 0)
+ if((f1 = fdalloc()) < 0)
goto oops;
p->ofile[f1] = rfd;
- if((f2 = fd_ualloc()) < 0)
+ if((f2 = fdalloc()) < 0)
goto oops;
p->ofile[f2] = wfd;
if(fetcharg(0, &fdp) < 0)
oops:
if(rfd)
- fd_close(rfd);
+ fileclose(rfd);
if(wfd)
- fd_close(wfd);
+ fileclose(wfd);
if(f1 >= 0)
p->ofile[f1] = 0;
if(f2 >= 0)
if(addr + n > p->sz)
return -1;
- ret = fd_write(p->ofile[fd], p->mem + addr, n);
+ ret = filewrite(p->ofile[fd], p->mem + addr, n);
return ret;
}
return -1;
if(addr + n > p->sz)
return -1;
- ret = fd_read(p->ofile[fd], p->mem + addr, n);
+ ret = fileread(p->ofile[fd], p->mem + addr, n);
return ret;
}
return -1;
if(p->ofile[fd] == 0)
return -1;
- fd_close(p->ofile[fd]);
+ fileclose(p->ofile[fd]);
p->ofile[fd] = 0;
return 0;
}
return -1;
}
- if((fd = fd_alloc()) == 0){
+ if((fd = filealloc()) == 0){
iput(ip);
return -1;
}
- if((ufd = fd_ualloc()) < 0){
+ if((ufd = fdalloc()) < 0){
iput(ip);
- fd_close(fd);
+ fileclose(fd);
return -1;
}
return -1;
if(addr + sizeof(struct stat) > cp->sz)
return -1;
- r = fd_stat(cp->ofile[fd], (struct stat*)(cp->mem + addr));
+ r = filestat(cp->ofile[fd], (struct stat*)(cp->mem + addr));
return r;
}
return -1;
if(cp->ofile[fd] == 0)
return -1;
- if((ufd1 = fd_ualloc()) < 0)
+ if((ufd1 = fdalloc()) < 0)
return -1;
cp->ofile[ufd1] = cp->ofile[fd];
- fd_incref(cp->ofile[ufd1]);
+ fileincref(cp->ofile[ufd1]);
return ufd1;
}