[gccsdk] Different behaviour between scl and unixlib

2010-09-27 Thread Jan-Jaap van der Geer
Hi,

I was playing with an old program that I'd always compiled against
the SharedCLibrary with Norcroft, but now I tried unixlib and gcc.

The end result was that my program did not work as expected. I
wonder why. This program shows the problem:

#include stdio.h

int main()
{
  char buffer[11];
  FILE* file = fopen(ram:$.input, r);
  int read;
  char lastchar;

  buffer[10] = '\n';
  while ((read = fscanf(file, %10[^\n]%c, buffer, lastchar)) != EOF)
  {
printf(read: %i: '%s', lastchar = %i\n, read, buffer, lastchar);
  }

  fclose (file);

  return 0;
}

If compiled with -mlibscl it reads all lines in ram:$.input, ten
chars at a time, until it encounters a LF. Empty lines are not
really a problem.

Compiled with unixlib, if it encounters an empty line, it does not
read anything at all and ends up in a never ending loop.

Is this correct?

Cheers,
Jan-Jaap

___
GCCSDK mailing list gcc@gccsdk.riscos.info
Bugzilla: http://www.riscos.info/bugzilla/index.cgi
List Info: http://www.riscos.info/mailman/listinfo/gcc
Main Page: http://www.riscos.info/index.php/GCCSDK


Re: [gccsdk] Different behaviour between scl and unixlib

2010-09-27 Thread Ralph Corderoy

Hi Jan-Jaap,

   while ((read = fscanf(file, %10[^\n]%c, buffer, lastchar)) != EOF)

fscanf(3) here says %[ matches a non-empty sequence.  That matches the
behaviour I see on Linux, no gccsdk involved.

$ printf 'foo\nbar\n\nxyz\n' | ./fscanf | sed 4q
read: 2: 'foo', lastchar = 10
read: 2: 'bar', lastchar = 10
read: 0: 'bar', lastchar = 10
read: 0: 'bar', lastchar = 10
$ 

Cheers,
Ralph.


___
GCCSDK mailing list gcc@gccsdk.riscos.info
Bugzilla: http://www.riscos.info/bugzilla/index.cgi
List Info: http://www.riscos.info/mailman/listinfo/gcc
Main Page: http://www.riscos.info/index.php/GCCSDK


Re: [gccsdk] Different behaviour between scl and unixlib

2010-09-27 Thread John Tytgat
In message 425e525c51.jan-j...@iyonix.c2i.net
  Jan-Jaap van der Geer jjvdg...@inbox.com wrote:

 The end result was that my program did not work as expected. I
 wonder why. This program shows the problem:
 
 #include stdio.h
 
 int main()
 {
   char buffer[11];
   FILE* file = fopen(ram:$.input, r);
   int read;
   char lastchar;
 
   buffer[10] = '\n';
   while ((read = fscanf(file, %10[^\n]%c, buffer, lastchar)) != EOF)
   {
 printf(read: %i: '%s', lastchar = %i\n, read, buffer, lastchar);
   }
 
   fclose (file);
 
   return 0;
 }
 
 If compiled with -mlibscl it reads all lines in ram:$.input, ten
 chars at a time, until it encounters a LF. Empty lines are not
 really a problem.
 
 Compiled with unixlib, if it encounters an empty line, it does not
 read anything at all and ends up in a never ending loop.
 
 Is this correct?

I'm not an fscanf() expert but I believe UnixLib has the correct behaviour
and SCL's one is wrong.  Cfr. http://www.opengroup.org/onlinepubs/009695399/

We have two directives here:

- %10[^\n]
- %c

Normally leading white space characters (and that includes newline) are
skipped before trying to match the conversion specifications but because
of the use of '[' and 'c' the leading white space character skipping is
surpressed.

So:

Input 0123\n results in fscanf() returns 2 with 0123 and '\n' as buffer
and lastchar result.
Input 0123456789abc\n results in fscanf() return 2 with 0123456789 and
'a' as buffer and lastchar result and in a next round you get again return
value 2 with bc and '\n' as buffer and lastchar result.
Input   abc\n gives again return value 2 and   abc and '\n' as buffer
and lastchar result.

However, when you have input \n you will get 0 as result as it does not
match %10[^\n] directive and this means failure.  As the input does not
get consumed, this starts to loop from now on.

John.
-- 
John Tytgat, in his comfy chair at home BASS
john.tyt...@aaug.net ARM powered, RISC OS driven

___
GCCSDK mailing list gcc@gccsdk.riscos.info
Bugzilla: http://www.riscos.info/bugzilla/index.cgi
List Info: http://www.riscos.info/mailman/listinfo/gcc
Main Page: http://www.riscos.info/index.php/GCCSDK