2000-04-14 H.J. Lu <hjl@gnu.org>
[binutils.git] / gas / output-file.c
bloba7df72f29377f74cf0ec7a16e9dadb9e82528698
1 /* output-file.c - Deal with the output file
2 Copyright (C) 1987, 90, 91, 93, 92, 94, 95, 96, 1998
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <stdio.h>
23 #include "as.h"
25 #include "output-file.h"
27 #ifdef BFD_HEADERS
28 #define USE_BFD
29 #endif
31 #ifdef BFD_ASSEMBLER
32 #define USE_BFD
33 #ifndef TARGET_MACH
34 #define TARGET_MACH 0
35 #endif
36 #endif
38 #ifdef USE_BFD
39 #include "bfd.h"
40 bfd *stdoutput;
42 void
43 output_file_create (name)
44 char *name;
46 if (name[0] == '-' && name[1] == '\0')
48 as_fatal (_("Can't open a bfd on stdout %s "), name);
50 else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
52 as_perror (_("FATAL: Can't create %s"), name);
53 exit (EXIT_FAILURE);
55 bfd_set_format (stdoutput, bfd_object);
56 #ifdef BFD_ASSEMBLER
57 bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH);
58 #endif
59 if (flag_traditional_format)
60 stdoutput->flags |= BFD_TRADITIONAL_FORMAT;
63 void
64 output_file_close (filename)
65 char *filename;
67 #ifdef BFD_ASSEMBLER
68 /* Close the bfd. */
69 if (bfd_close (stdoutput) == 0)
71 bfd_perror (filename);
72 as_perror (_("FATAL: Can't close %s\n"), filename);
73 exit (EXIT_FAILURE);
75 #else
76 /* Close the bfd without getting bfd to write out anything by itself */
77 if (bfd_close_all_done (stdoutput) == 0)
79 as_perror (_("FATAL: Can't close %s\n"), filename);
80 exit (EXIT_FAILURE);
82 #endif
83 stdoutput = NULL; /* Trust nobody! */
86 #ifndef BFD_ASSEMBLER
87 void
88 output_file_append (where, length, filename)
89 char *where ATTRIBUTE_UNUSED;
90 long length ATTRIBUTE_UNUSED;
91 char *filename ATTRIBUTE_UNUSED;
93 abort ();
95 #endif
97 #else
99 static FILE *stdoutput;
101 void
102 output_file_create (name)
103 char *name;
105 if (name[0] == '-' && name[1] == '\0')
107 stdoutput = stdout;
108 return;
111 stdoutput = fopen (name, "wb");
113 /* Some systems don't grok "b" in fopen modes. */
114 if (stdoutput == NULL)
115 stdoutput = fopen (name, "w");
117 if (stdoutput == NULL)
119 as_perror (_("FATAL: Can't create %s"), name);
120 exit (EXIT_FAILURE);
124 void
125 output_file_close (filename)
126 char *filename;
128 if (EOF == fclose (stdoutput))
130 as_perror (_("FATAL: Can't close %s"), filename);
131 exit (EXIT_FAILURE);
133 stdoutput = NULL; /* Trust nobody! */
136 void
137 output_file_append (where, length, filename)
138 char *where;
139 long length;
140 char *filename;
142 for (; length; length--, where++)
144 (void) putc (*where, stdoutput);
145 if (ferror (stdoutput))
146 /* if ( EOF == (putc( *where, stdoutput )) ) */
148 as_perror (_("Failed to emit an object byte"), filename);
149 as_fatal (_("Can't continue"));
154 #endif
156 /* end of output-file.c */