Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* output-file.c - Deal with the output file |
f740e790 | 2 | Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1996, 1998, 1999, 2001 |
252b5132 RH |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
f740e790 NC |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
20 | 02111-1307, USA. */ | |
252b5132 RH |
21 | |
22 | #include <stdio.h> | |
23 | ||
24 | #include "as.h" | |
25 | ||
26 | #include "output-file.h" | |
27 | ||
28 | #ifdef BFD_HEADERS | |
29 | #define USE_BFD | |
30 | #endif | |
31 | ||
32 | #ifdef BFD_ASSEMBLER | |
33 | #define USE_BFD | |
34 | #ifndef TARGET_MACH | |
35 | #define TARGET_MACH 0 | |
36 | #endif | |
37 | #endif | |
38 | ||
39 | #ifdef USE_BFD | |
40 | #include "bfd.h" | |
41 | bfd *stdoutput; | |
42 | ||
43 | void | |
44 | output_file_create (name) | |
45 | char *name; | |
46 | { | |
47 | if (name[0] == '-' && name[1] == '\0') | |
f740e790 NC |
48 | as_fatal (_("Can't open a bfd on stdout %s "), name); |
49 | ||
252b5132 RH |
50 | else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT))) |
51 | { | |
52 | as_perror (_("FATAL: Can't create %s"), name); | |
53 | exit (EXIT_FAILURE); | |
54 | } | |
f740e790 | 55 | |
252b5132 RH |
56 | bfd_set_format (stdoutput, bfd_object); |
57 | #ifdef BFD_ASSEMBLER | |
58 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH); | |
59 | #endif | |
60 | if (flag_traditional_format) | |
61 | stdoutput->flags |= BFD_TRADITIONAL_FORMAT; | |
62 | } | |
63 | ||
64 | void | |
65 | output_file_close (filename) | |
66 | char *filename; | |
67 | { | |
68 | #ifdef BFD_ASSEMBLER | |
69 | /* Close the bfd. */ | |
70 | if (bfd_close (stdoutput) == 0) | |
71 | { | |
72 | bfd_perror (filename); | |
73 | as_perror (_("FATAL: Can't close %s\n"), filename); | |
74 | exit (EXIT_FAILURE); | |
75 | } | |
76 | #else | |
f740e790 | 77 | /* Close the bfd without getting bfd to write out anything by itself. */ |
252b5132 RH |
78 | if (bfd_close_all_done (stdoutput) == 0) |
79 | { | |
80 | as_perror (_("FATAL: Can't close %s\n"), filename); | |
81 | exit (EXIT_FAILURE); | |
82 | } | |
83 | #endif | |
f740e790 | 84 | stdoutput = NULL; /* Trust nobody! */ |
252b5132 RH |
85 | } |
86 | ||
87 | #ifndef BFD_ASSEMBLER | |
88 | void | |
89 | output_file_append (where, length, filename) | |
a04b544b ILT |
90 | char *where ATTRIBUTE_UNUSED; |
91 | long length ATTRIBUTE_UNUSED; | |
92 | char *filename ATTRIBUTE_UNUSED; | |
252b5132 RH |
93 | { |
94 | abort (); | |
95 | } | |
96 | #endif | |
97 | ||
98 | #else | |
99 | ||
100 | static FILE *stdoutput; | |
101 | ||
102 | void | |
103 | output_file_create (name) | |
104 | char *name; | |
105 | { | |
106 | if (name[0] == '-' && name[1] == '\0') | |
107 | { | |
108 | stdoutput = stdout; | |
109 | return; | |
110 | } | |
111 | ||
f740e790 | 112 | stdoutput = fopen (name, FOPEN_WB); |
252b5132 RH |
113 | |
114 | /* Some systems don't grok "b" in fopen modes. */ | |
f740e790 | 115 | /* XXX - is this still necessary now that we use FOPEN_WB ? */ |
252b5132 | 116 | if (stdoutput == NULL) |
f740e790 | 117 | stdoutput = fopen (name, FOPEN_W); |
252b5132 RH |
118 | |
119 | if (stdoutput == NULL) | |
120 | { | |
121 | as_perror (_("FATAL: Can't create %s"), name); | |
122 | exit (EXIT_FAILURE); | |
123 | } | |
124 | } | |
125 | ||
126 | void | |
127 | output_file_close (filename) | |
128 | char *filename; | |
129 | { | |
130 | if (EOF == fclose (stdoutput)) | |
131 | { | |
132 | as_perror (_("FATAL: Can't close %s"), filename); | |
133 | exit (EXIT_FAILURE); | |
134 | } | |
f740e790 NC |
135 | |
136 | /* Trust nobody! */ | |
137 | stdoutput = NULL; | |
252b5132 RH |
138 | } |
139 | ||
140 | void | |
141 | output_file_append (where, length, filename) | |
f740e790 NC |
142 | char * where; |
143 | long length; | |
144 | char * filename; | |
252b5132 RH |
145 | { |
146 | for (; length; length--, where++) | |
147 | { | |
148 | (void) putc (*where, stdoutput); | |
f740e790 | 149 | |
252b5132 RH |
150 | if (ferror (stdoutput)) |
151 | /* if ( EOF == (putc( *where, stdoutput )) ) */ | |
152 | { | |
153 | as_perror (_("Failed to emit an object byte"), filename); | |
154 | as_fatal (_("Can't continue")); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | #endif | |
160 |