Fix leaking file descriptors on error.
[kugel-rb.git] / rbutil / tools / bin2c.c
blob36e245133e07731a4931037e42e7d3f63e854554
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #if !defined(_MSC_VER)
29 #include <unistd.h>
30 #else
31 #include <io.h>
32 #define snprintf _snprintf
33 #define open _open
34 #define close _close
35 #define read _read
36 #endif
37 #include <libgen.h>
39 #ifndef O_BINARY
40 #define O_BINARY 0
41 #endif
43 static off_t filesize(int fd)
45 struct stat buf;
47 fstat(fd,&buf);
48 return buf.st_size;
51 static int write_cfile(const unsigned char* buf, off_t len, const char* cname)
53 char filename[256];
54 char filebase[256];
55 char* bn;
56 FILE* fp;
57 int i;
59 snprintf(filename,256,"%s.c",cname);
60 strncpy(filebase, cname, 256);
61 bn = basename(filebase);
63 fp = fopen(filename,"w+");
64 if (fp == NULL) {
65 fprintf(stderr,"Couldn't open %s\n",filename);
66 return -1;
69 fprintf(fp,"/* Generated by bin2c */\n\n");
70 fprintf(fp,"unsigned char %s[] = {",bn);
72 for (i=0;i<len;i++) {
73 if ((i % 16) == 0) {
74 fprintf(fp,"\n ");
76 if (i == (len-1)) {
77 fprintf(fp,"0x%02x",buf[i]);
78 } else {
79 fprintf(fp,"0x%02x, ",buf[i]);
82 fprintf(fp,"\n};\n");
84 fclose(fp);
85 return 0;
88 static int write_hfile(off_t len, const char* cname)
90 char filename[256];
91 char filebase[256];
92 char* bn;
93 FILE* fp;
95 snprintf(filename,256,"%s.h",cname);
96 strncpy(filebase, cname, 256);
97 bn = basename(filebase);
98 fp = fopen(filename,"w+");
99 if (fp == NULL) {
100 fprintf(stderr,"Couldn't open %s\n",filename);
101 return -1;
104 fprintf(fp,"/* Generated by bin2c */\n\n");
105 fprintf(fp,"#define LEN_%s %d\n",bn,(int)len);
106 fprintf(fp,"extern unsigned char %s[];\n",bn);
107 fclose(fp);
108 return 0;
111 int main (int argc, char* argv[])
113 char* infile;
114 char* cname;
115 int fd;
116 unsigned char* buf;
117 int len;
118 int n;
120 if (argc != 3) {
121 fprintf(stderr,"Usage: bin2c file cname\n");
122 return 0;
125 infile=argv[1];
126 cname=argv[2];
128 fd = open(infile,O_RDONLY|O_BINARY);
129 if (fd < 0) {
130 fprintf(stderr,"Can not open %s\n",infile);
131 return 0;
134 len = filesize(fd);
136 buf = malloc(len);
137 n = read(fd,buf,len);
138 if (n < len) {
139 fprintf(stderr,"Short read, aborting\n");
140 return 0;
142 close(fd);
144 if (write_cfile(buf,len,cname) < 0) {
145 return -1;
147 if (write_hfile(len,cname) < 0) {
148 return -1;
151 return 0;