-/*proto-internal*
-*i bfd_open_file
-Call the OS to open a file for this BFD. Returns the FILE *
-(possibly null) that results from this operation. Sets up the
-BFD so that future accesses know the file is open. If the FILE *
-returned is null, then there is won't have been put in the cache, so
-it won't have to be removed from it.
-*; PROTO(FILE *, bfd_open_file, (bfd *));
-*-*/
-FILE *
-DEFUN(bfd_open_file, (abfd),
- bfd *abfd)
-{
- abfd->cacheable = true; /* Allow it to be closed later. */
- if(open_files >= BFD_CACHE_MAX_OPEN) {
- close_one();
- }
- switch (abfd->direction) {
- case read_direction:
- case no_direction:
- abfd->iostream = (char *) fopen(abfd->filename, "r");
- break;
- case both_direction:
- case write_direction:
- if (abfd->opened_once == true) {
- abfd->iostream = (char *) fopen(abfd->filename, "r+");
- if (!abfd->iostream) {
- abfd->iostream = (char *) fopen(abfd->filename, "w+");
- }
- } else {
- /*open for creat */
- abfd->iostream = (char *) fopen(abfd->filename, "w");
- abfd->opened_once = true;
+ switch (abfd->direction)
+ {
+ case read_direction:
+ case no_direction:
+ abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
+ break;
+ case both_direction:
+ case write_direction:
+ if (abfd->opened_once == true)
+ {
+ abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
+ if (abfd->iostream == NULL)
+ abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
+ }
+ else
+ {
+ /* Create the file.
+
+ Some operating systems won't let us overwrite a running
+ binary. For them, we want to unlink the file first.
+
+ However, gcc 2.95 will create temporary files using
+ O_EXCL and tight permissions to prevent other users from
+ substituting other .o files during the compilation. gcc
+ will then tell the assembler to use the newly created
+ file as an output file. If we unlink the file here, we
+ open a brief window when another user could still
+ substitute a file.
+
+ So we unlink the output file if and only if it has
+ non-zero size. */
+#ifndef __MSDOS__
+ /* Don't do this for MSDOS: it doesn't care about overwriting
+ a running binary, but if this file is already open by
+ another BFD, we will be in deep trouble if we delete an
+ open file. In fact, objdump does just that if invoked with
+ the --info option. */
+ struct stat s;
+
+ if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
+ unlink (abfd->filename);
+#endif
+ abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
+ abfd->opened_once = true;
+ }
+ break;