bin2c.c 891 B

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