This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

B20: large fread() calls fail


Hello,

My name is Nathan Strong, I'm one of several coders working on a MUD. I have
a bit of code that compiles fine on both its native Linux platform and on
Cygwin b20, but while it executes fine in Linux, on cygwin it fails. It
happens when a large amount of data needs to be read from a file handle.
UNIX doesn't complain and reads the data without complaining; cygwin returns
an error. With regards to the MUD, this results in necessary data files not
being loaded, which essentially makes it impossible to test my code before
it is uploaded to the server.

Here is the function that actually does the allocation:

typedef struct area_type AREA_TYPE;

struct area_type
{
  char *pString;
  char *text;
  int line;
};

/* OPEN_RESERVE() and CLOSE_RESERVE() are macros that close a free file
handle
 * open in the event of high load, so that the MUD can always write its
data.
 * Instead of using fclose() and fopen(), I wrap them with these macros so
that
 * double-fclose()ing fpReserve doesn't crash the MUD.
 */

AREA_TYPE *read_file(char *filename)
{
  AREA_TYPE *pArea;
  long size;
  struct stat sbuf;
  FILE *fp;

  if( stat(filename, &sbuf) )
  {
    perror(filename);
    return NULL;
  }

  size = (long) sbuf.st_size;

  if( (pArea = (AREA_TYPE *) malloc( sizeof(*pArea) )) == NULL
  ||  (pArea->pString = (char *) malloc(size) ) == NULL )
  {
    bug("Couldn't allocate %d bytes of memory.", (int) size);
    abort();
  }

  CLOSE_RESERVE(fpReserve);
  if( (fp = fopen(filename, "r")) == NULL )
  {
    perror(filename);
    return NULL;
  }

  if( fread(pArea->pString, 1, size, fp) < size ) /* this fails sometimes */
  {
    bug("Error reading area file into memory.", 0);
    fclose(fp);
    OPEN_RESERVE(fpReserve);
    return NULL;
  }
  fclose(fp);
  OPEN_RESERVE(fpReserve);
  pArea->text = pArea->pString;
  pArea->line = 1;

  return pArea;
}


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]