xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/bin2c.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* bin2c.c -- dump binary file in hex format
2*e992f068Schristos    Copyright (C) 2007-2022 Free Software Foundation, Inc.
375fd0b74Schristos 
475fd0b74Schristos    This file is part of GNU Binutils.
575fd0b74Schristos 
675fd0b74Schristos    This program is free software; you can redistribute it and/or modify
775fd0b74Schristos    it under the terms of the GNU General Public License as published by
875fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
975fd0b74Schristos    (at your option) any later version.
1075fd0b74Schristos 
1175fd0b74Schristos    This program is distributed in the hope that it will be useful,
1275fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1375fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1475fd0b74Schristos    GNU General Public License for more details.
1575fd0b74Schristos 
1675fd0b74Schristos    You should have received a copy of the GNU General Public License
1775fd0b74Schristos    along with this program; if not, write to the Free Software
1875fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
1975fd0b74Schristos    02110-1301, USA.  */
2075fd0b74Schristos 
2175fd0b74Schristos #include <stdio.h>
2275fd0b74Schristos #include <stdlib.h>
2375fd0b74Schristos #include "binary-io.h"
2475fd0b74Schristos 
2575fd0b74Schristos int
main(int argc,char * argv[])2675fd0b74Schristos main (int argc, char *argv[])
2775fd0b74Schristos {
2875fd0b74Schristos   int c;
2975fd0b74Schristos   int i;
3075fd0b74Schristos 
3175fd0b74Schristos   if (argc != 1)
3275fd0b74Schristos     {
3375fd0b74Schristos       int ishelp = 0;
3475fd0b74Schristos       FILE *stream;
3575fd0b74Schristos 
3675fd0b74Schristos       if (argc == 2 && argv[1][0] == '-')
3775fd0b74Schristos 	{
3875fd0b74Schristos 	  const char *opt = &argv[1][1];
3975fd0b74Schristos 	  if (*opt == '-')
4075fd0b74Schristos 	    ++opt;
4175fd0b74Schristos 	  ishelp = *opt == 'h' || *opt == 'H';
4275fd0b74Schristos 	}
4375fd0b74Schristos 
4475fd0b74Schristos       stream = ishelp ? stdout : stderr;
4575fd0b74Schristos       fprintf (stream, "Usage: %s < input_file > output_file\n", argv[0]);
4675fd0b74Schristos       fprintf (stream, "Prints bytes from stdin in hex format.\n");
4775fd0b74Schristos       exit (!ishelp);
4875fd0b74Schristos     }
4975fd0b74Schristos 
5075fd0b74Schristos   SET_BINARY (fileno (stdin));
5175fd0b74Schristos 
5275fd0b74Schristos   i = 0;
5375fd0b74Schristos   while ((c = getc (stdin)) != EOF)
5475fd0b74Schristos     {
5575fd0b74Schristos       printf ("0x%02x,", c);
5675fd0b74Schristos       if (++i == 16)
5775fd0b74Schristos 	{
5875fd0b74Schristos 	  printf ("\n");
5975fd0b74Schristos 	  i = 0;
6075fd0b74Schristos 	}
6175fd0b74Schristos     }
6275fd0b74Schristos   if (i != 0)
6375fd0b74Schristos     printf ("\n");
6475fd0b74Schristos 
6575fd0b74Schristos   exit (0);
6675fd0b74Schristos }
67