- * Convert a user supplied
- * file descriptor into a pointer
- * to a file structure.
- * Only task is to check range
- * of the descriptor.
- */
-struct file *
-getf(f)
-register int f;
-{
- register struct file *fp;
-
- if ((unsigned)f >= NOFILE || (fp = u.u_ofile[f]) == NULL) {
- u.u_error = EBADF;
- return (NULL);
- }
- return (fp);
-}
-
-/*
- * Internal form of close.
- * Decrement reference count on
- * file structure.
- * Also make sure the pipe protocol
- * does not constipate.
- *
- * Decrement reference count on the inode following
- * removal to the referencing file structure.
- * Call device handler on last close.
- */
-closef(fp)
-register struct file *fp;
-{
- register struct inode *ip;
- register struct mount *mp;
- int flag, mode;
- dev_t dev;
- register int (*cfunc)();
-
- if(fp == NULL)
- return;
- if (fp->f_count > 1) {
- fp->f_count--;
- return;
- }
- ip = fp->f_inode;
- flag = fp->f_flag;
- dev = (dev_t)ip->i_un.i_rdev;
- mode = ip->i_mode & IFMT;
-
- plock(ip);
- fp->f_count = 0;
- if(flag & FPIPE) {
- ip->i_mode &= ~(IREAD|IWRITE);
- wakeup((caddr_t)ip+1);
- wakeup((caddr_t)ip+2);
- }
- iput(ip);
-
- switch(mode) {
-
- case IFCHR:
- case IFMPC:
- cfunc = cdevsw[major(dev)].d_close;
- break;
-
- case IFBLK:
- /*
- * We don't want to really close the device if it is mounted
- */
- for (mp = mount; mp < &mount[NMOUNT]; mp++)
- if (mp->m_bufp != NULL && mp->m_dev == dev)
- return;
- case IFMPB:
- cfunc = bdevsw[major(dev)].d_close;
- break;
- default:
- return;
- }
-
- if ((flag & FMP) == 0) {
- for(fp=file; fp < fileNFILE; fp++)
- if (fp->f_count && (ip=fp->f_inode)->i_un.i_rdev==dev &&
- (ip->i_mode&IFMT) == mode)
- return;
- if (mode == IFBLK) {
- /*
- * on last close of a block device (that isn't mounted)
- * we must invalidate any in core blocks
- */
- bflush(dev);
- binval(dev);
- }
- }
- (*cfunc)(dev, flag, fp);
-}
-
-/*
- * openi called to allow handler