/* Return a new, zeroed, stream. You must set its cookie and io_mode. The first operation will give it a buffer unless you do. It will also give it the default functions unless you set the `seen' flag. Returns NULL if a stream can't be created. */ FILE * __newstream (void) { register FILE *stream; stream = __stdio_head; while (__validfp (stream)) stream = stream->__next; if (stream == NULL) { /* None to reuse. */ stream = (FILE *) malloc (sizeof (FILE)); if (stream == NULL) return NULL; stream->__next = __stdio_head; __stdio_head = stream; } __invalidate (stream); stream->__magic = _IOMAGIC; stream->__offset = (fpos_t) -1; stream->__target = (fpos_t) -1; stream->__flags = 0; return stream; }
int fseek(FILE* fp, long int offset, int whence) { if (!__validfp(fp)) { errno = EBADF; return EOF; } return (((streambuf*)fp)->seekoff(offset, (_seek_dir)whence) == EOF) ? EOF : 0; }
extern "C" char* fgets(char* s, int n, FILE* stream) { if (!__validfp(stream)) return NULL; long len = ((streambuf*)stream)->sgetline(s, n, '\n', 1); if (len <= 0) return NULL; return s; }
/* Close a stream. */ int __fcloseall () { /* Close all streams. */ register FILE *f; for (f = __stdio_head; f != NULL; f = f->__next) if (__validfp(f)) (void) fclose(f); return 0; }
extern "C" int ferror(FILE* fp) { if (!__validfp(fp)) return EOF; return (fp->_flags & _S_ERR_SEEN) != 0; }