コード例 #1
0
int __cdecl printf (
        const char *format,
        ...
        )
/*
 * stdout 'PRINT', 'F'ormatted
 */
{
    va_list arglist;
    int buffing;
    int retval = 0;

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

    va_start(arglist, format);

    _lock_str2(1, stdout);
    __try {
        buffing = _stbuf(stdout);

        retval = _output_l(stdout,format,NULL,arglist);

        _ftbuf(buffing, stdout);

    }
    __finally {
        _unlock_str2(1, stdout);
    }

    return(retval);
}
コード例 #2
0
ファイル: PRINTF.C プロジェクト: ngphloc/agmagic
int __cdecl printf (
        const char *format,
        ...
        )
/*
 * stdout 'PRINT', 'F'ormatted
 */
{
        va_list arglist;
        int buffing;
        int retval;

        va_start(arglist, format);

        _ASSERTE(format != NULL);

        _lock_str2(1, stdout);

        buffing = _stbuf(stdout);

        retval = _output(stdout,format,arglist);

        _ftbuf(buffing, stdout);

        _unlock_str2(1, stdout);

        return(retval);
}
コード例 #3
0
int __cdecl _putts (
        const _TCHAR *string
        )
{
        int buffing;
#ifndef _UNICODE
        unsigned int length;
        unsigned int ndone;
#endif  /* _UNICODE */
        int retval = _TEOF; /* error */

        _ASSERTE(string != NULL);

        _lock_str2(1, stdout);
        buffing = _stbuf(stdout);

#ifdef _UNICODE
        while (*string) {
            if (_putwchar_lk(*string++) == WEOF)
                goto done;
        }
        if (_putwchar_lk(L'\n') != WEOF)
            retval = 0; /* success */
#else  /* _UNICODE */
        length = strlen(string);
        ndone = _fwrite_lk(string,1,length,stdout);

        if (ndone == length) {
                _putc_lk('\n',stdout);
                retval = 0;     /* success */
        }
#endif  /* _UNICODE */

#ifdef _UNICODE
done:
#endif  /* _UNICODE */
        _ftbuf(buffing, stdout);
        _unlock_str2(1, stdout);

        return retval;
}
コード例 #4
0
ファイル: wscanf.c プロジェクト: mysticTot/learn_c
int __cdecl vwscanf(
    WINPUTFN winputfn,
    const wchar_t* format,
    _locale_t plocinfo,
    va_list arglist
)
/*
 * stdin 'W'char_t 'SCAN', 'F'ormatted
 */
{
    int retval;
    _VALIDATE_RETURN((format != NULL), EINVAL, EOF);
    _lock_str2(0, stdin);

    __try {
        retval = (winputfn(stdin, format, plocinfo, arglist));
    } __finally {
        _unlock_str2(0, stdin);
    }

    return (retval);
}
コード例 #5
0
int __cdecl scanf (
        const char *format,
        ...
        )
