* opncls.c (bfd_fopen): New API.
[deliverable/binutils-gdb.git] / bfd / opncls.c
index 501c56865c43b3f5062be2d5f7f2d6762aada1e4..e6755109c5a1ad9b270b79d67a3358a9c208129a 100644 (file)
@@ -1,6 +1,6 @@
 /* opncls.c -- open and close a BFD.
    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
-   2001, 2002, 2003, 2004
+   2001, 2002, 2003, 2004, 2005
    Free Software Foundation, Inc.
 
    Written by Cygnus Support.
@@ -19,7 +19,7 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
@@ -77,7 +77,7 @@ _bfd_new_bfd (void)
       return NULL;
     }
   nbfd->sections = NULL;
-  nbfd->section_tail = &nbfd->sections;
+  nbfd->section_last = NULL;
   nbfd->format = bfd_unknown;
   nbfd->my_archive = NULL;
   nbfd->origin = 0;
@@ -128,14 +128,18 @@ SECTION
 
 /*
 FUNCTION
-       bfd_openr
+       bfd_fopen
 
 SYNOPSIS
-       bfd *bfd_openr (const char *filename, const char *target);
+       bfd *bfd_fopen (const char *filename, const char *target,
+                        const char *mode, int fd);
 
 DESCRIPTION
-       Open the file @var{filename} (using <<fopen>>) with the target
-       @var{target}.  Return a pointer to the created BFD.
+       Open the file @var{filename} with the target @var{target}.
+       Return a pointer to the created BFD.  If @var{fd} is not -1,
+       then <<fdopen>> is used to open the file; otherwise, <<fopen>>
+       is used.  @var{mode} is passed directly to <<fopen>> or
+       <<fdopen>>. 
 
        Calls <<bfd_find_target>>, so @var{target} is interpreted as by
        that function.
@@ -146,11 +150,13 @@ DESCRIPTION
 */
 
 bfd *
-bfd_openr (const char *filename, const char *target)
+bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
 {
   bfd *nbfd;
   const bfd_target *target_vec;
 
+  bfd_set_error (bfd_error_system_call);
+
   nbfd = _bfd_new_bfd ();
   if (nbfd == NULL)
     return NULL;
@@ -161,21 +167,67 @@ bfd_openr (const char *filename, const char *target)
       _bfd_delete_bfd (nbfd);
       return NULL;
     }
+  
+#ifdef HAVE_FDOPEN
+  if (fd != -1)
+    nbfd->iostream = fdopen (fd, mode);
+  else
+#endif
+    nbfd->iostream = fopen (filename, mode);
+  if (nbfd->iostream == NULL)
+    {
+      _bfd_delete_bfd (nbfd);
+      return NULL;
+    }
 
+  /* OK, put everything where it belongs.  */
   nbfd->filename = filename;
-  nbfd->direction = read_direction;
 
-  if (bfd_open_file (nbfd) == NULL)
+  /* Figure out whether the user is opening the file for reading,
+     writing, or both, by looking at the MODE argument.  */
+  if ((mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a') 
+      && mode[1] == '+')
+    nbfd->direction = both_direction;
+  else if (mode[0] == 'r')
+    nbfd->direction = read_direction;
+  else
+    nbfd->direction = write_direction;
+
+  if (! bfd_cache_init (nbfd))
     {
-      /* File didn't exist, or some such.  */
-      bfd_set_error (bfd_error_system_call);
       _bfd_delete_bfd (nbfd);
       return NULL;
     }
+  nbfd->opened_once = TRUE;
 
   return nbfd;
 }
 
+/*
+FUNCTION
+       bfd_openr
+
+SYNOPSIS
+       bfd *bfd_openr (const char *filename, const char *target);
+
+DESCRIPTION
+       Open the file @var{filename} (using <<fopen>>) with the target
+       @var{target}.  Return a pointer to the created BFD.
+
+       Calls <<bfd_find_target>>, so @var{target} is interpreted as by
+       that function.
+
+       If <<NULL>> is returned then an error has occured.   Possible errors
+       are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
+       <<system_call>> error.
+*/
+
+bfd *
+bfd_openr (const char *filename, const char *target)
+{
+  return bfd_fopen (filename, target, FOPEN_RB, -1);
+}
+
 /* Don't try to `optimize' this function:
 
    o - We lock using stack space so that interrupting the locking
@@ -212,72 +264,30 @@ DESCRIPTION
 bfd *
 bfd_fdopenr (const char *filename, const char *target, int fd)
 {
-  bfd *nbfd;
-  const bfd_target *target_vec;
+  const char *mode;
+#if defined(HAVE_FCNTL) && defined(F_GETFL)
   int fdflags;
+#endif
 
   bfd_set_error (bfd_error_system_call);
 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
-  fdflags = O_RDWR;                    /* Assume full access.  */
+  mode = FOPEN_RUB; /* Assume full access.  */
 #else
   fdflags = fcntl (fd, F_GETFL, NULL);
-#endif
   if (fdflags == -1)
     return NULL;
 
