delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/08/08/20:28:54

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>,
<http://sourceware.cygnus.com/ml/#faqs>
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <000501bee1fd$f645d620$5001010a@gblues>
From: "Nathan Strong" <gblues AT jps DOT net>
To: <cygwin AT sourceware DOT cygnus DOT com>
Subject: B20: large fread() calls fail
Date: Sun, 8 Aug 1999 17:27:26 -0700
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300

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 AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019