Code:if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
return EOF;
plz pay attention to the part bolded and colored.Code:{ 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
Printable View
Code:if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
return EOF;
plz pay attention to the part bolded and colored.Code:{ 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
The above is excerpted from the below.
Hope this helps.
Code:Once the buffer is established, _fillbuf calls read to fill it,
sets the count and pointers, and returns the character
at the beginning of the buffer. Subsequent calls to _fillbuf
will find a buffer allocated.
#include "syscalls.h"
/* _fillbuf: allocate and fill input buffer */
int _fillbuf(FILE *fp)
{
int bufsize;
if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
return EOF;
bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ;
if (fp->base == NULL) /* no buffer yet */
if ((fp->base = (char *) malloc(bufsize)) == NULL)
return EOF; /* can't get buffer */
fp->ptr = fp->base;
fp->cnt = read(fp->fd, fp->ptr, bufsize);
if (--fp->cnt < 0) {
if (fp->cnt == -1)
fp->flag |= _EOF;
else
fp->flag |= _ERR;
fp->cnt = 0;
return EOF;
}
return (unsigned char) *fp->ptr++;
}
The only remaining loose end is how everything gets
started. The array _iob must be defined and initialized
for stdin, stdout and stderr:
FILE _iob[OPEN_MAX] = { /* stdin, stdout, stderr */
{ 0, (char *) 0, (char *) 0, _READ, 0 },
{ 0, (char *) 0, (char *) 0, _WRITE, 1 },
{ 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
};
I believe the second bolded spot you have is a typo. You seem to have added a comma after _WRITE, which shouldn't be there. Try and compile that and you'll find out what I mean.
What you're seeing here is the bitwise OR operator. For a full explanation, consider reading this FAQ entry. Basicly it's OR-ing two values on a bit by bit basis, giving you a resulting value which has all set bits of both values combine.
Quzah.
basically _WRITE and _UNBUF are, more than likely, macros #defined somewhere. whatever their values are, they are bitwise OR'd together; _WRITE | _UNBUF.
if _WRITE = 00000001
and _UNBUF = 00000010
then (to my knowledge) ORing them will result
00000011
Read up on bitwise operations.
thanks for replys from you both.
I know some about bitwise, and here what that confuses me is whether a "|" has been missed by typo.
and yes, they've been defined before being used.
so I'm guessing:Code:enum _flags {
_READ = 01, /* file open for reading */
_WRITE = 02, /* file open for writing */
_UNBUF = 04, /* file is unbuffered */
_EOF = 010, /* EOF has occurred on this file */
_ERR = 020 /* error occurred on this file */
};
is:Code:if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
and:Code:if ((fp->flag&(_READ|_EOF|_ERR)) != _READ)
is:Code:{ 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
Do you agree? Or you have the same book, K&R - The C programming language, please refers to chapter 8.5-Examples to see whether they are same as that in my book.Code:{ 0, (char *) 0, (char *) 0, _WRITE | _UNBUF, 2 }
...
Im trying to figure out what they exactly mean.
1) (_READ|_EOF|_ERR) set corresponding bits to 1;
fp->flag AND with it, to see whether result is _READ, namely _EOF and _ERR are gone.
2) like what quazh and sand_man said.
sigh! so hard!
They look like typos to me but you can only guess.
if it compiles then i would assume _EOF_ERR == 0011
given _EOF = 0010 //made up value
and _ERR = 0001 //made up value
i don;t know where _EOF_ERR would be defined though