Beispiel #1
0
size_t
fwrite(const void *vptr, size_t size, size_t count, FILE *f)
{
  const char *ptr = (const char *)vptr;
  register int s;

  s = size * count;

//  __libclog_printf("fwrite(%x,%u,%u,%u)\n",vptr,size,count,f->_file);

  if (f->_flag & _IOLBF)
    while (s > 0) {
      if (--f->_cnt > -f->_bufsiz && *(const char *)ptr != '\n')
	*f->_ptr++ = *(const char *)ptr++;
      else if (_flsbuf(*(const char *)ptr++, f) == EOF)
	break;
      s--;
    }
  else while (s > 0) {
    if (f->_cnt < s) {
      if (f->_cnt > 0) {
	memcpy(f->_ptr, ptr, f->_cnt);
	ptr += f->_cnt;
	f->_ptr += f->_cnt;
	s -= f->_cnt;
      }
      if (_flsbuf(*(const unsigned char *)ptr++, f) == EOF)
	break;
      s--;
    }
    if (f->_cnt >= s) {
      memcpy(f->_ptr, ptr, s);
      f->_ptr += s;
      f->_cnt -= s;
      return count;
    }
  }
  return size != 0 ? count - ((s + size - 1) / size) : 0;
}
extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putchar(int __c)
{
  return (--(&_iob[1])->_cnt >= 0)
    ? (int) (unsigned char) (*(&_iob[1])->_ptr++ = (char)__c)
    : _flsbuf (__c, (&_iob[1]));}
extern __inline__ int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putc (int __c, FILE* __F)
{
  return (--__F->_cnt >= 0)
    ? (int) (unsigned char) (*__F->_ptr++ = (char)__c)
    : _flsbuf (__c, __F);
}
Beispiel #4
0
void skip_line(register FILE *fpin, register FILE *fpout, register int echo)
{
register int ch;
while ((ch=(--(fpin)->_cnt>=0? ((int)*(fpin)->_ptr++):_filbuf(fpin))) != (-1) && ch != '\n')
if (echo)
(--( fpout)->_cnt >= 0 ? (int)(*( fpout)->_ptr++ = (unsigned char)(ch)) : ((( fpout)->_flag & 0200) && -( fpout)->_cnt < ( fpout)->_bufsiz ? ((*( fpout)->_ptr = (unsigned char)(ch)) != '\n' ? (int)(*( fpout)->_ptr++) : _flsbuf(*(unsigned char *)( fpout)->_ptr, fpout)) : _flsbuf((unsigned char)(ch), fpout)));
if (echo)
(--( fpout)->_cnt >= 0 ? (int)(*( fpout)->_ptr++ = (unsigned char)('\n')) : ((( fpout)->_flag & 0200) && -( fpout)->_cnt < ( fpout)->_bufsiz ? ((*( fpout)->_ptr = (unsigned char)('\n')) != '\n' ? (int)(*( fpout)->_ptr++) : _flsbuf(*(unsigned char *)( fpout)->_ptr, fpout)) : _flsbuf((unsigned char)('\n'), fpout)));
lineno++;
}
Beispiel #5
0
/* define the normal version */
#ifdef _MT
size_t __cdecl _fwrite_lk (
#else  /* _MT */
size_t __cdecl fwrite (
#endif  /* _MT */
        const void *buffer,
        size_t size,
        size_t num,
        FILE *stream
        )
{
        const char *data;               /* point to where data comes from next */
        unsigned total;                 /* total bytes to write */
        unsigned count;                 /* num bytes left to write */
        unsigned bufsize;               /* size of stream buffer */
        unsigned nbytes;                /* number of bytes to write now */
        unsigned nwritten;              /* number of bytes written */
        int c;                          /* a temp char */

        /* initialize local vars */
        data = buffer;
        count = total = size * num;
        if (0 == count)
            return 0;

        if (anybuf(stream))
                /* already has buffer, use its size */
                bufsize = stream->_bufsiz;
        else
#if defined (_M_M68K) || defined (_M_MPPC)
                /* assume will get BUFSIZ buffer */
                bufsize = BUFSIZ;
#else  /* defined (_M_M68K) || defined (_M_MPPC) */
                /* assume will get _INTERNAL_BUFSIZ buffer */
                bufsize = _INTERNAL_BUFSIZ;
#endif  /* defined (_M_M68K) || defined (_M_MPPC) */

        /* here is the main loop -- we go through here until we're done */
        while (count != 0) {
                /* if the buffer is big and has room, copy data to buffer */
                if (bigbuf(stream) && stream->_cnt != 0) {
                        /* how much do we want? */
                        nbytes = (count < (unsigned)stream->_cnt) ? count : stream->_cnt;
                        memcpy(stream->_ptr, data, nbytes);

                        /* update stream and amt of data written */
                        count -= nbytes;
                        stream->_cnt -= nbytes;
                        stream->_ptr += nbytes;
                        data += nbytes;
                }
                else if (count >= bufsize) {
                        /* If we have more than bufsize chars to write, write
                           data by calling write with an integral number of
                           bufsiz blocks.  If we reach here and we have a big
                           buffer, it must be full so _flush it. */

                        if (bigbuf(stream)) {
                                if (_flush(stream)) {
                                        /* error, stream flags set -- we're out
                                           of here */
                                        return (total - count) / size;
                                }
                        }

                        /* calc chars to read -- (count/bufsize) * bufsize */
                        nbytes = ( bufsize ? (count - count % bufsize) :
                                   count );

                        nwritten = _write(_fileno(stream), data, nbytes);
                        if (nwritten == (unsigned)EOF) {
                                /* error -- out of here */
                                stream->_flag |= _IOERR;
                                return (total - count) / size;
                        }

                        /* update count and data to reflect write */

                        count -= nwritten;
                        data += nwritten;

                        if (nwritten < nbytes) {
                                /* error -- out of here */
                                stream->_flag |= _IOERR;
                                return (total - count) / size;
                        }
                }
                else {
                        /* buffer full and not enough chars to do direct write,
                           so do a _flsbuf. */
                        c = *data;  /* _flsbuf write one char, this is it */
                        if (_flsbuf(c, stream) == EOF) {
                                /* error or eof, stream flags set by _flsbuf */
                                return (total - count) / size;
                        }

                        /* _flsbuf wrote a char -- update count */
                        ++data;
                        --count;

                        /* update buffer size */
                        bufsize = stream->_bufsiz > 0 ? stream->_bufsiz : 1;
                }
        }

        /* we finished successfully, so just return num */
        return num;
}