1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FREAD.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,111 @@
1.4 +/* FREAD.C
1.5 + *
1.6 + * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Copyright (c) 1990 The Regents of the University of California.
1.12 + * All rights reserved.
1.13 + *
1.14 + * Redistribution and use in source and binary forms are permitted
1.15 + * provided that the above copyright notice and this paragraph are
1.16 + * duplicated in all such forms and that any documentation,
1.17 + * advertising materials, and other materials related to such
1.18 + * distribution and use acknowledge that the software was developed
1.19 + * by the University of California, Berkeley. The name of the
1.20 + * University may not be used to endorse or promote products derived
1.21 + * from this software without specific prior written permission.
1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1.25 + */
1.26 +
1.27 +/*
1.28 +FUNCTION
1.29 +<<fread>>---read array elements from a file
1.30 +
1.31 +INDEX
1.32 + fread
1.33 +
1.34 +ANSI_SYNOPSIS
1.35 + #include <stdio.h>
1.36 + size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>,
1.37 + FILE *<[fp]>);
1.38 +
1.39 +TRAD_SYNOPSIS
1.40 + #include <stdio.h>
1.41 + size_t fread(<[buf]>, <[size]>, <[count]>, <[fp]>)
1.42 + char *<[buf]>;
1.43 + size_t <[size]>;
1.44 + size_t <[count]>;
1.45 + FILE *<[fp]>;
1.46 +
1.47 +DESCRIPTION
1.48 +<<fread>> attempts to copy, from the file or stream identified by
1.49 +<[fp]>, <[count]> elements (each of size <[size]>) into memory,
1.50 +starting at <[buf]>. <<fread>> may copy fewer elements than
1.51 +<[count]> if an error, or end of file, intervenes.
1.52 +
1.53 +<<fread>> also advances the file position indicator (if any) for
1.54 +<[fp]> by the number of @emph{characters} actually read.
1.55 +
1.56 +RETURNS
1.57 +The result of <<fread>> is the number of elements it succeeded in
1.58 +reading.
1.59 +
1.60 +PORTABILITY
1.61 +ANSI C requires <<fread>>.
1.62 +
1.63 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.64 +<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
1.65 +*/
1.66 +
1.67 +#include <stdio.h>
1.68 +#include <string.h>
1.69 +#include "LOCAL.H"
1.70 +
1.71 +/**
1.72 +Read block of data from a stream.
1.73 +Read count number of items each one with a size of size bytes from the stream
1.74 +and stores it in the specified buffer.
1.75 +Stream's postion indicator is increased by the number of bytes readed.
1.76 +Total amount of bytes read is (size x count).
1.77 +@return The total number of items readed is returned.
1.78 +@param buf Pointer to the destination structure with a minimum size of (size*count) bytes.
1.79 +@param size Size in bytes of each item to be read.
1.80 +@param count Number of items, each one with a size of size bytes.
1.81 +@param fp pointer to an open file.
1.82 +*/
1.83 +EXPORT_C size_t
1.84 +fread (void *buf, size_t size, size_t count, FILE * fp)
1.85 +{
1.86 + register size_t resid;
1.87 + register char *p;
1.88 + register int r;
1.89 + size_t total;
1.90 +
1.91 + if ((resid = count * size) == 0)
1.92 + return 0;
1.93 + if (fp->_r < 0)
1.94 + fp->_r = 0;
1.95 + total = resid;
1.96 + p = (char*)buf;
1.97 + while ((int)resid > (r = fp->_r))
1.98 + {
1.99 + (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
1.100 + fp->_p += r;
1.101 + /* fp->_r = 0 ... done in __srefill */
1.102 + p += r;
1.103 + resid -= r;
1.104 + if (__srefill (fp))
1.105 + {
1.106 + /* no more input: return partial result */
1.107 + return (total - resid) / size;
1.108 + }
1.109 + }
1.110 + (void) memcpy ((void *) p, (void *) fp->_p, resid);
1.111 + fp->_r -= resid;
1.112 + fp->_p += resid;
1.113 + return count;
1.114 +}