aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/fread.c
blob: 28f1376c7d833ca8f114dba2c92c8227b53ff563 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/

#define	import_spp
#define	import_libc
#define	import_stdio
#include <iraf.h>


/* FREAD -- Read a binary block of data from the input file.  To be consistent
** with UNIX we must read until nelem chars have been accumulated or until
** EOF is seen.  Hence, if reading from a record structured device such as a
** terminal, the read will not be terminated by end of record (newline).
** If the number of bytes (C chars) requested does not fill an integral number
** of XCHARS additional bytes will be read to fill out the last XCHAR.
*/
int
fread (
  char	*bp,			/* output buffer		*/
  int	szelem,			/* nbytes per element		*/
  int	nelem,			/* nelems to read		*/
  FILE	*fp
)
{
	register int nread, n;
	int	nbytes;
	XINT	fd = fileno (fp);
	char	*op = bp;


	fd     = fileno (fp);
	nbytes = nelem * szelem;
	nread  = 0;

	if (fp == stdin)
	    (void) fflush (stdout);
	if (szelem <= 0)
	    return (0);

	for (op = bp;  nread < nbytes;  op += n) {
	    iferr (n = c_read (fd, op, nbytes-nread)) {
		fp->_fflags |= _FERR;
		break;
	    } else if (n == EOF) {
		fp->_fflags |= _FEOF;
		break;
	    } else
		nread += n;
	}

	if (fp->_fflags & (_FEOF|_FERR))
	    return (nread ? nread / szelem : 0);
	else
	    return (nread / szelem);
}