From 7ad2a5c360d93ffed74ed793fb771a38d8086870 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Wed, 2 May 2012 12:30:47 +0200 Subject: [PATCH] bin2fex: open input and output files --- bin2fex.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/bin2fex.c b/bin2fex.c index f86d1bf..24e62b2 100644 --- a/bin2fex.c +++ b/bin2fex.c @@ -16,7 +16,76 @@ */ #include "sunxi-tools.h" -int main(int UNUSED(argc), char *UNUSED(argv[])) +#include +#include +#include +#include +#include +#include +#include +#include + +#define errf(...) fprintf(stderr, __VA_ARGS__) + +/** + */ +static int decompile(const char *bin, size_t bin_size, int out, const char *out_name) { + errf("bin size: %zu\n", bin_size); return 0; } + +/** + */ +int main(int argc, char *argv[]) +{ + struct stat sb; + int ret = -1; + int fd[] = {0, 1}; + const char *filename[] = { "stdin", "stdout" }; + void *p; + + /* open */ + if (argc>1) { + filename[0] = argv[1]; + + if ((fd[0] = open(filename[0], O_RDONLY)) < 0) { + errf("%s: %s\n", filename[0], strerror(errno)); + goto usage; + } + if (argc > 2) { + filename[1] = argv[2]; + + if ((fd[1] = open(filename[1], O_WRONLY|O_CREAT, 0666)) < 0) { + errf("%s: %s\n", filename[1], strerror(errno)); + goto usage; + } + } + } + + /* mmap input */ + if (fstat(fd[0], &sb) == -1) + errf("fstat: %s: %s\n", filename[0], strerror(errno)); + else if (!S_ISREG(sb.st_mode)) + errf("%s: %s\n", filename[0], strerror(errno)); + else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd[0], 0)) == MAP_FAILED) + errf("mmap: %s: %s\n", filename[0], strerror(errno)); + else { + /* decompile mmap */ + close(fd[0]); + + ret = decompile(p, sb.st_size, fd[1], filename[1]); + if (munmap(p, sb.st_size) == -1) + errf("munmap: %s: %s\n", filename[0], strerror(errno)); + + goto done; + } + +usage: + errf("Usage: %s [ []]\n", argv[0]); + + if (fd[0] > 2) close(fd[0]); +done: + if (fd[1] > 2) close(fd[1]); + return ret; +}