Commit fc036082 authored by Alejandro Mery's avatar Alejandro Mery
Browse files

bin2fex: changed to use FILE* for out

parent c0580cef
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
*/ */
static int decompile_section(void *bin, size_t bin_size, static int decompile_section(void *bin, size_t bin_size,
struct script_section *section, struct script_section *section,
int out, const char* out_named) FILE *out)
{ {
struct script_section_entry *entry = PTR(bin, section->offset<<2); struct script_section_entry *entry = PTR(bin, section->offset<<2);
int i = section->length; int i = section->length;
...@@ -53,7 +53,7 @@ static int decompile_section(void *bin, size_t bin_size, ...@@ -53,7 +53,7 @@ static int decompile_section(void *bin, size_t bin_size,
} }
/** /**
*/ */
static int decompile(void *bin, size_t bin_size, int out, const char *out_name) static int decompile(void *bin, size_t bin_size, FILE *out)
{ {
int i; int i;
struct { struct {
...@@ -73,7 +73,7 @@ static int decompile(void *bin, size_t bin_size, int out, const char *out_name) ...@@ -73,7 +73,7 @@ static int decompile(void *bin, size_t bin_size, int out, const char *out_name)
pr_info("%s:\t(section:%d, length:%d, offset:%d)\n", pr_info("%s:\t(section:%d, length:%d, offset:%d)\n",
section->name, i+1, section->length, section->offset); section->name, i+1, section->length, section->offset);
if (!decompile_section(bin, bin_size, section, out, out_name)) if (!decompile_section(bin, bin_size, section, out))
return 1; /* failure */ return 1; /* failure */
} }
return 0; /* success */ return 0; /* success */
...@@ -85,22 +85,23 @@ int main(int argc, char *argv[]) ...@@ -85,22 +85,23 @@ int main(int argc, char *argv[])
{ {
struct stat sb; struct stat sb;
int ret = -1; int ret = -1;
int fd[] = {0, 1}; int in = 0;
const char *filename[] = { "stdin", "stdout" }; FILE *out = stdout;
const char *filename[] = {"stdin", "stdout"};
void *p; void *p;
/* open */ /* open */
if (argc>1) { if (argc>1) {
filename[0] = argv[1]; filename[0] = argv[1];
if ((fd[0] = open(filename[0], O_RDONLY)) < 0) { if ((in = open(filename[0], O_RDONLY)) < 0) {
errf("%s: %s\n", filename[0], strerror(errno)); errf("%s: %s\n", filename[0], strerror(errno));
goto usage; goto usage;
} }
if (argc > 2) { if (argc > 2) {
filename[1] = argv[2]; filename[1] = argv[2];
if ((fd[1] = open(filename[1], O_WRONLY|O_CREAT, 0666)) < 0) { if ((out = fopen(filename[1], "w")) == NULL) {
errf("%s: %s\n", filename[1], strerror(errno)); errf("%s: %s\n", filename[1], strerror(errno));
goto usage; goto usage;
} }
...@@ -108,17 +109,17 @@ int main(int argc, char *argv[]) ...@@ -108,17 +109,17 @@ int main(int argc, char *argv[])
} }
/* mmap input */ /* mmap input */
if (fstat(fd[0], &sb) == -1) if (fstat(in, &sb) == -1)
errf("fstat: %s: %s\n", filename[0], strerror(errno)); errf("fstat: %s: %s\n", filename[0], strerror(errno));
else if (!S_ISREG(sb.st_mode)) else if (!S_ISREG(sb.st_mode))
errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode); errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode);
else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd[0], 0)) == MAP_FAILED) else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0)) == MAP_FAILED)
errf("mmap: %s: %s\n", filename[0], strerror(errno)); errf("mmap: %s: %s\n", filename[0], strerror(errno));
else { else {
/* decompile mmap */ /* close and decompile mmap */
close(fd[0]); close(in);
ret = decompile(p, sb.st_size, fd[1], filename[1]); ret = decompile(p, sb.st_size, out);
if (munmap(p, sb.st_size) == -1) if (munmap(p, sb.st_size) == -1)
errf("munmap: %s: %s\n", filename[0], strerror(errno)); errf("munmap: %s: %s\n", filename[0], strerror(errno));
...@@ -128,8 +129,8 @@ int main(int argc, char *argv[]) ...@@ -128,8 +129,8 @@ int main(int argc, char *argv[])
usage: usage:
errf("Usage: %s [<script.bin> [<script.fex>]]\n", argv[0]); errf("Usage: %s [<script.bin> [<script.fex>]]\n", argv[0]);
if (fd[0] > 2) close(fd[0]); if (in > 2) close(in);
done: done:
if (fd[1] > 2) close(fd[1]); if (out != stdout) fclose(out);
return ret; return ret;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment