1214571Sdim/* bin2c.c -- dump binary file in hex format
2214571Sdim   Copyright 2007 Free Software Foundation, Inc.
3214571Sdim
4214571Sdim   This file is part of GNU Binutils.
5214571Sdim
6214571Sdim   This program is free software; you can redistribute it and/or modify
7214571Sdim   it under the terms of the GNU General Public License as published by
8214571Sdim   the Free Software Foundation; either version 2 of the License, or
9214571Sdim   (at your option) any later version.
10214571Sdim
11214571Sdim   This program is distributed in the hope that it will be useful,
12214571Sdim   but WITHOUT ANY WARRANTY; without even the implied warranty of
13214571Sdim   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14214571Sdim   GNU General Public License for more details.
15214571Sdim
16214571Sdim   You should have received a copy of the GNU General Public License
17214571Sdim   along with this program; if not, write to the Free Software
18214571Sdim   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19214571Sdim   02110-1301, USA.  */
20214571Sdim
21214571Sdim#include "sysdep.h"
22214571Sdim#include "bfd.h"
23214571Sdim#include "bucomm.h"
24214571Sdim
25214571Sdim#if !defined O_BINARY && defined _O_BINARY
26214571Sdim  /* For MSC-compatible compilers.  */
27214571Sdim# define O_BINARY _O_BINARY
28214571Sdim# define O_TEXT _O_TEXT
29214571Sdim#endif
30214571Sdim
31214571Sdim#ifdef __BEOS__
32214571Sdim  /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect.  */
33214571Sdim# undef O_BINARY
34214571Sdim# undef O_TEXT
35214571Sdim#endif
36214571Sdim
37214571Sdim#if O_BINARY
38214571Sdim# ifndef __DJGPP__
39214571Sdim#  define setmode _setmode
40214571Sdim#  define fileno(_fp) _fileno (_fp)
41214571Sdim# endif /* not DJGPP */
42214571Sdim# define SET_BINARY(_f) \
43214571Sdim  do { if (!isatty (_f)) setmode (_f, O_BINARY); } while (0)
44214571Sdim#else
45214571Sdim# define SET_BINARY(f) (void) 0
46214571Sdim# define O_BINARY 0
47214571Sdim# define O_TEXT 0
48214571Sdim#endif /* O_BINARY */
49214571Sdim
50214571Sdimint
51214571Sdimmain (int argc, char *argv[])
52214571Sdim{
53214571Sdim  int c;
54214571Sdim  int i;
55214571Sdim
56214571Sdim#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
57214571Sdim  setlocale (LC_MESSAGES, "");
58214571Sdim#endif
59214571Sdim#if defined (HAVE_SETLOCALE)
60214571Sdim  setlocale (LC_CTYPE, "");
61214571Sdim#endif
62214571Sdim  bindtextdomain (PACKAGE, LOCALEDIR);
63214571Sdim  textdomain (PACKAGE);
64214571Sdim
65214571Sdim  if (argc != 1)
66214571Sdim    {
67214571Sdim      int ishelp = 0;
68214571Sdim      int isvers = 0;
69214571Sdim      FILE *stream;
70214571Sdim
71214571Sdim      if (argc == 2 && argv[1][0] == '-')
72214571Sdim	{
73214571Sdim	  const char *opt = &argv[1][1];
74214571Sdim	  if (*opt == '-')
75214571Sdim	    ++opt;
76214571Sdim	  ishelp = *opt == 'h' || *opt == 'H';
77214571Sdim	  isvers = *opt == 'v' || *opt == 'V';
78214571Sdim	}
79214571Sdim
80214571Sdim      if (isvers)
81214571Sdim	print_version ("bin2c");
82214571Sdim
83214571Sdim      stream = ishelp ? stdout : stderr;
84214571Sdim      fprintf (stream, _("Usage: %s < input_file > output_file\n"), argv[0]);
85214571Sdim      fprintf (stream, _("Prints bytes from stdin in hex format.\n"));
86214571Sdim      exit (!ishelp);
87214571Sdim    }
88214571Sdim
89214571Sdim  SET_BINARY (fileno (stdin));
90214571Sdim
91214571Sdim  i = 0;
92214571Sdim  while ((c = getc (stdin)) != EOF)
93214571Sdim    {
94214571Sdim      printf ("0x%02x,", c);
95214571Sdim      if (++i == 16)
96214571Sdim	{
97214571Sdim	  printf ("\n");
98214571Sdim	  i = 0;
99214571Sdim	}
100214571Sdim    }
101214571Sdim  if (i != 0)
102214571Sdim    printf ("\n");
103214571Sdim
104214571Sdim  exit (0);
105214571Sdim}
106