/* * Internal routine to determine `proper' buffering for a file. */ int __swhatbuf(FILE *fp, size_t *bufsize, int *couldbetty) { struct stat st; _DIAGASSERT(fp != NULL); _DIAGASSERT(bufsize != NULL); _DIAGASSERT(couldbetty != NULL); if (__sfileno(fp) == -1 || fstat(__sfileno(fp), &st) < 0) { *couldbetty = 0; *bufsize = BUFSIZ; return __SNPT; } /* could be a tty iff it is a character device */ *couldbetty = S_ISCHR(st.st_mode); if (st.st_blksize == 0) { *bufsize = BUFSIZ; return __SNPT; } /* * Optimise fseek() only if it is a regular file. (The test for * __sseek is mainly paranoia.) It is safe to set _blksize * unconditionally; it will only be used if __SOPT is also set. */ *bufsize = st.st_blksize; fp->_blksize = st.st_blksize; return (st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ? __SOPT : __SNPT; }
/* * Allocate a file buffer, or switch to unbuffered I/O. * Per the ANSI C standard, ALL tty devices default to line buffered. * * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek * optimisation) right after the fstat() that finds the buffer size. */ void __smakebuf(FILE *fp) { void *p; int flags; size_t size; int couldbetty; _DIAGASSERT(fp != NULL); if (fp->_flags & __SNBF) { fp->_bf._base = fp->_p = fp->_nbuf; fp->_bf._size = 1; return; } flags = __swhatbuf(fp, &size, &couldbetty); if ((p = malloc(size)) == NULL) { fp->_flags |= __SNBF; fp->_bf._base = fp->_p = fp->_nbuf; fp->_bf._size = 1; return; } __cleanup = _cleanup; flags |= __SMBF; fp->_bf._base = fp->_p = p; _DIAGASSERT(__type_fit(int, size)); fp->_bf._size = (int)size; if (couldbetty && isatty(__sfileno(fp))) flags |= __SLBF; fp->_flags |= flags; }
int fileno(FILE* fp) { int fd; FLOCKFILE(fp); fd = __sfileno(fp); FUNLOCKFILE(fp); return (fd); }
int fileno(FILE *fp) { int ret; FLOCKFILE(fp); ret = __sfileno(fp); FUNLOCKFILE(fp); return (ret); }
int fileno ( FILE * fp /* stream */ ) { if (OBJ_VERIFY (fp, fpClassId) != OK) return (ERROR); return (__sfileno(fp)); }
int _fileno(FILE *fp) { int r; _DIAGASSERT(fp != NULL); FLOCKFILE(fp); r = __sfileno(fp); FUNLOCKFILE(fp); return r; }
int fileno_unlocked (FILE * f) { int result; CHECK_INIT (_REENT, f); if (f->_flags) result = __sfileno (f); else { result = -1; __errno_r(_REENT) = EBADF; } return result; }
/** * @brief Maps a stream pointer to a file descriptor. * * @details Returns the integer file descriptor associated * with the stream pointed to by @p f. * * @return Returns the integer value of the file descriptor * associated with @p f. Otherwise, the value -1 is returned * and errno set to indicate the error. */ int fileno(FILE * f) { int result; CHECK_INIT (_REENT, f); _newlib_flockfile_start (f); if (f->_flags) result = __sfileno (f); else { result = -1; _REENT->_errno = EBADF; } _newlib_flockfile_end (f); return result; }
/* * Allocate a file buffer, or switch to unbuffered I/O. * Per the ANSI C standard, ALL tty devices default to line buffered. * * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek * optimisation) right after the fstat() that finds the buffer size. */ void __smakebuf(FILE *fp) { void *p; int flags; size_t size; int couldbetty; _DIAGASSERT(fp != NULL); if (fp->_flags & __SNBF) goto unbuf; flags = __swhatbuf(fp, &size, &couldbetty); if ((fp->_flags & (__SLBF|__SNBF|__SMBF)) == 0 && fp->_cookie == fp && fp->_file >= 0) { flags |= __senvbuf(fp, &size, &couldbetty); if (flags & __SNBF) goto unbuf; } if ((p = malloc(size)) == NULL) goto unbuf; __cleanup = _cleanup; flags |= __SMBF; fp->_bf._base = fp->_p = p; _DIAGASSERT(__type_fit(int, size)); fp->_bf._size = (int)size; if (couldbetty && isatty(__sfileno(fp))) flags |= __SLBF; fp->_flags |= flags; return; unbuf: fp->_flags |= __SNBF; fp->_bf._base = fp->_p = fp->_nbuf; fp->_bf._size = 1; }
/* * Seek the given file to the given offset. * `Whence' must be one of the three SEEK_* macros. */ int fseeko(FILE *fp, off_t offset, int whence) { fpos_t (*seekfn)(void *, fpos_t, int); fpos_t target, curoff; size_t n; struct stat st; int havepos; /* make sure stdio is set up */ if (!__sdidinit) __sinit(); /* * Have to be able to seek. */ if ((seekfn = fp->_seek) == NULL || isatty(__sfileno(fp))) { __sseterr(fp, ESPIPE); /* historic practice */ return (EOF); } /* * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. * After this, whence is either SEEK_SET or SEEK_END. */ FLOCKFILE(fp); switch (whence) { case SEEK_CUR: /* * In order to seek relative to the current stream offset, * we have to first find the current stream offset a la * ftell (see ftell for details). */ __sflush(fp); /* may adjust seek offset on append stream */ if (fp->_flags & __SOFF) curoff = fp->_offset; else { curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); if (curoff == (fpos_t)-1) { FUNLOCKFILE(fp); return (EOF); } } if (fp->_flags & __SRD) { curoff -= fp->_r; if (HASUB(fp)) curoff -= fp->_ur; } else if (fp->_flags & __SWR && fp->_p != NULL) curoff += fp->_p - fp->_bf._base; offset += curoff; whence = SEEK_SET; havepos = 1; break; case SEEK_SET: case SEEK_END: curoff = 0; /* XXX just to keep gcc quiet */ havepos = 0; break; default: FUNLOCKFILE(fp); __sseterr(fp, EINVAL); return (EOF); } /* * Can only optimise if: * reading (and not reading-and-writing); * not unbuffered; and * this is a `regular' Unix file (and hence seekfn==__sseek). * We must check __NBF first, because it is possible to have __NBF * and __SOPT both set. */ if (fp->_bf._base == NULL) __smakebuf(fp); if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) goto dumb; if ((fp->_flags & __SOPT) == 0) { if (seekfn != __sseek || fp->_file < 0 || __libc_fstat(fp->_file, &st) || (st.st_mode & S_IFMT) != S_IFREG) { fp->_flags |= __SNPT; goto dumb; } fp->_blksize = st.st_blksize; fp->_flags |= __SOPT; } /* * We are reading; we can try to optimise. * Figure out where we are going and where we are now. */ if (whence == SEEK_SET) target = offset; else { if (__libc_fstat(fp->_file, &st)) goto dumb; target = st.st_size + offset; } if (!havepos) { if (fp->_flags & __SOFF) curoff = fp->_offset; else { curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); if (curoff == POS_ERR) goto dumb; } curoff -= fp->_r; if (HASUB(fp)) curoff -= fp->_ur; } /* * Compute the number of bytes in the input buffer (pretending * that any ungetc() input has been discarded). Adjust current * offset backwards by this count so that it represents the * file offset for the first byte in the current input buffer. */ if (HASUB(fp)) { curoff += fp->_r; /* kill off ungetc */ n = fp->_up - fp->_bf._base; curoff -= n; n += fp->_ur; } else { n = fp->_p - fp->_bf._base; curoff -= n; n += fp->_r; } /* * If the target offset is within the current buffer, * simply adjust the pointers, clear EOF, undo ungetc(), * and return. (If the buffer was modified, we have to * skip this; see fgetln.c.) */ if ((fp->_flags & __SMOD) == 0 && target >= curoff && target < curoff + n) { int o = target - curoff; fp->_p = fp->_bf._base + o; fp->_r = n - o; if (HASUB(fp)) FREEUB(fp); fp->_flags &= ~__SEOF; FUNLOCKFILE(fp); return (0); } /* * The place we want to get to is not within the current buffer, * but we can still be kind to the kernel copyout mechanism. * By aligning the file offset to a block boundary, we can let * the kernel use the VM hardware to map pages instead of * copying bytes laboriously. Using a block boundary also * ensures that we only read one block, rather than two. */ curoff = target & ~(fp->_blksize - 1); if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) goto dumb; fp->_r = 0; fp->_p = fp->_bf._base; if (HASUB(fp)) FREEUB(fp); fp->_flags &= ~__SEOF; n = target - curoff; if (n) { if (__srefill(fp) || fp->_r < n) goto dumb; fp->_p += n; fp->_r -= n; } FUNLOCKFILE(fp); return (0); /* * We get here if we cannot optimise the seek ... just * do it. Allow the seek function to change fp->_bf._base. */ dumb: if (__sflush(fp) || (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) { FUNLOCKFILE(fp); return (EOF); } /* success: clear EOF indicator and discard ungetc() data */ if (HASUB(fp)) FREEUB(fp); fp->_p = fp->_bf._base; fp->_r = 0; /* fp->_w = 0; */ /* unnecessary (I think...) */ fp->_flags &= ~__SEOF; FUNLOCKFILE(fp); return (0); }
int fileno_unlocked(FILE *fp) { return (__sfileno(fp)); }
DESCRIPTION You can use <<fileno>> to return the file descriptor identified by <[fp]>. RETURNS <<fileno>> returns a non-negative integer when successful. If <[fp]> is not an open stream, <<fileno>> returns -1. PORTABILITY <<fileno>> is not part of ANSI C. POSIX requires <<fileno>>. Supporting OS subroutines required: none. */ #include <_ansi.h> #include <stdio.h> #include "local.h" int _DEFUN(fileno, (f), FILE * f) { int result; CHECK_INIT (_REENT); _flockfile (f); result = __sfileno (f); _funlockfile (f); return result; }
int pd_fileno_unlocked_p(FILEpd *fp) { return (__sfileno(fp)); }
int fileno(FILE *f) { CHECK_INIT(f); return __sfileno(f); }
/* * Re-direct an existing, open (probably) file to some other file. * ANSI is written such that the original file gets closed if at * all possible, no matter what. */ FILE * freopen(const char *file, const char *mode, FILE *fp) { int f; int flags, isopen, oflags, sverrno, wantfd; _DIAGASSERT(file != NULL); _DIAGASSERT(mode != NULL); _DIAGASSERT(fp != NULL); if ((flags = __sflags(mode, &oflags)) == 0) { (void) fclose(fp); return NULL; } if (!__sdidinit) __sinit(); /* * There are actually programs that depend on being able to "freopen" * descriptors that weren't originally open. Keep this from breaking. * Remember whether the stream was open to begin with, and which file * descriptor (if any) was associated with it. If it was attached to * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) * should work. This is unnecessary if it was not a Unix file. */ if (fp->_flags == 0) { fp->_flags = __SEOF; /* hold on to it */ isopen = 0; wantfd = -1; } else { /* flush the stream; ANSI doesn't require this. */ if (fp->_flags & __SWR) (void)__sflush(fp); /* if close is NULL, closing is a no-op, hence pointless */ isopen = fp->_close != NULL; if ((wantfd = __sfileno(fp)) == -1 && isopen) { (void) (*fp->_close)(fp->_cookie); isopen = 0; } } /* Get a new descriptor to refer to the new file. */ f = open(file, oflags, DEFFILEMODE); if (f < 0 && isopen) { /* If out of fd's close the old one and try again. */ if (errno == ENFILE || errno == EMFILE) { (void) (*fp->_close)(fp->_cookie); isopen = 0; f = open(file, oflags, DEFFILEMODE); } } sverrno = errno; /* * Finish closing fp. Even if the open succeeded above, we cannot * keep fp->_base: it may be the wrong size. This loses the effect * of any setbuffer calls, but stdio has always done this before. */ if (isopen && f != wantfd) (void) (*fp->_close)(fp->_cookie); if (fp->_flags & __SMBF) free((char *)fp->_bf._base); fp->_w = 0; fp->_r = 0; fp->_p = NULL; fp->_bf._base = NULL; fp->_bf._size = 0; fp->_lbfsize = 0; if (HASUB(fp)) FREEUB(fp); WCIO_FREE(fp); _UB(fp)._size = 0; FREELB(fp); if (f < 0) { /* did not get it after all */ fp->_flags = 0; /* set it free */ errno = sverrno; /* restore in case _close clobbered */ return NULL; } if (oflags & O_NONBLOCK) { struct stat st; if (fstat(f, &st) == -1) { sverrno = errno; (void)close(f); errno = sverrno; return NULL; } if (!S_ISREG(st.st_mode)) { (void)close(f); errno = EFTYPE; return NULL; } } /* * If reopening something that was open before on a real file, try * to maintain the descriptor. Various C library routines (perror) * assume stderr is always fd STDERR_FILENO, even if being freopen'd. */ if (wantfd >= 0 && f != wantfd) { if (dup2(f, wantfd) >= 0) { (void) close(f); f = wantfd; } } /* * File descriptors are a full int, but _file is only a short. * If we get a valid file descriptor that is greater or equal to * USHRT_MAX, then the fd will get sign-extended into an * invalid file descriptor. Handle this case by failing the * open. (We treat the short as unsigned, and special-case -1). */ if (f >= USHRT_MAX) { (void)close(f); errno = EMFILE; return NULL; } fp->_flags = flags; fp->_file = f; fp->_cookie = fp; fp->_read = __sread; fp->_write = __swrite; fp->_seek = __sseek; fp->_close = __sclose; /* * When reopening in append mode, even though we use O_APPEND, * we need to seek to the end so that ftell() gets the right * answer. If the user then alters the seek pointer, or * the file extends, this will fail, but there is not much * we can do about this. (We could set __SAPP and check in * fseek and ftell.) */ if (oflags & O_APPEND) (void) __sseek((void *)fp, (off_t)0, SEEK_END); return fp; }