示例#1
0
int FSEEK(register FILE *stream, OFFSET_TYPE offset, int whence)
{
#if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE)

	return fseeko64(stream, offset, whence);

#else

	__offmax_t pos = offset;
	int retval = -1;
	__STDIO_AUTO_THREADLOCK_VAR;

	if (((unsigned int) whence) > 2) {
		__set_errno(EINVAL);
	} else {
		__STDIO_AUTO_THREADLOCK(stream);

		__STDIO_STREAM_VALIDATE(stream);

		if ((!__STDIO_STREAM_IS_WRITING(stream)
			  || !__STDIO_COMMIT_WRITE_BUFFER(stream))
			&& ((whence != SEEK_CUR)
				 || (__stdio_adjust_position(stream, &pos) >= 0))
			&& (__SEEK(stream, &pos, whence) >= 0)
			) {

			/* Clear reading/writing modes, EOF, and ungots. */
			stream->__modeflags &=
				~(__MASK_READING|__FLAG_WRITING|__FLAG_EOF);

			/* Make sure all pointers are reset. */
			__STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
			__STDIO_STREAM_DISABLE_GETC(stream);
			__STDIO_STREAM_DISABLE_PUTC(stream);

			/* We reinitialize the mbstate object.  Doing so is
			 * implementation defined behavior. */
#ifdef __STDIO_MBSTATE
			__INIT_MBSTATE(&(stream->__state));
#endif
#ifdef __UCLIBC_HAS_WCHAR__
			stream->__ungot_width[0] = 0;
#endif

			retval = 0;
		}

		__STDIO_STREAM_VALIDATE(stream);

		__STDIO_AUTO_THREADUNLOCK(stream);
	}

	return retval;

#endif
}
int fflush_unlocked(register FILE *stream)
{
#ifdef __STDIO_BUFFERS

	int retval = 0;
#ifdef __UCLIBC_MJN3_ONLY__
#warning REMINDER: should probably define a modeflags type
#endif
	unsigned short bufmask = __FLAG_LBF;

#ifndef NDEBUG
	if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
		__STDIO_STREAM_VALIDATE(stream); /* debugging only */
	}
#endif

	if (stream == (FILE *) &_stdio_openlist) { /* Flush all lbf streams. */
		stream = NULL;
		bufmask = 0;
	}

	if (!stream) {				/* Flush all (lbf) writing streams. */

		__STDIO_OPENLIST_INC_USE;

		__STDIO_THREADLOCK_OPENLIST_ADD;
		stream = _stdio_openlist;
		__STDIO_THREADUNLOCK_OPENLIST_ADD;

		while(stream) {
			/* We only care about currently writing streams and do not want to
			 * block trying to obtain mutexes on non-writing streams. */
#warning fix for nonatomic
#warning unnecessary check if no threads
			if (__STDIO_STREAM_IS_WRITING(stream)) { /* ONLY IF ATOMIC!!! */
				__MY_STDIO_THREADLOCK(stream);
				/* Need to check again once we have the lock. */
				if (!(((stream->__modeflags | bufmask)
					   ^ (__FLAG_WRITING|__FLAG_LBF)
					   ) & (__FLAG_WRITING|__MASK_BUFMODE))
					) {
					if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
						__STDIO_STREAM_DISABLE_PUTC(stream);
						__STDIO_STREAM_CLEAR_WRITING(stream);
					} else {
						retval = EOF;
					}
				}
				__MY_STDIO_THREADUNLOCK(stream);
			}
			stream = stream->__nextopen;
		}

		__STDIO_OPENLIST_DEC_USE;

	} else if (__STDIO_STREAM_IS_WRITING(stream)) {
		if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
			__STDIO_STREAM_DISABLE_PUTC(stream);
			__STDIO_STREAM_CLEAR_WRITING(stream);
		} else {
			retval = EOF;
		}
	}
#if 0
	else if (stream->__modeflags & (__MASK_READING|__FLAG_READONLY)) {
		/* ANSI/ISO says behavior in this case is undefined but also says you
		 * shouldn't flush a stream you were reading from.  As usual, glibc
		 * caters to broken programs and simply ignores this. */
		__UNDEFINED_OR_NONPORTABLE;
		__STDIO_STREAM_SET_ERROR(stream);
		__set_errno(EBADF);
		retval = EOF;
	}
#endif

#ifndef NDEBUG
	if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
		__STDIO_STREAM_VALIDATE(stream); /* debugging only */
	}
#endif

	return retval;

#else  /* __STDIO_BUFFERS --------------------------------------- */

#ifndef NDEBUG
	if ((stream != NULL)
#ifdef __STDIO_HAS_OPENLIST
		&& (stream != (FILE *) &_stdio_openlist)
#endif
		) {
		__STDIO_STREAM_VALIDATE(stream); /* debugging only */
	}
#endif

