xref: /linux/scripts/bin2c.c (revision c417fbce)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Unloved program to convert a binary on stdin to a C include on stdout
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Jan 1999 Matt Mackall <mpm@selenic.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This software may be used and distributed according to the terms
71da177e4SLinus Torvalds  * of the GNU General Public License, incorporated herein by reference.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <stdio.h>
111da177e4SLinus Torvalds 
main(int argc,char * argv[])121da177e4SLinus Torvalds int main(int argc, char *argv[])
131da177e4SLinus Torvalds {
14*8370edeaSVivek Goyal 	int ch, total = 0;
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds 	if (argc > 1)
171da177e4SLinus Torvalds 		printf("const char %s[] %s=\n",
181da177e4SLinus Torvalds 			argv[1], argc > 2 ? argv[2] : "");
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds 	do {
211da177e4SLinus Torvalds 		printf("\t\"");
22*8370edeaSVivek Goyal 		while ((ch = getchar()) != EOF) {
231da177e4SLinus Torvalds 			total++;
24*8370edeaSVivek Goyal 			printf("\\x%02x", ch);
251da177e4SLinus Torvalds 			if (total % 16 == 0)
261da177e4SLinus Torvalds 				break;
271da177e4SLinus Torvalds 		}
281da177e4SLinus Torvalds 		printf("\"\n");
291da177e4SLinus Torvalds 	} while (ch != EOF);
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	if (argc > 1)
3221532b9eSTautschnig, Michael 		printf("\t;\n\n#include <linux/types.h>\n\nconst size_t %s_size = %d;\n",
3321532b9eSTautschnig, Michael 		       argv[1], total);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 	return 0;
361da177e4SLinus Torvalds }
37