_STD_BEGIN FILE *_freopen(const char *filename, const char *mode, FILE *fp, int large) { int oflag, fd, err; if ((oflag = __parse_oflag(mode)) == -1) { fclose(fp); errno = EINVAL; return(NULL); } _Lockfileatomic(fp); fflush(fp); if (filename == NULL) { if ((fd = openfd(_FD_NO(fp), oflag | large)) == -1) { err = (errno == EACCES) ? EBADF : errno; _Unlockfileatomic(fp); fclose(fp); errno = err; return(NULL); } /* * Attempt to keep the same file descriptor since the POSIX * spec says that freopen(NULL,...) does not need to close * the existing file descriptor. * * The filename case requires the old file descriptor to be * closed and a new file descriptor allocated as the lowest * numbered unused descriptor. */ if (fd != _FD_NO(fp) && dup2(fd, _FD_NO(fp)) != -1) { close(fd); fd = _FD_NO(fp); } } else { close(_FD_NO(fp)); if ((fd = open(filename, oflag | large, 0666)) == -1) { err = errno; _Unlockfileatomic(fp); fclose(fp); errno = err; return(NULL); } } _Locksyslock(_LOCK_STREAM); if (fp->_Mode & _MALBUF) free(fp->_Buf); fp->_Mode &= _MALFIL; __fpinit(fp, fd, oflag); _Unlockfileatomic(fp); _Unlocksyslock(_LOCK_STREAM); return(fp); }
int fcloseall(void) { FILE *fp; int again; _Locksyslock(_LOCK_STREAM); for(again = 1; again;) { again = 0; for(fp = _Files[0]; fp; fp = fp->_NextFile) { if(fp->_Mode & (_MOPENR | _MOPENW)) { _Unlocksyslock(_LOCK_STREAM); fclose(fp); again = 1; _Locksyslock(_LOCK_STREAM); break; } } } _Unlocksyslock(_LOCK_STREAM); return 0; }
int remove(const char *name) { /* Place a call directly into the PrimIO routine. * Note that since this doesn't use a File Descriptor we avoid using * the generalised FileI/O. * Without file/device-specific locks, PrimIO does its own locking. */ int result; if (!name) return -1; #if !_FILE_OP_LOCKS _Locksyslock(_LOCK_STREAM); #endif result = PRIMITIVE_REMOVE(name); #if !_FILE_OP_LOCKS _Unlocksyslock(_LOCK_STREAM); #endif return result; }