unexpected behavior of read() under win95

Sebastian Kuzminsky kuzminsk@taussky.cs.colorado.edu
Sat Jan 24 07:53:00 GMT 1998

   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");

   file_fd = open(argv[1], O_RDONLY);
   if (file_fd == -1) {
      fprintf(stderr, "cannot open %s:  ", argv[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);


----- 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

   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.


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

More information about the Cygwin mailing list