This is a small little program used to execute a bad practice a clean way :-)

It will read a file on stdin and write it as decimal integers on stdout,
this is useful for embedding files in c-sources.

There are a few places where this is needed, and this is a better way than
the current practice of hand-editing the sources.

The command:

date | file2c 'const char date[] = {' ',0};'

will produce:

const char date[] = {
83,97,116,32,74,97,110,32,50,56,32,49,54,58,52,55,58,51,51,32,80,83,84,
32,49,57,57,53,10
,0};

The manual page is 2 lines longer than the source :-)
This commit is contained in:
Poul-Henning Kamp 1995-01-29 00:49:57 +00:00
parent 400560267f
commit b71255826d
3 changed files with 100 additions and 0 deletions

6
usr.bin/file2c/Makefile Normal file
View File

@ -0,0 +1,6 @@
# $Id: Makefile,v 1.3 1994/09/24 02:55:45 davidg Exp $
PROG= file2c
MAN1= file2c.1
.include <bsd.prog.mk>

48
usr.bin/file2c/file2c.1 Normal file
View File

@ -0,0 +1,48 @@
.\"----------------------------------------------------------------------------
.\" "THE BEER-WARE LICENSE" (Revision 42):
.\" <phk@freebsd.org> wrote this file. As long as you retain this notice, you
.\" can do whatever you want with this file. If we meet some day, and you think
.\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
.\" ---------------------------------------------------------------------------
.\"
.\" $Id$
.\"
.Dd Jan 28, 1995
.Dt FILE2C 1
.Os
.Sh NAME
.Nm file2c
.Nd convert file to c-source.
.Sh SYNOPSIS
.Nm file2c
.Op "string"
.Op "string"
.Sh DESCRIPTION
The
.Nm file2c
utility reads a file from stdin and writes it to stdout, converting each
byte to its decimal representation on the fly.
.Pp
If the first
.Op string
is present, it is printed before the data, if the second
.Op string
is present, it is printed after the data.
.Pp
This program is used to embedd binary or other files into C source files,
for instance as a char[].
.Sh EXAMPLE
The command:
.Bd -literal -offset indent
date | file2c 'const char date[] = {' ',0};'
.Ed
.Pp
will produce:
.Bd -literal -offset indent
const char date[] = {
83,97,116,32,74,97,110,32,50,56,32,49,54,58,50,56,58,48,53,
32,80,83,84,32,49,57,57,53,10
,0};
.Ed

46
usr.bin/file2c/file2c.c Normal file
View File

@ -0,0 +1,46 @@
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* $Id$
*
*/
#include <stdio.h>
int
main(int argc, char **argv)
{
int i,j,k;
char s[10];
if (argc > 1)
printf("%s\n",argv[1]);
k = 0;
j = 0;
while((i = getchar()) != EOF) {
if(k++) {
putchar(',');
j++;
}
if (j > 70) {
putchar('\n');
j = 0;
}
printf("%d",i);
if (i > 99)
j += 3;
else if (i > 9)
j += 2;
else
j++;
}
putchar('\n');
if (argc > 2)
printf("%s\n",argv[2]);
return 0;
}