-  nbfd = _bfd_new_bfd ();
-  if (nbfd == NULL)
-    return NULL;
-
-  target_vec = bfd_find_target (target, nbfd);
-  if (target_vec == NULL)
-    {
-      _bfd_delete_bfd (nbfd);
-      return NULL;
-    }
-
-#ifndef HAVE_FDOPEN
-  nbfd->iostream = fopen (filename, FOPEN_RB);
-#else
   /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
   switch (fdflags & (O_ACCMODE))
     {
-    case O_RDONLY: nbfd->iostream = fdopen (fd, FOPEN_RB);   break;
-    case O_WRONLY: nbfd->iostream = fdopen (fd, FOPEN_RUB);  break;
-    case O_RDWR:   nbfd->iostream = fdopen (fd, FOPEN_RUB);  break;
+    case O_RDONLY: mode = FOPEN_RB;
+    case O_WRONLY: mode = FOPEN_RUB;
+    case O_RDWR:   mode = FOPEN_RUB;
     default: abort ();
     }
 #endif
 
-  if (nbfd->iostream == NULL)
-    {
-      _bfd_delete_bfd (nbfd);
-      return NULL;
-    }
-
-  /* OK, put everything where it belongs.  */
-  nbfd->filename = filename;
-
-  /* As a special case we allow a FD open for read/write to
-     be written through, although doing so requires that we end
-     the previous clause with a preposition.  */
-  /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
-  switch (fdflags & (O_ACCMODE))
-    {
-    case O_RDONLY: nbfd->direction = read_direction; break;
-    case O_WRONLY: nbfd->direction = write_direction; break;
-    case O_RDWR: nbfd->direction = both_direction; break;
-    default: abort ();
-    }
-
-  if (! bfd_cache_init (nbfd))
-    {
-      _bfd_delete_bfd (nbfd);
-      return NULL;
-    }
-  nbfd->opened_once = TRUE;
-
-  return nbfd;
+  return bfd_fopen (filename, target, mode, fd);
 }
 
 /*
@@ -405,7 +415,7 @@ static file_ptr
 opncls_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
 {
   struct opncls *vec = abfd->iostream;
-  file_ptr nread = vec->pread (abfd, vec->stream, buf, nbytes, vec->where);
+  file_ptr nread = (vec->pread) (abfd, vec->stream, buf, nbytes, vec->where);
   if (nread < 0)
     return nread;
   vec->where += nread;
@@ -428,7 +438,7 @@ opncls_bclose (struct bfd *abfd)
      free it.  */
   int status = 0;
   if (vec->close != NULL)
-    status = vec->close (abfd, vec->stream);
+    status = (vec->close) (abfd, vec->stream);
   abfd->iostream = NULL;
   return status;
 }
@@ -820,7 +830,6 @@ DESCRIPTION
        <<abfd>> and return a pointer to it.
 */
 
-
 void *
 bfd_alloc (bfd *abfd, bfd_size_type size)
 {
@@ -838,6 +847,18 @@ bfd_alloc (bfd *abfd, bfd_size_type size)
   return ret;
 }
 
+/*
+INTERNAL_FUNCTION
+       bfd_zalloc
+
+SYNOPSIS
+       void *bfd_zalloc (bfd *abfd, bfd_size_type wanted);
+
+DESCRIPTION
+       Allocate a block of @var{wanted} bytes of zeroed memory
+       attached to <<abfd>> and return a pointer to it.
+*/
+
 void *
 bfd_zalloc (bfd *abfd, bfd_size_type size)
 {
@@ -980,6 +1001,7 @@ get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
   unsigned long crc32;
   bfd_byte *contents;
   int crc_offset;
+  char *name;
 
   BFD_ASSERT (abfd);
   BFD_ASSERT (crc32_out);
@@ -997,13 +1019,14 @@ get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
     }
 
   /* Crc value is stored after the filename, aligned up to 4 bytes.  */
-  crc_offset = strlen (contents) + 1;
+  name = (char *) contents;
+  crc_offset = strlen (name) + 1;
   crc_offset = (crc_offset + 3) & ~3;
 
   crc32 = bfd_get_32 (abfd, contents + crc_offset);
 
   *crc32_out = crc32;
-  return contents;
+  return name;
 }
 
 /*
@@ -1022,7 +1045,7 @@ DESCRIPTION
 static bfd_boolean
 separate_debug_file_exists (const char *name, const unsigned long crc)
 {
-  static char buffer [8 * 1024];
+  static unsigned char buffer [8 * 1024];
   unsigned long file_crc = 0;
   int fd;
   bfd_size_type count;
@@ -1189,10 +1212,6 @@ RETURNS
 char *
 bfd_follow_gnu_debuglink (bfd *abfd, const char *dir)
 {
-#if 0 /* Disabled until DEBUGDIR can be defined by configure.in.  */
-  if (dir == NULL)
-    dir = DEBUGDIR;
-#endif
   return find_separate_debug_file (abfd, dir);
 }
 
@@ -1290,7 +1309,7 @@ bfd_fill_in_gnu_debuglink_section (bfd *abfd,
   char * contents;
   bfd_size_type crc_offset;
   FILE * handle;
-  static char buffer[8 * 1024];
+  static unsigned char buffer[8 * 1024];
   size_t count;
 
   if (abfd == NULL || sect == NULL || filename == NULL)
This page took 0.027202 seconds and 4 git commands to generate.