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


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

Re: Patch: Initialise new file space to zero for Beos


Hi Ian,

: So I don't think you should write your patch as a change to bfd_seek.
: I think you should write a version of fseek which you put into
: libiberty, and you should fix up the libiberty configuration to build
: that version of fseek on BeOS.  It may turn out to be convenient to
: give it a different name, and to use an appropriate #define when
: building BFD, which would be fine by me.

OK, well I have a patch to libiberty (see below) that creates a new
function called zero_fseek() for BeOS hosts.  I do have two questions
though:

  1. At the moment zero_fseek is BeOS specific, should it be, or
     should there be some kind of test to determine if fseek zeroes a
     file ?  (eg create a file 512 bytes long.  Fill it with non-zero
     bytes.  Truncate it to 1 byte long.  Re-extended it to 512
     bytes.  Read in the 511 bytes and check that they are zero).

  2. How can BFD determine if it needs to use the zero_fseek function?
     At the moment the libiberty patch does not create a
     HAVE_zero_fseek define.  (I could not find a way to do this from
     inside config/mh-beos).  Should bfd_seek() just call zero_fseek
     if __BEOS__ is defined ?

Cheers
	Nick


Index: libiberty/config/mh-beos
===================================================================
RCS file: /cvs/cvsfiles/devo/libiberty/config/mh-beos,v
retrieving revision 1.2
diff -p -r1.2 mh-beos
*** mh-beos	1999/04/11 09:07:48	1.2
--- mh-beos	1999/09/20 09:49:36
***************
*** 4,7 ****
  # limit in BeOS is either increased or made user settable somehow.
  # This probably won't happen until after the DR9 release.
  
! EXTRA_OFILES = alloca.o
--- 4,10 ----
  # limit in BeOS is either increased or made user settable somehow.
  # This probably won't happen until after the DR9 release.
  
! # zero_fseek is included because the standard fseek under BeOS does
! # not initialise new file space to zero (yet).
! 
! EXTRA_OFILES = alloca.o zero_fseek.o

Index: libiberty/Makefile.in
===================================================================
RCS file: /cvs/cvsfiles/devo/libiberty/Makefile.in,v
retrieving revision 1.155
diff -p -r1.155 Makefile.in
*** Makefile.in	1999/07/26 19:44:45	1.155
--- Makefile.in	1999/09/20 09:49:36
*************** xexit.o: $(INCDIR)/libiberty.h
*** 266,268 ****
--- 266,269 ----
  xmalloc.o: $(INCDIR)/libiberty.h
  xstrdup.o: config.h $(INCDIR)/libiberty.h
  xstrerror.o: config.h $(INCDIR)/libiberty.h
+ zero_fseek.o: config.h $(INCDIR)/libiberty.h

Index: include/libiberty.h
===================================================================
RCS file: /cvs/cvsfiles/devo/include/libiberty.h,v
retrieving revision 1.25
diff -p -r1.25 libiberty.h
*** libiberty.h	1999/03/24 01:47:34	1.25
--- libiberty.h	1999/09/20 09:49:36
*************** extern int pexecute PARAMS ((const char 
*** 172,177 ****
--- 172,184 ----
  
  extern int pwait PARAMS ((int, int *, int));
  
+ /* Perform an fseek which initialises new file space to zero.  */
+ #ifdef ANSI_PROTOTYPES
+ /* Get a definition for FILE.  */
+ #include <stdio.h>
+ #endif
+ extern int zero_fseek PARAMS ((FILE *, long, int));
+ 
  #ifdef __cplusplus
  }
  #endif

*** /dev/null	Tue May  5 21:32:27 1998
--- libiberty/zero_fseek.c	Mon Sep 20 10:39:30 1999
***************
*** 0 ****
--- 1,105 ----
+ /* Perform an fseek which explicitly zeros newly created file space.
+    Copyright (C) 1999 Free Software Foundation, Inc.
+    Written by Fred Fish <fnf@www.ninemoons.com>
+    and Nick Clifton <nickc@cygnus.com>
+ 
+ This file is part of the libiberty library.
+ Libiberty is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+ 
+ Libiberty is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Library General Public License for more details.
+ 
+ You should have received a copy of the GNU Library General Public
+ License along with libiberty; see the file COPYING.LIB.  If
+ not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.  */
+ 
+ 
+ /*
+ 
+ NAME
+ 
+ 	zero_fseek -- An fseek that zeroes newly created file space.
+ 
+ SYNOPSIS
+ 
+ 	#include <stdio.h>
+ 
+ 	int zero_fseek (FILE * stream, long offset, int whence)
+ 
+ DESCRIPTION
+ 
+ 	Sets the file pointer for STREAM to OFFSET bytes from the start of
+ 	the file, current file position, or end of the file, depending upon
+ 	whether WHENCE is equal to SEEK_SET, SEEK_CUR or SEEK_END.  If the
+ 	file is extended beyonds its current size, the newly created portion
+ 	of the file is initialised with zero bytes.
+ 
+ 	Returns 0 upon success, -1 upon failure (setting errno).
+ 
+ NOTES
+ 
+ 	For most OSes this function is unnecessary, as the standard fseek
+ 	will have exactly the same behaviour.   This function is provided
+ 	for those OSes where extending a file into new file space does not
+ 	initialise the new space to zero.
+ */
+ 
+ 
+ #include "ansidecl.h"
+ #include "libiberty.h"
+ 
+ #include <stdio.h>
+ 
+ int
+ zero_fseek (stream, offset, whence)
+      FILE * stream;
+      long   offset;
+      int    whence;
+ {
+   file_ptr eof;
+   file_ptr pos;
+ 
+   /* Get the current file position, and the length of the file.  */
+   pos = ftell (stream);
+   fseek (stream, 0L, SEEK_END);
+   eof = ftell (stream);
+ 
+   /* Make everything be in terms of SEEK_SET.  */
+   if (whence == SEEK_CUR)
+     offset += pos;
+   else if (whence == SEEK_END)
+     offset += eof;
+ 
+   /* See if we are extending beyond the current end of the file.  */
+   if (eof < offset)
+     {
+       static char zeros[512];
+       file_ptr diff;
+       
+       diff = offset - eof;
+       
+       while (diff >= sizeof (zeros))
+ 	{
+ 	  fwrite (zeros, sizeof (zeros), 1, stream);
+ 	  diff -= sizeof (zeros);
+ 	}
+       
+       if (diff > 0)
+ 	fwrite (zeros, diff, 1, stream);
+       
+       /* In theory we do not need to perform the fseek now, since the fwrite
+ 	 calls will have moved the file pointer to the correct location.  In
+ 	 practice however we leave the call in, just in case something went
+ 	 wrong with the fwrites and we missed it.  (After all we are not
+ 	 checking their return codes).  */
+     }
+ 
+   /* Now perform the normal fseek, as provided by the host OS.  */
+   return fseek (stream, offset, SEEK_SET);
+ }

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