From 1852731df87e5895e299b74aabdb1984b7e3f795 Mon Sep 17 00:00:00 2001 From: Denis Ryndine Date: Fri, 5 Jul 2024 10:22:22 +1000 Subject: [PATCH] dd_main.c: Add oseek optional argument. - If oseek=N passed, will seek N*bs on output file before writing. --- system/dd/dd_main.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/system/dd/dd_main.c b/system/dd/dd_main.c index 3466fc298..37526787a 100644 --- a/system/dd/dd_main.c +++ b/system/dd/dd_main.c @@ -65,6 +65,7 @@ struct dd_s int outfd; /* File descriptor of the output device */ uint32_t nsectors; /* Number of sectors to transfer */ uint32_t skip; /* The number of sectors skipped on input */ + uint32_t seek; /* The number of sectors seeked on output */ bool eof; /* true: The end of the input or output file has been hit */ uint16_t sectsize; /* Size of one sector */ uint16_t nbytes; /* Number of valid bytes in the buffer */ @@ -176,7 +177,7 @@ static void print_usage(void) { fprintf(stderr, "usage:\n"); fprintf(stderr, " %s if= of= [bs=] " - "[count=] [skip=]\n", g_dd); + "[count=] [skip=] [seek=]\n", g_dd); } /**************************************************************************** @@ -226,6 +227,10 @@ int main(int argc, FAR char **argv) { dd.skip = atoi(&argv[i][5]); } + else if (strncmp(argv[i], "seek=", 5) == 0) + { + dd.seek = atoi(&argv[i][5]); + } } if (infile == NULL || outfile == NULL) @@ -262,7 +267,7 @@ int main(int argc, FAR char **argv) if (dd.skip) { ret = lseek(dd.infd, dd.skip * dd.sectsize, SEEK_SET); - if (ret < -1) + if (ret < 0) { fprintf(stderr, "%s: failed to lseek: %s\n", g_dd, strerror(errno)); @@ -271,6 +276,18 @@ int main(int argc, FAR char **argv) } } + if (dd.seek) + { + ret = lseek(dd.outfd, dd.seek * dd.sectsize, SEEK_SET); + if (ret < 0) + { + fprintf(stderr, "%s: failed to lseek on output: %s\n", + g_dd, strerror(errno)); + ret = ERROR; + goto errout_with_outf; + } + } + /* Then perform the data transfer */ clock_gettime(CLOCK_MONOTONIC, &ts0);