Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / as / output-file.c
1 /* output-file.c -  Deal with the output file
2    Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /*
21  * Confines all details of emitting object bytes to this module.
22  * All O/S specific crocks should live here.
23  * What we lose in "efficiency" we gain in modularity.
24  * Note we don't need to #include the "as.h" file. No common coupling!
25  */
26
27 /* note that we do need config info.  xoxorich. */
28
29 #ifndef lint
30 static char rcsid[] = "$FreeBSD: src/gnu/usr.bin/as/output-file.c,v 1.6 1999/08/27 23:34:20 peter Exp $";
31 #endif
32
33 #include <stdio.h>
34
35 #include "as.h"
36
37 #include "output-file.h"
38 #ifdef BFD_HEADERS
39 #include "bfd.h"
40 bfd *stdoutput;
41 void output_file_create(name)
42 char *name;
43 {
44         if (name[0] == '-' && name[1] == '\0')  {
45                 as_perror("FATAL: Can't open a bfd on stdout %s ", name);
46         }
47         else if ( ! (stdoutput = bfd_openw( name, TARGET_FORMAT )) )
48             {
49                     as_perror ("FATAL: Can't create %s", name);
50                     exit(42);
51             }
52         bfd_set_format(stdoutput, bfd_object);
53 }
54 /* output_file_create() */
55
56
57 void output_file_close(filename)
58 char *filename;
59 {
60         /* Close the bfd without getting bfd to write out anything by itself */
61         if ( bfd_close_all_done( stdoutput ) == 0 )
62             {
63                     as_perror ("FATAL: Can't close %s\n", filename);
64                     exit(42);
65             }
66         stdoutput = NULL;               /* Trust nobody! */
67 }                               /* output_file_close() */
68
69 void output_file_append(where, length, filename)
70 char *where;
71 long length;
72 char *filename;
73 {
74         abort(); /* Never do this */
75 }
76
77 #else
78
79 static FILE *stdoutput;
80
81 void output_file_create(name)
82 char *name;
83 {
84         if (name[0] == '-' && name[1] == '\0')
85             stdoutput=stdout;
86         else if (!(stdoutput = fopen(name, "wb"))) {
87                 as_perror("FATAL: Can't create %s", name);
88                 exit(42);
89         }
90 } /* output_file_create() */
91
92
93
94 void output_file_close(filename)
95 char *filename;
96 {
97         if (EOF == fclose(stdoutput)) {
98                 as_perror ("FATAL: Can't close %s", filename);
99                 exit(42);
100         }
101         stdoutput = NULL;               /* Trust nobody! */
102 } /* output_file_close() */
103
104 void output_file_append(where, length, filename)
105 char *where;
106 long length;
107 char *filename;
108 {
109
110         for (; length; length--, where++) {
111                 (void) putc(*where, stdoutput);
112                 if (ferror(stdoutput))
113                     /* if ( EOF == (putc( *where, stdoutput )) ) */
114                     {
115                             as_perror("Failed to emit an object byte", filename);
116                             as_fatal("Can't continue");
117                     }
118         }
119 } /* output_file_append() */
120 #endif
121
122 /* end of output-file.c */