gonzui


Format: Advanced Search

t2ex/bsd_source/lib/libc/src_bsd/stdio/fread.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /* Add __sfread() for call from locked region */
    2: 
    3: /*      $OpenBSD: fread.c,v 1.11 2009/11/21 09:53:44 guenther Exp $ */
    4: /*-
    5:  * Copyright (c) 1990, 1993
    6:  *      The Regents of the University of California.  All rights reserved.
    7:  *
    8:  * This code is derived from software contributed to Berkeley by
    9:  * Chris Torek.
   10:  *
   11:  * Redistribution and use in source and binary forms, with or without
   12:  * modification, are permitted provided that the following conditions
   13:  * are met:
   14:  * 1. Redistributions of source code must retain the above copyright
   15:  *    notice, this list of conditions and the following disclaimer.
   16:  * 2. Redistributions in binary form must reproduce the above copyright
   17:  *    notice, this list of conditions and the following disclaimer in the
   18:  *    documentation and/or other materials provided with the distribution.
   19:  * 3. Neither the name of the University nor the names of its contributors
   20:  *    may be used to endorse or promote products derived from this software
   21:  *    without specific prior written permission.
   22:  *
   23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33:  * SUCH DAMAGE.
   34:  */
   35: 
   36: #include <stdio.h>
   37: #include <string.h>
   38: #include "local.h"
   39: 
   40: size_t
   41: __sfread(void *buf, size_t size, size_t count, FILE *fp)
   42: {
   43:         size_t resid;
   44:         char *p;
   45:         int r;
   46:         size_t total;
   47: 
   48:         /*
   49:          * ANSI and SUSv2 require a return value of 0 if size or count are 0.
   50:          */
   51:         if ((resid = count * size) == 0)
   52:                 return (0);
   53:         /*FLOCKFILE(fp);*/
   54:         _SET_ORIENTATION(fp, -1);
   55:         if (fp->_r < 0)
   56:                 fp->_r = 0;
   57:         total = resid;
   58:         p = buf;
   59:         while (resid > (r = fp->_r)) {
   60:                 (void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
   61:                 fp->_p += r;
   62:                 /* fp->_r = 0 ... done in __srefill */
   63:                 p += r;
   64:                 resid -= r;
   65:                 if (__srefill(fp)) {
   66:                         /* no more input: return partial result */
   67:                         /*FUNLOCKFILE(fp);*/
   68:                         return ((total - resid) / size);
   69:                 }
   70:         }
   71:         (void)memcpy((void *)p, (void *)fp->_p, resid);
   72:         fp->_r -= resid;
   73:         fp->_p += resid;
   74:         /*FUNLOCKFILE(fp);*/
   75:         return (count);
   76: }
   77: size_t
   78: fread(void *buf, size_t size, size_t count, FILE *fp)
   79: {
   80:         size_t cnt;
   81: 
   82:         FLOCKFILE(fp);
   83:         cnt = __sfread(buf, size, count, fp);
   84:         FUNLOCKFILE(fp);
   85:         return cnt;
   86: }