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