delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2007/08/06/07:59:27

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
X-Authenticated: #27081556
X-Provags-ID: V01U2FsdGVkX1+e7YRUREqe5e1ihrJCjihFjiARQas2JjLjPmeCcj
R7niXldLLoFOX7
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
To: djgpp-workers AT delorie DOT com
Subject: A couple of questions about fflush
Date: Mon, 6 Aug 2007 14:04:13 +0200
User-Agent: KMail/1.9.5
MIME-Version: 1.0
Message-Id: <200708061404.13903.juan.guerrero@gmx.de>
X-Y-GMX-Trusted: 0
Reply-To: djgpp-workers AT delorie DOT com

Please look at the following code snippet:


#include <stdio.h>
#include <unistd.h>
/*
 *  Attention:  All coments added by me.
 *
 *  Content of conftest.txt:
 *  hello world\r\n
 *  012345678901112  (char offset in the string, this line is not part of the file.)
 */
int
main ()
{
FILE *f = fopen ("conftest.txt", "r");
	 char buffer[10];
	 int fd = fileno (f);
         int i,ii;  /*  Both integers added by me to catch the return value of the functions.  */

i=fread (buffer, 1, 5, f);
	 if (!f || 0 > fd || i != 5)
	   return 2;

	 /* For deterministic results, ensure f read a bigger buffer.  */
i=lseek (fd, 0, SEEK_CUR);
	 if (i == 5)
	   return 3;

	 /* POSIX requires fflush-fseek to set file offset of fd.  */
ii=fflush (f);
i=fseek (f, 0, SEEK_CUR);
	 if (ii != 0 || i != 0)
	   return 4;

i=lseek (fd, 0, SEEK_CUR);
	 return !(i == 5);

  ;
  return 0;
}

The above code is take from the configure script of m4-1.4.10 that I am trying
to port to djgpp204.  It fails like the code checking for fseeko and ftello and
some other functions.  Those functions are then replaced by those ones supplied
by the included gnulib that some times do not work very well with djgpp.

The important issue here is fflush that does not work as I expect and that makes
the test program fail.  As can be seen in the code, the input stream is opened
for reading *only*.  No read/write nor appending is wanted.  When I run the code
with the debugger, I can see that when it reach the fflush line the file pointer
is at offset 5.  This means that the string "hello world\r\n" looks like
" world\r\n" if we use the actual pointer value to access it content.  Again, the
stream has been opened for reading only.  After executing the fflush() command
the offset has the value 13 (stored in ii=13).  Offset 13 is the first byte *beyond*
the '\n' byte.  If I read the fflush info page supplied by djdev204 I will get the
following info:
<quoting>
Note that @code{fflush} has no effect for streams opened for reading
only.
<end of quoting>

If the function has no effect on streams opened for reading only, why the pointer
is moved bejond the end of the file content.  IMHO nothing should happend at all.
This is tested by the program and because fflush fails, fflush and some other
functions related to this issue are replaced with gnulib ones and they do not
work very well without some adjustments.  This adjustments must be adjusted
every time the gnulib function changes and I do not have enough time to waist it
in that way so the djgpp library must adjusted.

For an average programer like me the following question ariases:
  1) Is the actual fflush behaviour a bug?
  2) Is the actual fflush behaviour a feature?
  3) Is the actual fflush info simple wrong?

I have never been involved in the programing of fflush so I can not say anything
about this issue.


There araises a second question important to me:
 1)  Where is an exact definition of the meaning of all bits in the _flag component
     of the FILE * structure?
 2)  What of this bits are supported by djgpp?
 3)  In which files are the macros/values of the bits defined?
Inspecting my linux system I have seen that a file called libio.h is included by stdio.h
that seems to do the job of defining allmost all of the possible values except for _IOFBF,
_IONBF and _IOLBF that must be defined in stdio.h.  Please note that I am not critizesing
the djgpp design nor I am lobbing for changing anything.  I only want to know where the
things are so I can using them.

Hope someone can answer my questions.

Regards,
Juan M. Guerrero

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019