xref: /freebsd/contrib/libpcap/msdos/bin2c.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <time.h>
5 
6 static void Abort (const char *fmt,...)
7 {
8   va_list args;
9   va_start (args, fmt);
10   vfprintf (stderr, fmt, args);
11   va_end (args);
12   exit (1);
13 }
14 
15 int main (int argc, char **argv)
16 {
17   FILE  *inFile;
18   FILE  *outFile = stdout;
19   time_t now     = time (NULL);
20   int    ch, i;
21 
22   if (argc != 2)
23      Abort ("Usage: %s bin-file [> result]", argv[0]);
24 
25   if ((inFile = fopen(argv[1],"rb")) == NULL)
26      Abort ("Cannot open %s\n", argv[1]);
27 
28   fprintf (outFile,
29            "/* data statements for file %s at %.24s */\n"
30            "/* Generated by BIN2C, G. Vanem 1995 */\n",
31            argv[1], ctime(&now));
32 
33   i = 0;
34   while ((ch = fgetc(inFile)) != EOF)
35   {
36     if (i++ % 12 == 0)
37        fputs ("\n  ", outFile);
38     fprintf (outFile, "0x%02X,", ch);
39   }
40   fputc ('\n', outFile);
41   fclose (inFile);
42   return (0);
43 }
44