Skip to content

Commit e5a1bb1

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
nshlib/dd: Add support for reading from/writing to standard input/output
Test 1. Input from stdin and output to stdout Keyboard input: 12345AAAAABBBBB nsh> dd bs=5 1234512345AAAAAAAAAABBBBBBBBBB 2. Input from file and output to stdout nsh> dd if=/etc/init.d/rc.sysinit mkrd -m 2 -s 512 1024 mkfatfs /dev/ram2 mount -t vfat /dev/ram2 "/tmp" 3. Input from stdin and output to file Keyboard input: QWERT dd of=/data/dd_stdout bs=5 Then, cat the output file in host (based on HostFS): $ cat ./dd_stdout QWERT Signed-off-by: wangjianyu3 <[email protected]>
1 parent a799c3e commit e5a1bb1

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

nshlib/nsh_command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static const struct cmdmap_s g_cmdmap[] =
186186
#endif
187187

188188
#ifndef CONFIG_NSH_DISABLE_DD
189-
CMD_MAP("dd", cmd_dd, 3, 7,
189+
CMD_MAP("dd", cmd_dd, 1, 7,
190190
"if=<infile> of=<outfile> [bs=<sectsize>] [count=<sectors>] "
191191
"[skip=<sectors>] [seek=<sectors>] [verify] [conv=<nocreat,notrunc>]"),
192192
#endif

nshlib/nsh_ddcmd.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@
5454
*/
5555

5656
#define DEFAULT_SECTSIZE 512
57-
58-
/* At present, piping of input and output are not support, i.e., both of=
59-
* and if= arguments are required.
60-
*/
61-
62-
#undef CAN_PIPE_FROM_STD
63-
6457
#define g_dd "dd"
6558

6659
/****************************************************************************
@@ -258,10 +251,15 @@ static int dd_verify(FAR const char *infile, FAR const char *outfile,
258251

259252
/****************************************************************************
260253
* Name: cmd_dd
254+
*
255+
* At present, redirect of input and output are supported.
256+
* of= and if= arguments are required only when verify enabled.
257+
*
261258
****************************************************************************/
262259

263260
int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
264261
{
262+
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
265263
struct dd_s dd;
266264
FAR char *infile = NULL;
267265
FAR char *outfile = NULL;
@@ -287,17 +285,13 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
287285
* from stdin.
288286
*/
289287

290-
#ifdef CAN_PIPE_FROM_STD
291-
dd->infd = 0; /* stdin */
292-
#endif
288+
dd.infd = INFD(pstate); /* stdin */
293289

294290
/* If no OF= option is provided on the command line, then write
295291
* to stdout.
296292
*/
297293

298-
#ifdef CAN_PIPE_FROM_STD
299-
dd->outfd = 1; /* stdout */
300-
#endif
294+
dd.outfd = OUTFD(pstate); /* stdout */
301295

302296
/* Parse command line parameters */
303297

@@ -354,13 +348,13 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
354348
}
355349
}
356350

357-
#ifndef CAN_PIPE_FROM_STD
358-
if (infile == NULL || outfile == NULL)
351+
/* If verify enabled, infile and outfile are mandatory */
352+
353+
if ((dd.oflags & O_RDONLY) && (infile == NULL || outfile == NULL))
359354
{
360355
nsh_error(vtbl, g_fmtargrequired, g_dd);
361356
goto errout_with_paths;
362357
}
363-
#endif
364358

365359
/* Allocate the I/O buffer */
366360

@@ -373,18 +367,24 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
373367

374368
/* Open the input file */
375369

376-
ret = dd_infopen(infile, &dd);
377-
if (ret < 0)
370+
if (infile)
378371
{
379-
goto errout_with_alloc;
372+
ret = dd_infopen(infile, &dd);
373+
if (ret < 0)
374+
{
375+
goto errout_with_alloc;
376+
}
380377
}
381378

382379
/* Open the output file */
383380

384-
ret = dd_outfopen(outfile, &dd);
385-
if (ret < 0)
381+
if (outfile)
386382
{
387-
goto errout_with_inf;
383+
ret = dd_outfopen(outfile, &dd);
384+
if (ret < 0)
385+
{
386+
goto errout_with_inf;
387+
}
388388
}
389389

390390
/* Then perform the data transfer */
@@ -467,10 +467,16 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
467467
}
468468

469469
errout_with_outf:
470-
close(dd.outfd);
470+
if (outfile)
471+
{
472+
close(dd.outfd);
473+
}
471474

472475
errout_with_inf:
473-
close(dd.infd);
476+
if (infile)
477+
{
478+
close(dd.infd);
479+
}
474480

475481
errout_with_alloc:
476482
free(dd.buffer);

0 commit comments

Comments
 (0)