diff options
Diffstat (limited to 'bin2sh.c')
| -rw-r--r-- | bin2sh.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/bin2sh.c b/bin2sh.c new file mode 100644 index 0000000..2ec81dd --- /dev/null +++ b/bin2sh.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <stdint.h> +#include <stdbool.h> + +#define WIDTH 10 + + +void usage(const char *progname) { + fprintf(stderr, "usage: %s <file> [-r <file>] [-w <width>]\n", progname); +} + + +int main(int argc, char *argv[]) { + FILE *fp; + uint8_t buf[1]; + int i = 0; + int opt; + char *redir = NULL; + unsigned int width = WIDTH; + bool r = false; + + + if (argc < 2) { + usage(argv[0]); + return EXIT_FAILURE; + } + + while ((opt = getopt(argc, argv, "w:r:")) != -1) { + switch (opt) { + case 'w': + width = atoi(optarg); + break; + case 'r': + redir = optarg; + break; + default: + usage(argv[0]); + return EXIT_FAILURE; + } + } + + argc -= optind; + argv += optind; + + fp = fopen(argv[0], "rb"); + if (fp == NULL) { + fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno)); + return EXIT_FAILURE; + } + + printf("echo -ne \""); + + while(fread(buf, 1, 1, fp)) { + i++; + printf("\\x%02x", buf[0]); + if (i % width == 0) { + if (redir) { + printf("\" %s %s\n", r ? ">>" : ">", redir); + r = true; + } else { + printf("\"\n"); + } + printf("echo -ne \""); + } + } + + if (i % WIDTH != 0) + printf("\"\n"); + + fclose(fp); + + return EXIT_SUCCESS; +} |
