public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* unexpected behavior of read() under win95
@ 1998-01-24  7:53 Sebastian Kuzminsky
  1998-01-24 21:53 ` Fergus Henderson
  0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Kuzminsky @ 1998-01-24  7:53 UTC (permalink / raw)
  To: gnu-win32

   Hi, i'm new to the gnu-win32 scene.  (I think it's a great idea,
i'll take vi and gcc over Microsoft Visual This or That ++ anyday.
Thanks a lot, developers!)


   Anyway, i'm having a problem with read().  I've included a program
which (on my system) demonstrates the problem.  I'm using version 'b17.1'
of win32gcc.  I'm working under Linux and cross-compiling for a win95 box.
The program works under Linux when compiled with gcc, exact same flags.
Here's the program:

----- begin include simptest.c -----


#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>




int main(int argc, char *argv[]) { 
   int file_fd;
   int n;
   char buffer[100];

   int total_read = 0;


   if (argc < 2) {
      printf("usage:  simptest <filename>\n");
      exit(1);
   }

   file_fd = open(argv[1], O_RDONLY);
   if (file_fd == -1) {
      fprintf(stderr, "cannot open %s:  ", argv[1]);
      exit(1);
   }


   do {
      n = read(file_fd, buffer, 50);
      if (n == -1) {
	 return -1;
      }

      if (n == 0) {
	 printf("end of file?\n");
	 printf("read %d bytes total\n", total_read);
	 return 0;
      }

      if (n != 50) {
	 fprintf(stderr, "read %d bytes, expected 50\n", n);
      } else {
         printf("read 50, ok\n");
      }

      total_read += n;
   } while(1);


   exit(0);
}


----- end include simptest.c -----


   When run under Linux, this program reads 50 byte chunks of the named
file, until there's less than 50 bytes left, then it reads whatever's
left and then quits.  This is what i would call the expected behavior.


   When run under win95, however, it behaves unexpectedly.  Calls to
read() often read less than 50 bytes (which is surprising and annoying but
legal), and sometimes read 0 bytes (signifying EOF, which is not legal).
This happens both on binary and text files.


   Any idea what's up here?  I guess i should try it using the latest
version.




   Also, i cant link my program static.  Even if i give the -static flag
at the link step i still need cygwin.dll to run my program under win95.




Sebastian

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: unexpected behavior of read() under win95
  1998-01-24  7:53 unexpected behavior of read() under win95 Sebastian Kuzminsky
@ 1998-01-24 21:53 ` Fergus Henderson
  1998-01-26 11:40   ` Sebastian Kuzminsky
  0 siblings, 1 reply; 3+ messages in thread
From: Fergus Henderson @ 1998-01-24 21:53 UTC (permalink / raw)
  To: Sebastian Kuzminsky; +Cc: gnu-win32

On 22-Jan-1998, Sebastian Kuzminsky <kuzminsk@taussky.cs.colorado.edu> wrote:
>    file_fd = open(argv[1], O_RDONLY);

If you want the return values of read() match the number of characters you
asked for, you need to open the file in binary mode, using O_BINARY:

     file_fd = open(argv[1], O_RDONLY | O_BINARY);

(The exception is that if your file system is mounted in text=binary mode,
then O_BINARY is the default.)

For text mode opens, read() is stripping out \r's.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: < http://www.cs.mu.oz.au/~fjh >   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: unexpected behavior of read() under win95
  1998-01-24 21:53 ` Fergus Henderson
@ 1998-01-26 11:40   ` Sebastian Kuzminsky
  0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Kuzminsky @ 1998-01-26 11:40 UTC (permalink / raw)
  To: Fergus Henderson; +Cc: gnu-win32

Fergus Henderson <fjh@cs.mu.OZ.AU> wrote:
] For text mode opens, read() is stripping out \r's.


   It strips our \r when preceeded by \n, and it reports EOF on reading
0x1A...  But, as you say, the O_BINARY flag makes it not mangle the data.
Thanks!




   I will remember that not everyone is Posixally correct.
   I will remember that not everyone is Posixally correct.
   I will remember that not everyone is Posixally correct.
   I will remember that not everyone is Posixally correct.
   ...




Sebastian

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-01-26 11:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-24  7:53 unexpected behavior of read() under win95 Sebastian Kuzminsky
1998-01-24 21:53 ` Fergus Henderson
1998-01-26 11:40   ` Sebastian Kuzminsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).