aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Mitchell <mark@codesourcery.com>2005-06-07 22:53:32 +0000
committerMark Mitchell <mark@codesourcery.com>2005-06-07 22:53:32 +0000
commit2d0123b7851267f634ef63c15c43172e2136bc78 (patch)
tree842bbc2f0305c50b4e004cce1f461713e8720208
parent4a5329c63abbeb362968edcc3cb6df0ca754e1ee (diff)
downloadgdb-2d0123b7851267f634ef63c15c43172e2136bc78.zip
gdb-2d0123b7851267f634ef63c15c43172e2136bc78.tar.gz
gdb-2d0123b7851267f634ef63c15c43172e2136bc78.tar.bz2
* opncls.c (bfd_fopen): New API.
(bfd_openr): Use it. (bfd_fdopenr): Likewise. * bfd-in2.h: Regenerated.
-rw-r--r--bfd/ChangeLog7
-rw-r--r--bfd/bfd-in2.h3
-rw-r--r--bfd/opncls.c128
3 files changed, 79 insertions, 59 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 3f4898e..1845735 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,10 @@
+2005-06-07 Mark Mitchell <mark@codesourcery.com>
+
+ * opncls.c (bfd_fopen): New API.
+ (bfd_openr): Use it.
+ (bfd_fdopenr): Likewise.
+ * bfd-in2.h: Regenerated.
+
2005-06-07 Aldy Hernandez <aldyh@redhat.com>
Michael Snyder <msnyder@redhat.com>
Stan Cox <scox@redhat.com>
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index ec19440..739277c 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -893,6 +893,9 @@ extern struct coff_comdat_info *bfd_coff_get_comdat_section
void bfd_init (void);
/* Extracted from opncls.c. */
+bfd *bfd_fopen (const char *filename, const char *target,
+ const char *mode, int fd);
+
bfd *bfd_openr (const char *filename, const char *target);
bfd *bfd_fdopenr (const char *filename, const char *target, int fd);
diff --git a/bfd/opncls.c b/bfd/opncls.c
index 0e4eb8a..e675510 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -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);
}
/*