📄 filedes.c
字号:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/fs/filedes.c
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22200 /* This file contains the procedures that manipulate file descriptors.
22201 *
22202 * The entry points into this file are
22203 * get_fd: look for free file descriptor and free filp slots
22204 * get_filp: look up the filp entry for a given file descriptor
22205 * find_filp: find a filp slot that points to a given inode
22206 */
22207
22208 #include "fs.h"
22209 #include "file.h"
22210 #include "fproc.h"
22211 #include "inode.h"
22212
22213 /*===========================================================================*
22214 * get_fd *
22215 *===========================================================================*/
22216 PUBLIC int get_fd(start, bits, k, fpt)
22217 int start; /* start of search (used for F_DUPFD) */
22218 mode_t bits; /* mode of the file to be created (RWX bits) */
22219 int *k; /* place to return file descriptor */
22220 struct filp **fpt; /* place to return filp slot */
22221 {
22222 /* Look for a free file descriptor and a free filp slot. Fill in the mode word
22223 * in the latter, but don't claim either one yet, since the open() or creat()
22224 * may yet fail.
22225 */
22226
22227 register struct filp *f;
22228 register int i;
22229
22230 *k = -1; /* we need a way to tell if file desc found */
22231
22232 /* Search the fproc fp_filp table for a free file descriptor. */
22233 for (i = start; i < OPEN_MAX; i++) {
22234 if (fp->fp_filp[i] == NIL_FILP) {
22235 /* A file descriptor has been located. */
22236 *k = i;
22237 break;
22238 }
22239 }
22240
22241 /* Check to see if a file descriptor has been found. */
22242 if (*k < 0) return(EMFILE); /* this is why we initialized k to -1 */
22243
22244 /* Now that a file descriptor has been found, look for a free filp slot. */
22245 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
22246 if (f->filp_count == 0) {
22247 f->filp_mode = bits;
22248 f->filp_pos = 0L;
22249 f->filp_flags = 0;
22250 *fpt = f;
22251 return(OK);
22252 }
22253 }
22254
22255 /* If control passes here, the filp table must be full. Report that back. */
22256 return(ENFILE);
22257 }
22260 /*===========================================================================*
22261 * get_filp *
22262 *===========================================================================*/
22263 PUBLIC struct filp *get_filp(fild)
22264 int fild; /* file descriptor */
22265 {
22266 /* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
22267
22268 err_code = EBADF;
22269 if (fild < 0 || fild >= OPEN_MAX ) return(NIL_FILP);
22270 return(fp->fp_filp[fild]); /* may also be NIL_FILP */
22271 }
22274 /*===========================================================================*
22275 * find_filp *
22276 *===========================================================================*/
22277 PUBLIC struct filp *find_filp(rip, bits)
22278 register struct inode *rip; /* inode referred to by the filp to be found */
22279 Mode_t bits; /* mode of the filp to be found (RWX bits) */
22280 {
22281 /* Find a filp slot that refers to the inode 'rip' in a way as described
22282 * by the mode bit 'bits'. Used for determining whether somebody is still
22283 * interested in either end of a pipe. Also used when opening a FIFO to
22284 * find partners to share a filp field with (to shared the file position).
22285 * Like 'get_fd' it performs its job by linear search through the filp table.
22286 */
22287
22288 register struct filp *f;
22289
22290 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
22291 if (f->filp_count != 0 && f->filp_ino == rip && (f->filp_mode & bits)){
22292 return(f);
22293 }
22294 }
22295
22296 /* If control passes here, the filp wasn't there. Report that back. */
22297 return(NIL_FILP);
22298 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -