Exemple #1
0
int __cdecl fputws (
        const wchar_t *string,
        FILE *stream
        )
{
        size_t length;
        int retval = 0;

        _VALIDATE_RETURN((string != NULL), EINVAL, WEOF);
        _VALIDATE_RETURN((stream != NULL), EINVAL, WEOF);

        length = wcslen(string);

        _lock_str(stream);
        __try {

        while (length--)
        {
            if (_putwc_nolock(*string++, stream) == WEOF)
            {
                retval = -1;
                break;
            }
        }

        }
        __finally {
            _unlock_str(stream);
        }

        return(retval);
}
Exemple #2
0
int __cdecl fwprintf (
	FILE *str,
	const wchar_t *format,
	...
	)
/*
 * 'F'ile (stream) 'W'char_t 'PRINT', 'F'ormatted
 */
{
	va_list(arglist);
	REG1 FILE *stream;
	REG2 int buffing;
	int retval;

// UNDONE: make va_start work with wchar_t format string
	va_start(arglist, format);

	_ASSERTE(str != NULL);
	_ASSERTE(format != NULL);

	/* Init stream pointer */
	stream = str;

	_lock_str(stream);
	buffing = _stbuf(stream);
	retval = _woutput(stream,format,arglist);
	_ftbuf(buffing, stream);
	_unlock_str(stream);

	return(retval);
}
Exemple #3
0
int __cdecl vfwprintf_helper (
        WOUTPUTFN woutfn,
        FILE *str,
        const wchar_t *format,
        _locale_t plocinfo,
        va_list ap
        )
/*
 * 'V'ariable argument 'F'ile (stream) 'W'char_t 'PRINT', 'F'ormatted
 */
{
        REG1 FILE *stream;
        REG2 int buffing;
        REG3 int retval;

        _VALIDATE_RETURN( (str != NULL), EINVAL, -1);
        _VALIDATE_RETURN( (format != NULL), EINVAL, -1);

        /* Init stream pointer */
        stream = str;

        _lock_str(stream);
        __try {

        buffing = _stbuf(stream);
        retval = woutfn(stream,format,plocinfo,ap );
        _ftbuf(buffing, stream);

        }
        __finally {
            _unlock_str(stream);
        }

        return(retval);
}
Exemple #4
0
int __cdecl _putw (
        int word,
        FILE *str
        )
{
        REG1 FILE *stream;
        REG3 int bytecount = sizeof(int);
        REG2 char *byteptr = (char *)&word;
        int retval;

        _VALIDATE_RETURN((str != NULL), EINVAL, EOF);

        /* Init stream pointer */
        stream = str;

        _lock_str(stream);
        __try {

        while (bytecount--)
        {
            _putc_nolock(*byteptr,stream);
            ++byteptr;
        }
        retval = (ferror(stream) ? EOF : word);

        }
        __finally {
            _unlock_str(stream);
        }

        return(retval);
}
Exemple #5
0
int __cdecl vfwscanf (
        WINPUTFN winputfn,
        FILE *stream,
        const wchar_t *format,
        _locale_t plocinfo,
        va_list arglist
        )
/*
 * 'F'ile (stream) 'W'char_t 'SCAN', 'F'ormatted
 */
{
        int retval;

        _VALIDATE_RETURN((stream != NULL), EINVAL, EOF);
        _VALIDATE_RETURN((format != NULL), EINVAL, EOF);

        _lock_str(stream);
        __try {

        retval = (winputfn(stream, format, plocinfo, arglist));

        }
        __finally {
            _unlock_str(stream);
        }

        return(retval);
}
Exemple #6
0
int __cdecl _getw(
    FILE* str
) {
    REG1 FILE* stream;
    REG2 int bytecount = sizeof(int);
    int word;
    char* byteptr = (char*)&word;
    int retval;
    _VALIDATE_RETURN((str != NULL), EINVAL, EOF);
    /* Init stream pointer */
    stream = str;
    _lock_str(stream);

    __try {
        while (bytecount--) {
            *byteptr++ = (char)_getc_nolock(stream);
        }

        retval = ((feof(stream) || ferror(stream)) ? EOF : word);
    } __finally {
        _unlock_str(stream);
    }

    return (retval);
}
Exemple #7
0
int __cdecl _putw (
    int word,
    FILE *str
)
{
    REG1 FILE *stream;
    REG3 int bytecount = sizeof(int);
    REG2 char *byteptr = (char *)&word;
    int retval;

    _ASSERTE(str != NULL);

    /* Init stream pointer */
    stream = str;

    _lock_str(stream);

    while (bytecount--)
    {
        _putc_lk(*byteptr,stream);
        ++byteptr;
    }
    retval = (ferror(stream) ? EOF : word);

    _unlock_str(stream);
    return(retval);
}
int __cdecl fputws (
        const wchar_t *string,
        FILE *stream
        )
{
        unsigned int length;
        int retval = 0;

        _ASSERTE(string != NULL);
        _ASSERTE(stream != NULL);

        length = wcslen(string);

        _lock_str(stream);

        while (length--)
        {
            if (_putwc_lk(*string++, stream) == WEOF)
            {
                retval = -1;
                break;
            }
        }

        _unlock_str(stream);

        return(retval);
}
int __cdecl vfwprintf (
        FILE *str,
        const wchar_t *format,
        va_list ap
        )
/*
 * 'V'ariable argument 'F'ile (stream) 'W'char_t 'PRINT', 'F'ormatted
 */
{
        REG1 FILE *stream;
        REG2 int buffing;
        REG3 int retval;

        _ASSERTE(str != NULL);
        _ASSERTE(format != NULL);

        /* Init stream pointer */
        stream = str;

        _lock_str(stream);
        buffing = _stbuf(stream);
        retval = _woutput(stream,format,ap );
        _ftbuf(buffing, stream);
        _unlock_str(stream);

        return(retval);
}
Exemple #10
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;
}
Exemple #11
0
/* define locking/unlocking version */
size_t __cdecl fwrite (
        const void *buffer,
        size_t size,
        size_t count,
        FILE *stream
        )
{
        size_t retval;

        _lock_str(stream);                      /* lock stream */
        retval = _fwrite_lk(buffer, size, count, stream);  /* do the read */
        _unlock_str(stream);                    /* unlock stream */
        return retval;
}
Exemple #12
0
int __cdecl fgetc (
	REG1 FILE *stream
	)
{
	int retval;

	_ASSERTE(stream != NULL);

	_lock_str(stream);
	retval = _getc_lk(stream);
	_unlock_str(stream);

	return(retval);
}
Exemple #13
0
long __cdecl ftell(
    FILE* stream
) {
    long retval;
    _VALIDATE_RETURN((stream != NULL), EINVAL, (-1L));
    _lock_str(stream);

    __try {
        retval = _ftell_nolock(stream);
    } __finally {
        _unlock_str(stream);
    }

    return (retval);
}
wint_t __cdecl ungetwc (
        REG2 wint_t ch,
        REG1 FILE *stream
        )
{
        wint_t retval;

        _ASSERTE(stream != NULL);

        _lock_str(stream);

        retval = _ungetwc_lk (ch, stream);

        _unlock_str(stream);

        return(retval);
}
int __cdecl fseek (
        FILE *stream,
        long offset,
        int whence
        )
{
        int retval;

        _ASSERTE(stream != NULL);

        _lock_str(stream);

        retval = _fseek_lk (stream, offset, whence);

        _unlock_str(stream);

        return(retval);
}
Exemple #16
0
int __cdecl fseek(
    FILE* stream,
    long offset,
    int whence
) {
    int retval;
    _VALIDATE_RETURN((stream != NULL), EINVAL, -1);
    _VALIDATE_RETURN(((whence == SEEK_SET) || (whence == SEEK_CUR) || (whence == SEEK_END)), EINVAL, -1);
    _lock_str(stream);

    __try {
        retval = _fseek_nolock(stream, offset, whence);
    } __finally {
        _unlock_str(stream);
    }

    return (retval);
}
int __cdecl _fseeki64 (
        FILE *stream,
        __int64 offset,
        int whence
        )
{
        int retval;

        _ASSERTE(stream != NULL);

        _lock_str(stream);

        retval = _fseeki64_lk (stream, offset, whence);

        _unlock_str(stream);

        return(retval);
}
Exemple #18
0
void __cdecl rewind (
        FILE *str
        )
{
        REG1 FILE *stream;
        REG2 int fd;

        _VALIDATE_RETURN_VOID( (str != NULL), EINVAL);

        /* Init stream pointer */
        stream = str;

        fd = _fileno(stream);

        /* Lock the file */
        _lock_str(stream);
        __try {

        /* Flush the stream */
        _flush(stream);

        /* Clear errors */
        stream->_flag &= ~(_IOERR|_IOEOF);
        _osfile_safe(fd) &= ~(FEOFLAG);

        /* Set flags */
        /* [note: _flush set _cnt=0 and _ptr=_base] */
        if (stream->_flag & _IORW)
            stream->_flag &= ~(_IOREAD|_IOWRT);

        /* Position to beginning of file */
        if(_lseek(fd,0L,0)==-1)
                {
                        stream->_flag |= _IOERR;
                }

        }
        __finally {
            /* unlock stream */
            _unlock_str(stream);
        }

}
Exemple #19
0
int __cdecl fflush (
        REG1 FILE *stream
        )
{
        int rc;

        /* if stream is NULL, flush all streams
         */
        if ( stream == NULL )
                return(flsall(FFLUSHNULL));

        _lock_str(stream);

        rc = _fflush_lk(stream);

        _unlock_str(stream);

        return(rc);
}
Exemple #20
0
wint_t __cdecl fputwc (
        wint_t ch,
        FILE *str
        )
{
        REG1 FILE *stream;
        REG2 wint_t retval;

        _ASSERTE(str != NULL);

        /* Init stream pointer */
        stream = str;

        _lock_str(stream);
        retval = _putwc_lk(ch,stream);
        _unlock_str(stream);

        return(retval);
}
Exemple #21
0
int __cdecl ungetc (
        REG2 int ch,
        REG1 FILE *stream
        )
{
        int retval;

        _VALIDATE_RETURN( (stream != NULL), EINVAL, EOF);

        _lock_str(stream);

        __try {
                retval = _ungetc_nolock (ch, stream);
        }
        __finally {
                _unlock_str(stream);
        }

        return(retval);
}
Exemple #22
0
/* define locking/unlocking version */
size_t __cdecl fread_s(
    void *buffer,
    size_t bufferSize,
    size_t elementSize,
    size_t count,
    FILE *stream
)
{
    size_t retval = 0;

    if (elementSize == 0 || count == 0)
    {
        return 0;
    }

    /* validation */
    _VALIDATE_RETURN((buffer != NULL), EINVAL, 0);
    if (stream == NULL || count > (SIZE_MAX / elementSize))
    {
        if (bufferSize != SIZE_MAX)
        {
            memset(buffer, _BUFFER_FILL_PATTERN, bufferSize);
        }

        _VALIDATE_RETURN((stream != NULL), EINVAL, 0);
        _VALIDATE_RETURN(count <= (SIZE_MAX / elementSize), EINVAL, 0);
    }

    _lock_str(stream);
    __try
    {
        /* do the read; _fread_nolock_s will make sure we do not buffer overrun */
        retval = _fread_nolock_s(buffer, bufferSize, elementSize, count, stream);
    }
    __finally
    {
        _unlock_str(stream);
    }

    return retval;
}
int __cdecl fputs (
        const char *string,
        FILE *stream
        )
{
        REG2 int buffing;
        REG1 unsigned int length;
        REG3 unsigned int ndone;

        _ASSERTE(string != NULL);
        _ASSERTE(stream != NULL);

        length = strlen(string);
        _lock_str(stream);
        buffing = _stbuf(stream);
        ndone = _fwrite_lk(string,1,length,stream);
        _ftbuf(buffing, stream);
        _unlock_str(stream);

        return(ndone == length ? 0 : EOF);
}
int __cdecl fprintf (
        FILE *str,
        const char *format,
        ...
        )
/*
 * 'F'ile (stream) 'PRINT', 'F'ormatted
 */
{
    va_list(arglist);
    FILE *stream;
    int buffing;
    int retval=0;

    _VALIDATE_RETURN( (str != NULL), EINVAL, -1);
    _VALIDATE_RETURN( (format != NULL), EINVAL, -1);

    va_start(arglist, format);

    /* Init stream pointer */
    stream = str;

    _lock_str(stream);
    __try {
        _VALIDATE_STREAM_ANSI_SETRET(stream, EINVAL, retval, -1);

        if (retval==0)
        {
            buffing = _stbuf(stream);
            retval = _output_l(stream,format,NULL,arglist);
            _ftbuf(buffing, stream);
        }
    }
    __finally {
        _unlock_str(stream);
    }

    return(retval);
}
Exemple #25
0
int __cdecl vwprintf (
	const wchar_t *format,
	va_list ap
	)
/*
 * stdout 'V'ariable, 'W'char_t 'PRINT', 'F'ormatted
 */
{
	REG1 FILE *stream = stdout;
	REG2 int buffing;
	REG3 int retval;

	_ASSERTE(format != NULL);

	_lock_str(stream);
	buffing = _stbuf(stream);
	retval = _woutput(stream, format, ap );
	_ftbuf(buffing, stream);
	_unlock_str(stream);

	return(retval);
}
void __cdecl clearerr (
        FILE *stream
        )
{
        _ASSERTE(stream != NULL);

        _lock_str(stream);

        /* Clear stdio level flags */
        stream->_flag &= ~(_IOERR|_IOEOF);

        /* Clear lowio level flags */

#ifdef _WIN32

        _osfile_safe(_fileno(stream)) &= ~(FEOFLAG);

#else  /* _WIN32 */
        _osfile[_fileno(stream)] &= ~(FEOFLAG);
#endif  /* _WIN32 */

        _unlock_str(stream);
}
Exemple #27
0
int __cdecl fputs(
    const char* string,
    FILE* stream
) {
    REG2 int buffing;
    REG1 size_t length;
    REG3 size_t ndone;
    _VALIDATE_RETURN((string != NULL), EINVAL, EOF);
    _VALIDATE_RETURN((stream != NULL), EINVAL, EOF);
    _VALIDATE_STREAM_ANSI_RETURN(stream, EINVAL, EOF);
    length = strlen(string);
    _lock_str(stream);

    __try {
        buffing = _stbuf(stream);
        ndone = _fwrite_nolock(string, 1, length, stream);
        _ftbuf(buffing, stream);
    } __finally {
        _unlock_str(stream);
    }

    return (ndone == length ? 0 : EOF);
}
Exemple #28
0
int __cdecl fwprintf (
        FILE *str,
        const wchar_t *format,
        ...
        )
/*
 * 'F'ile (stream) 'W'char_t 'PRINT', 'F'ormatted
 */
{
        va_list(arglist);
        REG1 FILE *stream;
        REG2 int buffing;
        int retval;

        _VALIDATE_RETURN( (str != NULL), EINVAL, -1);
        _VALIDATE_RETURN( (format != NULL), EINVAL, -1);

        va_start(arglist, format);

        /* Init stream pointer */
        stream = str;

        _lock_str(stream);
        __try {

        buffing = _stbuf(stream);
        retval = _woutput_l(stream,format,NULL,arglist);
        _ftbuf(buffing, stream);

        }
        __finally {
            _unlock_str(stream);
        }

        return(retval);
}
Exemple #29
0
int __cdecl fscanf (
	FILE *stream,
	const char *format,
	...
	)
/*
 * 'F'ile (stream) 'SCAN', 'F'ormatted
 */
{
	int retval;

	va_list arglist;

	va_start(arglist, format);

	_ASSERTE(stream != NULL);
	_ASSERTE(format != NULL);

	_lock_str(stream);
	retval = (_input(stream,format,arglist));
	_unlock_str(stream);

	return(retval);
}
_TSCHAR * __cdecl _fgetts (
        _TSCHAR *string,
        int count,
        FILE *str
        )
{
    FILE *stream;
    _TSCHAR *pointer = string;
    _TSCHAR *retval = string;
    int ch;

    _VALIDATE_RETURN(( string != NULL ) || ( count == 0 ), EINVAL, NULL);
    _VALIDATE_RETURN(( count >= 0 ), EINVAL, NULL);
    _VALIDATE_RETURN(( str != NULL ), EINVAL, NULL);

    if (count == 0)
    {
        return NULL;
    }

    /* The C Standard states the input buffer should remain
    unchanged if EOF is encountered immediately. Hence we
    do not blank out the input buffer here */

    /* Init stream pointer */
    stream = str;

    _lock_str(stream);
    __try {
#ifndef _UNICODE
        _VALIDATE_STREAM_ANSI_SETRET(stream, EINVAL, retval, NULL);
#endif  /* _UNICODE */
        if (retval!=NULL)
        {
            while (--count)
            {
                if ((ch = _fgettc_nolock(stream)) == _TEOF)
                {
                    if (pointer == string) {
                                    retval=NULL;
                                    goto done;
                    }

                    break;
                }

                if ((*pointer++ = (_TSCHAR)ch) == _T('\n'))
                    break;
            }

            *pointer = _T('\0');
        }


/* Common return */
done: ;
    }
    __finally {
        _unlock_str(stream);
    }

    return(retval);
}