From f7ad6c896a0c95c87b840906ff40a2a24aebc147 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 4 Feb 2024 12:58:36 +0300 Subject: [PATCH] added count flag in dd, also fixed help message and suffixes parser --- include/libmu/human.h | 2 ++ src/coreutils/dd/dd.c | 27 +++++++++++++++++---------- src/miscutils/spark/spark.c | 10 ++++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/include/libmu/human.h b/include/libmu/human.h index 2140b66..207b846 100644 --- a/include/libmu/human.h +++ b/include/libmu/human.h @@ -1,6 +1,8 @@ #ifndef _HUMAN_H #define _HUMAN_H +#include + char *mu_humansize(off_t n) { static char buf[16]; char *postfixes = "BKMGTPE"; diff --git a/src/coreutils/dd/dd.c b/src/coreutils/dd/dd.c index 95244a5..6211560 100644 --- a/src/coreutils/dd/dd.c +++ b/src/coreutils/dd/dd.c @@ -41,22 +41,22 @@ off_t strtonum(char *str) { if (!strcmp(p, "b")) res *= 512; - else if (!strcmp(p, "MB")) + else if (!strcmp(p, "m")) res *= 1000000; else if (!strcmp(p, "M")) res *= 1048576; - else if (!strcmp(p, "KB")) + else if (!strcmp(p, "K")) res *= 1024; - else if (!strcmp(p, "kB")) + else if (!strcmp(p, "k")) res *= 1000; - else if (!strcmp(p, "G")) + else if (!strcmp(p, "g")) res *= 1000000000; - else if (!strcmp(p, "GB")) + else if (!strcmp(p, "G")) res *= 1073741824; } @@ -87,6 +87,7 @@ int main(int argc, char **argv) { char *ifp = "-"; char *ofp = "-"; + off_t count = -1; off_t skip = 0; off_t seek = 0; @@ -104,7 +105,7 @@ int main(int argc, char **argv) { char *val = strchr(arg, '='); if (val == NULL) { - printf("dd\n\t[if=InputFile]\n\t[of=OutputFile]\n\nN and BYTES may be followed by the following multiplicative\nsuffixes: w=2, b=512, kB=1000, K=1024, MB=1000*1000,\nM=1024*1024, GB=1000*1000*1000, G=1024*1024*1024\n"); + printf("dd\n\t[if=InputFile] [of=OutputFile]\n\t[bs=obs and obs]\n\t[ibs=Input buffer size]\n\t[obs=Output buffer size]\n\t[seek=Skip N obs-sized output blocks]\n\t[skip=Skip N ibs-sized output blocks]\n\t[count=Copy only N input blocks]\n\nN and BYTES may be followed by the following multiplicative\nsuffixes: w=2, b=512, k=1000, K=1024, m=1000*1000,\nM=1024*1024, g=1000*1000*1000, G=1024*1024*1024\n"); return 1; } @@ -181,19 +182,25 @@ int main(int argc, char **argv) { /* dd */ while (1) { - off_t n = read(ifd, ibuf, ibs); - if (n == 0) + if (count == infull + inpart) break; - if (ibs == n) + off_t n = read(ifd, ibuf, ibs); + if (n <= 0) + break; + + else if (ibs == n) infull++; else inpart++; - if (ibs == obs) + if (ibs == obs) { if (copy(ofd, ibuf, n, ibs)) goto CLOSE; + } + + else {} } /* End */ diff --git a/src/miscutils/spark/spark.c b/src/miscutils/spark/spark.c index 3b96ec6..81cb6f0 100644 --- a/src/miscutils/spark/spark.c +++ b/src/miscutils/spark/spark.c @@ -43,11 +43,13 @@ int main(int argc, char **argv) { for (int i = 0; i < argc; i++) { double val = 0; - if (!strcmp(argv[i], "-r")) - r_flag = 1; + if (argv[i][0] == '-') { + if (strstr(argv[i], "r")) + r_flag = 1; - else if (!strcmp(argv[i], "-n")) - ret = '\r'; + if (strstr(argv[i], "n")) + ret = '\r'; + } else if (parse_double(argv[i], &val)) { if (val > max)