/* MAKE ARRAY * Converts any given file into a C array * * Sean Middleditch * DEDICATED TO THE PUBLIC DOMAIN - September, 2008 * * Usage: * ./marray */ #include #include #include int main (int argc, char **argv) { FILE *in; int byte, col; /* check usage */ if (argc != 3) { fprintf(stderr, "Usage:\n"); fprintf(stderr, "./marray \n"); fprintf(stderr, " The input file to be converted\n"); fprintf(stderr, " The name of the array in the output\n"); return 1; } /* open file */ in = fopen(argv[1], "r"); if (!in) { fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(errno)); return 1; } /* output header */ printf("const char %s[] = {\n ", argv[2]); /* read in bytes, and spit them out */ col = 0; while ((byte = fgetc(in)) != EOF) { printf(" '\\x%02x',", byte); if (++col == 9) { printf("\n "); col = 0; } } /* output footer, cleanup */ printf(" '\\0'\n};\nconst unsigned int %s_len = sizeof(%s)-1;\n", argv[2], argv[2]); fclose(in); return 0; }