1

I have a question regarding the other question asked on stack overflow:- segmentation fault on c K&R fopen and fillbuf. In this question there is a discussion of flags in struct _iobuf. These are used as different access modes. But down here in enum flags there are are some particular values of flags. So will 'flag' take only these particular values or these are some standard values whereas flag can take some other values as well? My doubt arises from the fact that while defining the array _iob only the three standard values (for stdin, stdout, stderr) were given(out of 20) so the fp can take some other values as well(17 others at the same time). The second doubt is that if flag can take only the defined values like(_READ,_WRITE etc.), then in int _fillbuf() function in place of writing

if((fp->flag & (_READ|_EOF|_ERR))!=_READ)

can we write as

if((fp->flag==_WRITE || fp->flag== _UNBUF))

because out of the given fixed flag values it still makes sense.

RSSB
  • 144
  • 2
  • 4
  • 14

1 Answers1

3

The enum values are flag bits and the flag member can have several of them set. (Not all combinations are meaningful, but many are; the __READ, __EOF and __ERR flags are independent of each other and all eight combinations are possible.).

Consequently

if((fp->flag & (_READ|_EOF|_ERR))!=_READ)

Tests that the file is open for reading and has neither the error nor the EOF flags set.

rici
  • 234,347
  • 28
  • 237
  • 341
  • What it means by combinations and what are those 'eight' combinations and in the last part do you mean to say that flag must not be neither _EOF nor _ERR nor their combination of any sort(with each other or with any other flag bit)? Please reply – RSSB May 30 '18 at 07:44
  • 1
    There are eight (2^3) possible combinations of these three flags and any combination can be used because these flags are independent of each other. The example given by @rici tests that file fp is open for reading and both _EOF and _ERR flags are 0. – wahab May 30 '18 at 08:57