home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / amiga / binutils-2.5.2-src.lha / binutils-2.5.2 / gas / output-file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  3.2 KB  |  155 lines

  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. #include <stdio.h>
  21.  
  22. #include "as.h"
  23.  
  24. #include "output-file.h"
  25.  
  26. #ifdef BFD_HEADERS
  27. #define USE_BFD
  28. #endif
  29.  
  30. #ifdef BFD_ASSEMBLER
  31. #define USE_BFD
  32. #ifndef TARGET_MACH
  33. #define TARGET_MACH 0
  34. #endif
  35. #endif
  36.  
  37. #ifdef USE_BFD
  38. #include "bfd.h"
  39. bfd *stdoutput;
  40.  
  41. void
  42. output_file_create (name)
  43.      char *name;
  44. {
  45.   if (name[0] == '-' && name[1] == '\0')
  46.     {
  47.       as_fatal ("Can't open a bfd on stdout %s ", name);
  48.     }
  49.   else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
  50.     {
  51.       bfd_perror (name);
  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.  
  61. void
  62. output_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
  85. void
  86. output_file_append (where, length, filename)
  87.      char *where;
  88.      long length;
  89.      char *filename;
  90. {
  91.   abort ();
  92. }
  93. #endif
  94.  
  95. #else
  96.  
  97. static FILE *stdoutput;
  98.  
  99. void
  100. output_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.  
  122. void
  123. output_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.  
  134. void
  135. output_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.