#if 0
	if (stream && (stream->__modeflags & (__MASK_READING|__FLAG_READONLY))) {
		/* ANSI/ISO says behavior in this case is undefined but also says you
		 * shouldn't flush a stream you were reading from.  As usual, glibc
		 * caters to broken programs and simply ignores this. */
		__UNDEFINED_OR_NONPORTABLE;
		__STDIO_STREAM_SET_ERROR(stream);
		__set_errno(EBADF);
		return EOF;
	}
#endif

	return 0;
#endif /* __STDIO_BUFFERS */
}
示例#3
0
文件: _stdio.c 项目: OPSF/uClinux
/* We assume here that we are the only remaining thread. */
void attribute_hidden _stdio_term(void)
{
#if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
	register FILE *ptr;

#ifdef __UCLIBC_HAS_THREADS__
	/* First, make sure the open file list is unlocked.  If it was
	 * locked, then I suppose there is a chance that a pointer in the
	 * chain might be corrupt due to a partial store.
	 */
	__stdio_init_mutex(&_stdio_openlist_add_lock);
#warning check
#ifdef __STDIO_BUFFERS
	__stdio_init_mutex(&_stdio_openlist_del_lock);
#endif

	/* Next we need to worry about the streams themselves.  If a stream
	 * is currently locked, then it may be in an invalid state.  So we
	 * 'disable' it in case a custom stream is stacked on top of it.
	 * Then we reinitialize the locks.
	 */
	for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
		if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) {
			/* The stream is already locked, so we don't want to touch it.
			 * However, if we have custom streams, we can't just close it
			 * or leave it locked since a custom stream may be stacked
			 * on top of it.  So we do unlock it, while also disabling it.
			 */
			ptr->__modeflags = (__FLAG_READONLY|__FLAG_WRITEONLY);
			__STDIO_STREAM_DISABLE_GETC(ptr);
			__STDIO_STREAM_DISABLE_PUTC(ptr);
			__STDIO_STREAM_INIT_BUFREAD_BUFPOS(ptr);
		}

		ptr->__user_locking = 1; /* Set locking mode to "by caller". */
		__stdio_init_mutex(&ptr->__lock); /* Shouldn't be necessary, but... */
	}
#endif

	/* Finally, flush all writing streams and shut down all custom streams.
	 * NOTE: We assume that any stacking by custom streams is done on top
	 *       of streams previously allocated, and hence further down the
	 *       list.  Otherwise we have no way of knowing the order in which
	 *       to shut them down.
	 *       Remember that freopen() counts as a new allocation here, even
	 *       though the stream is reused.  That's because it moves the
	 *       stream to the head of the list.
	 */
	for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
#ifdef __STDIO_BUFFERS
		/* Write any pending buffered chars. */
		if (__STDIO_STREAM_IS_WRITING(ptr)) {
			__STDIO_COMMIT_WRITE_BUFFER(ptr);
		}
#endif
#ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
		/* Actually close all custom streams to perform any special cleanup. */
		if (ptr->__cookie != &ptr->__filedes) {
			__CLOSE(ptr);
		}
#endif
	}

#endif
}
示例#4
0
int __fputc_unlocked(int c, register FILE *stream)
{
	__STDIO_STREAM_VALIDATE(stream);

	/* First the fast path.  We're good to go if putc macro enabled. */
	if (__STDIO_STREAM_CAN_USE_BUFFER_ADD(stream)) {
		__STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
		return (unsigned char) c;
	}

	/* Next quickest... writing and narrow oriented, but macro
	 * disabled and/or buffer is full. */
	if (__STDIO_STREAM_IS_NARROW_WRITING(stream)
		|| !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW)
		) {
		if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)) {
			return (unsigned char) c;
		}

		if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
			/* The buffer is full and/or the stream is line buffered. */
			if (!__STDIO_STREAM_BUFFER_WAVAIL(stream) /* Buffer full? */
				&& __STDIO_COMMIT_WRITE_BUFFER(stream) /* Commit failed! */
				) {
				goto BAD;
			}
#ifdef __UCLIBC_MJN3_ONLY__
#warning CONSIDER: Should we fail if the commit fails but we now have room?
#endif

			__STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));

			if (IS_STD_STREAM(stream))
			{
				if( __STDIO_COMMIT_WRITE_BUFFER(stream))
			   	{
					/* Commit failed! */
					__STDIO_STREAM_BUFFER_UNADD(stream); /* Undo the write! */
					goto BAD;
				}
			}
			else if (__STDIO_STREAM_IS_LBF(stream)) {
				if ((((unsigned char) c) == '\n')
					&& __STDIO_COMMIT_WRITE_BUFFER(stream)) {
					/* Commit failed! */
					__STDIO_STREAM_BUFFER_UNADD(stream); /* Undo the write! */
					goto BAD;
				}
			}
		} else {
			/* NOTE: Do not try to save space by moving uc to the top of
			 * the file, as that dramaticly increases runtime. */
			unsigned char uc = (unsigned char) c;
			if (! __stdio_WRITE(stream, &uc, 1)) {
				goto BAD;
			}
		}
		return (unsigned char) c;
	}

 BAD:
	return EOF;
}