Back to index

cell-binutils  2.17cvs20070401
bin2c.c
Go to the documentation of this file.
00001 /* bin2c.c -- dump binary file in hex format
00002    Copyright 2007 Free Software Foundation, Inc.
00003 
00004    This file is part of GNU Binutils.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
00019    02110-1301, USA.  */
00020 
00021 #include "bfd.h"
00022 #include "bucomm.h"
00023 
00024 #if !defined O_BINARY && defined _O_BINARY
00025   /* For MSC-compatible compilers.  */
00026 # define O_BINARY _O_BINARY
00027 # define O_TEXT _O_TEXT
00028 #endif
00029 
00030 #ifdef __BEOS__
00031   /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect.  */
00032 # undef O_BINARY
00033 # undef O_TEXT
00034 #endif
00035 
00036 #if O_BINARY
00037 # ifndef __DJGPP__
00038 #  define setmode _setmode
00039 #  define fileno(_fp) _fileno (_fp)
00040 # endif /* not DJGPP */
00041 # define SET_BINARY(_f) \
00042   do { if (!isatty (_f)) setmode (_f, O_BINARY); } while (0)
00043 #else
00044 # define SET_BINARY(f) (void) 0
00045 # define O_BINARY 0
00046 # define O_TEXT 0
00047 #endif /* O_BINARY */
00048 
00049 int
00050 main (int argc, char *argv[])
00051 {
00052   int c;
00053   int i;
00054 
00055 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
00056   setlocale (LC_MESSAGES, "");
00057 #endif
00058 #if defined (HAVE_SETLOCALE)
00059   setlocale (LC_CTYPE, "");
00060 #endif
00061   bindtextdomain (PACKAGE, LOCALEDIR);
00062   textdomain (PACKAGE);
00063 
00064   if (argc != 1)
00065     {
00066       int ishelp = 0;
00067       int isvers = 0;
00068       FILE *stream;
00069 
00070       if (argc == 2 && argv[1][0] == '-')
00071        {
00072          const char *opt = &argv[1][1];
00073          if (*opt == '-')
00074            ++opt;
00075          ishelp = *opt == 'h' || *opt == 'H';
00076          isvers = *opt == 'v' || *opt == 'V';
00077        }
00078 
00079       if (isvers)
00080        print_version ("bin2c");
00081 
00082       stream = ishelp ? stdout : stderr;
00083       fprintf (stream, _("Usage: %s < input_file > output_file\n"), argv[0]);
00084       fprintf (stream, _("Prints bytes from stdin in hex format.\n"));
00085       exit (!ishelp);
00086     }
00087 
00088   SET_BINARY (fileno (stdin));
00089 
00090   i = 0;
00091   while ((c = getc (stdin)) != EOF)
00092     {
00093       printf ("0x%02x,", c);
00094       if (++i == 16)
00095        {
00096          printf ("\n");
00097          i = 0;
00098        }
00099     }
00100   if (i != 0)
00101     printf ("\n");
00102 
00103   exit (0);
00104 }