Skip to content

Commit 1852731

Browse files
Denis Ryndinexiaoxiang781216
Denis Ryndine
authored andcommitted
dd_main.c: Add oseek optional argument.
- If oseek=N passed, will seek N*bs on output file before writing.
1 parent ea64cef commit 1852731

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

system/dd/dd_main.c

+19-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct dd_s
6565
int outfd; /* File descriptor of the output device */
6666
uint32_t nsectors; /* Number of sectors to transfer */
6767
uint32_t skip; /* The number of sectors skipped on input */
68+
uint32_t seek; /* The number of sectors seeked on output */
6869
bool eof; /* true: The end of the input or output file has been hit */
6970
uint16_t sectsize; /* Size of one sector */
7071
uint16_t nbytes; /* Number of valid bytes in the buffer */
@@ -176,7 +177,7 @@ static void print_usage(void)
176177
{
177178
fprintf(stderr, "usage:\n");
178179
fprintf(stderr, " %s if=<infile> of=<outfile> [bs=<sectsize>] "
179-
"[count=<sectors>] [skip=<sectors>]\n", g_dd);
180+
"[count=<sectors>] [skip=<sectors>] [seek=<sectors>]\n", g_dd);
180181
}
181182

182183
/****************************************************************************
@@ -226,6 +227,10 @@ int main(int argc, FAR char **argv)
226227
{
227228
dd.skip = atoi(&argv[i][5]);
228229
}
230+
else if (strncmp(argv[i], "seek=", 5) == 0)
231+
{
232+
dd.seek = atoi(&argv[i][5]);
233+
}
229234
}
230235

231236
if (infile == NULL || outfile == NULL)
@@ -262,7 +267,7 @@ int main(int argc, FAR char **argv)
262267
if (dd.skip)
263268
{
264269
ret = lseek(dd.infd, dd.skip * dd.sectsize, SEEK_SET);
265-
if (ret < -1)
270+
if (ret < 0)
266271
{
267272
fprintf(stderr, "%s: failed to lseek: %s\n",
268273
g_dd, strerror(errno));
@@ -271,6 +276,18 @@ int main(int argc, FAR char **argv)
271276
}
272277
}
273278

279+
if (dd.seek)
280+
{
281+
ret = lseek(dd.outfd, dd.seek * dd.sectsize, SEEK_SET);
282+
if (ret < 0)
283+
{
284+
fprintf(stderr, "%s: failed to lseek on output: %s\n",
285+
g_dd, strerror(errno));
286+
ret = ERROR;
287+
goto errout_with_outf;
288+
}
289+
}
290+
274291
/* Then perform the data transfer */
275292

276293
clock_gettime(CLOCK_MONOTONIC, &ts0);

0 commit comments

Comments
 (0)