Esempio n. 1
0
errno_t __cdecl _tfreopen_helper(
    FILE** pfile,
    const _TSCHAR* filename,
    const _TSCHAR* mode,
    FILE* str,
    int shflag
) {
    REG1 FILE* stream;
    _VALIDATE_RETURN_ERRCODE((pfile != NULL), EINVAL);
    *pfile = NULL;
    _VALIDATE_RETURN_ERRCODE((filename != NULL), EINVAL);
    _VALIDATE_RETURN_ERRCODE((mode != NULL), EINVAL);
    _VALIDATE_RETURN_ERRCODE((str != NULL), EINVAL);

    /* We deliberately don't hard-validate for emptry strings here. All other invalid
    path strings are treated as runtime errors by the inner code in _open and openfile.
    This is also the appropriate treatment here. Since fopen is the primary access point
    for file strings it might be subjected to direct user input and thus must be robust to
    that rather than aborting. The CRT and OS do not provide any other path validator (because
    WIN32 doesn't allow such things to exist in full generality).
    */
    if (*filename == _T('\0')) {
        errno = EINVAL;
        return errno;
    }

    /* Init stream pointer */
    stream = str;
    _lock_str(stream);

    __try {
        /* If the stream is in use, try to close it. Ignore possible
         * error (ANSI 4.9.5.4). */
        if (inuse(stream)) {
            _fclose_nolock(stream);
        }

        stream->_ptr = stream->_base = NULL;
        stream->_cnt = stream->_flag = 0;
#ifdef _UNICODE
        *pfile = _wopenfile(filename, mode, shflag, stream);
#else  /* _UNICODE */
        *pfile = _openfile(filename, mode, shflag, stream);
#endif  /* _UNICODE */
    } __finally {
        _unlock_str(stream);
    }

    if (*pfile) {
        return 0;
    }

    return errno;
}
Esempio n. 2
0
FILE * __cdecl _tfsopen (
	const _TSCHAR *file,
	const _TSCHAR *mode
#ifndef _POSIX_
	,int shflag
#endif
	)
{
	REG1 FILE *stream;
	REG2 FILE *retval;

	_ASSERTE(file != NULL);
	_ASSERTE(*file != _T('\0'));
	_ASSERTE(mode != NULL);
	_ASSERTE(*mode != _T('\0'));

	/* Get a free stream */
	/* [NOTE: _getstream() returns a locked stream.] */

	if ((stream = _getstream()) == NULL)
		return(NULL);

	/* open the stream */
#ifdef _POSIX_
#ifdef _UNICODE
	retval = _wopenfile(file,mode, stream);
#else
	retval = _openfile(file,mode, stream);
#endif
#else
#ifdef _UNICODE
	retval = _wopenfile(file,mode,shflag,stream);
#else
	retval = _openfile(file,mode,shflag,stream);
#endif
#endif

	/* unlock stream and return. */
	_unlock_str(stream);
	return(retval);
}
Esempio n. 3
0
_CODE_ACCESS FILE *fopen(const char *_fname, const char *_mode)
{
    FILE *f;

    /*-----------------------------------------------------------------------*/
    /* This is a critical section because search_fp looks for a new file     */
    /* slot in the global table _ftable.				     */
    /*-----------------------------------------------------------------------*/
    __TI_resource_lock(__TI_LOCK_FILE_TBL);
    f = _openfile(_fname, _search_fp(), _mode);
    __TI_data_synch_WBINV(&_ftable, sizeof(_ftable));
    __TI_resource_unlock(__TI_LOCK_FILE_TBL);
    return f;
}
Esempio n. 4
0
_CODE_ACCESS FILE *freopen(const char *_fname, const char *_mode, register FILE *_fp)
{
    FILE *f;

    /*-----------------------------------------------------------------------*/
    /* This is a critical section because it expects the same slot in the    */
    /* global table _ftable to be available.				     */
    /*-----------------------------------------------------------------------*/
    __TI_resource_lock(__TI_LOCK_FILE_TBL);
    _closefile(_fp); 
    f = _openfile(_fname, _fp, _mode);
    __TI_data_synch_WBINV(&_ftable, sizeof(_ftable));
    __TI_resource_unlock(__TI_LOCK_FILE_TBL);
    return f;
}
Esempio n. 5
0
 LogFile::LogFile(const std::string & fname, bool append, bool writeheader, bool delayfilecreation, StringEncoding wenc) : _m_filename(fname), _m_wenc(wenc), _m_log(nullptr), _append(append), _header(writeheader)
     {
     if (!delayfilecreation) { _openfile(); }
     return;
     }