delorie.com/djgpp/bugs/show.cgi   search  
Bug 000316

When Created: 01/28/2000 15:00:43
Against DJGPP version: 2.02
By whom: Keith.Kalski@usa.net
Abstract: when reading binary files (executables, etc) read() hits EOF before the end of file
Basically, I was attempting to create something that copied files over, and it
works fine for any non-binary files, but only reads part-way into a binary file.

My code was very basic and should work, I have check the return values of each
of the functions to make sure everything else works properly and it is. 
My code looks basically like this:
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char **argv)
{
  int i, numread, fd, fd2;
  char buf[1024];

  if (argc<3)
  {
    printf("Usage: %s <source-file> <destination-file>\n", argv[0]);
    exit(1);
  }

  fd=open(argv[1], O_RDONLY, 0);
  fd2=open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0777);

  while((numread=read(fd, buf, 1024))>0)
  {
      write(fd2, buf, numread);
  }

  close(fd);
  close(fd2);
  exit(0);
}

Note added: 02/04/2000 15:00:17
By whom: eliz@is.elta.co.il
This is not a bug in DJGPP, but a programmatic error in the test
program cited by the report author: binary files need to be open
using the O_BINARY flag.  Otherwise, they are treated as text files,
and will have all their CR characters stripped and the read operation
will stop upon seeing the first ^Z character.

This is explained further in the FAQ.

I'm closing this report.

Closed on 02/04/2000 15:00:10: Bug in user code, not in DJGPP.
By whom: eliz@is.elta.co.il



  webmaster     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010