output-file.c revision 33965
1178476Sjb/* output-file.c -  Deal with the output file
2178476Sjb   Copyright (C) 1987, 90, 91, 93, 92, 94, 95, 1996
3178476Sjb   Free Software Foundation, Inc.
4178476Sjb
5178476Sjb   This file is part of GAS, the GNU Assembler.
6178476Sjb
7178476Sjb   GAS is free software; you can redistribute it and/or modify
8178476Sjb   it under the terms of the GNU General Public License as published by
9178476Sjb   the Free Software Foundation; either version 2, or (at your option)
10178476Sjb   any later version.
11178476Sjb
12178476Sjb   GAS is distributed in the hope that it will be useful,
13178476Sjb   but WITHOUT ANY WARRANTY; without even the implied warranty of
14178476Sjb   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15178476Sjb   GNU General Public License for more details.
16178476Sjb
17178476Sjb   You should have received a copy of the GNU General Public License
18178476Sjb   along with GAS; see the file COPYING.  If not, write to
19178476Sjb   the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20178476Sjb
21178476Sjb#include <stdio.h>
22178476Sjb
23178476Sjb#include "as.h"
24178476Sjb
25178476Sjb#include "output-file.h"
26178476Sjb
27178476Sjb#ifdef BFD_HEADERS
28178476Sjb#define USE_BFD
29178476Sjb#endif
30178476Sjb
31178476Sjb#ifdef BFD_ASSEMBLER
32178476Sjb#define USE_BFD
33178476Sjb#ifndef TARGET_MACH
34178476Sjb#define TARGET_MACH 0
35178476Sjb#endif
36178476Sjb#endif
37178476Sjb
38178476Sjb#ifdef USE_BFD
39178476Sjb#include "bfd.h"
40178476Sjbbfd *stdoutput;
41178476Sjb
42178476Sjbvoid
43178476Sjboutput_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