dd

C89 implementation of dd
git clone git://ssnf.xyz/dd
Log | Files | Refs

commit 353b5c2d6d9aef05e311074b2942f45f0d598978
Author: ssnf <ssnf@ssnf.xyz>
Date:   Sun, 11 Jul 2021 11:37:17 +0000

inital commit

Diffstat:
Add.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+), 0 deletions(-)

diff --git a/dd.c b/dd.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +typedef unsigned char uchar; +typedef unsigned int uint; + +FILE* +efopen(char* path, char* mode) +{ + FILE* f; + + f = fopen(path, mode); + if (!f) { + perror(path); + exit(1); + } + return f; +} + +int +main(int argc, char* argv[]) +{ + FILE* in, *out; + char* buf; + uint bs, count, i, n; + + in = out = NULL; + buf = NULL; + bs = 512; + count = 0; + for (i = 1; i < argc; ++i) { + if (!strncmp(argv[i], "if=", 3)) + in = efopen(argv[i] + 3, "r"); + else if (!strncmp(argv[i], "of=", 3)) + out = efopen(argv[i] + 3, "w"); + else if (!strncmp(argv[i], "count=", 6)) + sscanf(argv[i] + 6, "%d", &count); + else if (!strncmp(argv[i], "bs=", 3)) { + sscanf(argv[i] + 3, "%d", &bs); + switch (*(argv[i] + strlen(argv[i]) - 1)) { + case 'k': + bs <<= 10; + break; + case 'M': + bs <<= 20; + break; + case 'G': + bs <<= 30; + } + } + } + buf = malloc(bs); + if (!in) + in = stdin; + if (!out) + out = stdout; + if (count) + goto count; + while (n = fread(buf, 1, bs, in)) + fwrite(buf, 1, n, out); + goto exit; +count: + for (i = 0; i < count; ++i) { + n = fread(buf, 1, bs, in); + fwrite(buf, 1, n, out); + } +exit: + fclose(in); + fclose(out); +}