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