From aa60207559bb57760cf4d29944e1678387945bde Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 7 May 2025 08:53:03 -0700 Subject: initial commit --- bin2sh.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 bin2sh.c (limited to 'bin2sh.c') 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 +#include +#include +#include +#include +#include +#include + +#define WIDTH 10 + + +void usage(const char *progname) { + fprintf(stderr, "usage: %s [-r ] [-w ]\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; +} -- cgit v1.2.3