/*
 * stdin 'SCAN', 'F'ormatted
 */
{
        int retval;

        va_list arglist;

        va_start(arglist, format);

        _ASSERTE(format != NULL);

        _lock_str2(0, stdin);

        retval = (_input(stdin,format,arglist));

        _unlock_str2(0, stdin);

        return(retval);
}
コード例 #6
0
FILE * __cdecl _getstream (
    void
    )
{
    FILE *retval = NULL;
    int i;

    /* Get the iob[] scan lock */
    _mlock(_IOB_SCAN_LOCK);
    __try {

        /*
        * Loop through the __piob table looking for a free stream, or the
        * first NULL entry.
        */
        for ( i = 0 ; i < _nstream ; i++ ) {

            if ( __piob[i] != NULL ) {
                /*
                * if the stream is not inuse, return it.
                */
                if ( !inuse( (FILE *)__piob[i] ) && !str_locked( (FILE *)__piob[i] ) ) {
                    /*
                    * Allocate the FILE lock, in case it hasn't already been
                    * allocated (only necessary for the first _IOB_ENTRIES
                    * locks, not including stdin/stdout/stderr).  Return
                    * failure if lock can't be allocated.
                    */
                    if ( i > 2 && i < _IOB_ENTRIES )
                        if ( !_mtinitlocknum( _STREAM_LOCKS + i ) )
                            break;

                    _lock_str2(i, __piob[i]);

                    if ( inuse( (FILE *)__piob[i] ) ) {
                        _unlock_str2(i, __piob[i]);
                        continue;
                    }
                    retval = (FILE *)__piob[i];
                    break;
                }
            }
            else {
                /*
                * allocate a new _FILEX, set _piob[i] to it and return a
                * pointer to it.
                */
                if ( (__piob[i] = _malloc_crt( sizeof(_FILEX) )) != NULL ) {

                    __crtInitializeCriticalSectionEx(&(((_FILEX *)__piob[i])->lock), _CRT_SPINCOUNT, 0);
                    EnterCriticalSection( &(((_FILEX *)__piob[i])->lock) );
                    retval = (FILE *)__piob[i];
                    retval->_flag = 0;
                }

                break;
            }
        }

        /*
        * Initialize the return stream.
        */
        if ( retval != NULL ) {
            /* make sure that _IOLOCKED is preserved (if set) and zero out the other bits of _flag */
            retval->_flag &= _IOLOCKED;
            retval->_cnt = 0;
            retval->_tmpfname = retval->_ptr = retval->_base = NULL;
            retval->_file = -1;
        }

    }
    __finally {
        _munlock(_IOB_SCAN_LOCK);
    }

    return(retval);
}
コード例 #7
0
ファイル: gets.c プロジェクト: mysticTot/learn_c
_TCHAR* __cdecl _getts_helper(
    _TCHAR* string,
    size_t bufferSize,
    int earlyOutIfEOFIsFirstChar
) {
    int ch = 0;
    _TCHAR* pointer = string;
    _TCHAR* retval = string;
    _VALIDATE_RETURN((string != NULL), EINVAL, NULL);
    _VALIDATE_RETURN((bufferSize > 0), EINVAL, 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 */
    _lock_str2(0, stdin);

    __try {
#ifndef _UNICODE
        _VALIDATE_STREAM_ANSI_SETRET(stdin, EINVAL, retval, NULL);

        if (retval == NULL) {
            goto done;
        }

#endif  /* _UNICODE */
        /* special case: check if the first char is EOF and treat it differently if the user requested so */
        ch = _getchar_helper();

        if (ch == _TEOF) {
            retval = NULL;

            if (earlyOutIfEOFIsFirstChar) {
                goto done;
            }
        }

        if (bufferSize == (size_t) - 1) {
            /* insecure case: no buffer size check, no debug filling */
            while (ch != _T('\n') && ch != _TEOF) {
                *pointer++ = (_TCHAR)ch;
                ch = _getchar_helper();
            }

            *pointer = 0;
        } else {
            /* secure case, check buffer size; if buffer overflow, keep on reading until /n or EOF */
            size_t available = bufferSize;

            while (ch != _T('\n') && ch != _TEOF) {
                if (available > 0) {
                    --available;
                    *pointer++ = (_TCHAR)ch;
                }

                ch = _getchar_helper();
            }

            if (available == 0) {
                _RESET_STRING(string, bufferSize);
                _RETURN_BUFFER_TOO_SMALL_ERROR(string, bufferSize, NULL);
            }

            *pointer = 0;
            _FILL_STRING(string, bufferSize, bufferSize - available + 1);
        }

        /* Common return */
    done:
        ;
    } __finally {
        _unlock_str2(0, stdin);
    }

    return retval;
}
コード例 #8
0
ファイル: FFLUSH.C プロジェクト: pbarounis/ActiveBar2
static int __cdecl flsall (
        int flushflag
        )
{
        REG1 int i;
        int count = 0;
        int errcode = 0;

        _mlock(_IOB_SCAN_LOCK);

        for ( i = 0 ; i < _nstream ; i++ ) {

                if ( (__piob[i] != NULL) && (inuse((FILE *)__piob[i])) ) {

#ifdef _MT
                        /*
                         * lock the stream. this is not done until testing
                         * the stream is in use to avoid unnecessarily creating
                         * a lock for every stream. the price is having to
                         * retest the stream after the lock has been asserted.
                         */
                        _lock_str2(i, __piob[i]);

                        /*
                         * if the stream is STILL in use (it may have been
                         * closed before the lock was asserted), see about
                         * flushing it.
                         */
                        if ( inuse((FILE *)__piob[i]) ) {
#endif  /* _MT */

                        if ( flushflag == FLUSHALL ) {
                                /*
                                 * FLUSHALL functionality: fflush the read or
                                 * write stream and, if successful, update the
                                 * count of flushed streams
                                 */
                                if ( _fflush_lk(__piob[i]) != EOF )
                                        /* update count of successfully flushed
                                         * streams
                                         */
                                        count++;
                        }
                        else if ( (flushflag == FFLUSHNULL) &&
                                  (((FILE *)__piob[i])->_flag & _IOWRT) ) {
                                /*
                                 * FFLUSHNULL functionality: fflush the write
                                 * stream and kept track of the error, if one
                                 * occurs
                                 */
                                if ( _fflush_lk(__piob[i]) == EOF )
                                        errcode = EOF;
                        }

#ifdef _MT
                        }
                        _unlock_str2(i, __piob[i]);
#endif  /* _MT */
                }
        }

        _munlock(_IOB_SCAN_LOCK);

        if ( flushflag == FLUSHALL )
                return(count);
        else
                return(errcode);
}
コード例 #9
0
ファイル: STREAM.C プロジェクト: ngphloc/agmagic
FILE * __cdecl _getstream (
        void
        )
{
        REG2 FILE *retval = NULL;

#ifdef _WIN32

        REG1 int i;

        /* Get the iob[] scan lock */
        _mlock(_IOB_SCAN_LOCK);

        /*
         * Loop through the __piob table looking for a free stream, or the
         * first NULL entry.
         */
        for ( i = 0 ; i < _nstream ; i++ ) {

            if ( __piob[i] != NULL ) {
                /*
                 * if the stream is not inuse, return it.
                 */
                if ( !inuse( (FILE *)__piob[i] ) ) {
#ifdef _MT
                    _lock_str2(i, __piob[i]);

                    if ( inuse( (FILE *)__piob[i] ) ) {
                        _unlock_str2(i, __piob[i]);
                        continue;
                    }
#endif  /* _MT */
                    retval = (FILE *)__piob[i];
                    break;
                }
            }
            else {
                /*
                 * allocate a new _FILEX, set _piob[i] to it and return a
                 * pointer to it.
                 */
                if ( (__piob[i] = _malloc_crt( sizeof(_FILEX) )) != NULL ) {

#if defined (_MT)
                    InitializeCriticalSection( &(((_FILEX *)__piob[i])->lock) );
                    EnterCriticalSection( &(((_FILEX *)__piob[i])->lock) );
#endif  /* defined (_MT) */
                    retval = (FILE *)__piob[i];
                }

                break;
            }
        }

        /*
         * Initialize the return stream.
         */
        if ( retval != NULL ) {
            retval->_flag = retval->_cnt = 0;
            retval->_tmpfname = retval->_ptr = retval->_base = NULL;
            retval->_file = -1;
        }

        _munlock(_IOB_SCAN_LOCK);

#else  /* _WIN32 */
#if defined (_M_MPPC) || defined (_M_M68K)

        REG1 FILE *stream = _iob;

        /* Loop through the _iob table looking for a free stream.*/
        for (; stream <= _lastiob; stream++) {

                if ( !inuse(stream) ) {
                        stream->_flag = stream->_cnt = 0;
                        stream->_tmpfname = stream->_ptr = stream->_base = NULL;
                        stream->_file = -1;
                        retval = stream;
                        break;
                }
        }

#endif  /* defined (_M_MPPC) || defined (_M_M68K) */
#endif  /* _WIN32 */

        return(retval);
}