54
54
*/
55
55
56
56
#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
-
64
57
#define g_dd "dd"
65
58
66
59
/****************************************************************************
@@ -258,10 +251,15 @@ static int dd_verify(FAR const char *infile, FAR const char *outfile,
258
251
259
252
/****************************************************************************
260
253
* 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
+ *
261
258
****************************************************************************/
262
259
263
260
int cmd_dd (FAR struct nsh_vtbl_s * vtbl , int argc , FAR char * * argv )
264
261
{
262
+ FAR struct console_stdio_s * pstate = (FAR struct console_stdio_s * )vtbl ;
265
263
struct dd_s dd ;
266
264
FAR char * infile = NULL ;
267
265
FAR char * outfile = NULL ;
@@ -287,17 +285,13 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
287
285
* from stdin.
288
286
*/
289
287
290
- #ifdef CAN_PIPE_FROM_STD
291
- dd -> infd = 0 ; /* stdin */
292
- #endif
288
+ dd .infd = INFD (pstate ); /* stdin */
293
289
294
290
/* If no OF= option is provided on the command line, then write
295
291
* to stdout.
296
292
*/
297
293
298
- #ifdef CAN_PIPE_FROM_STD
299
- dd -> outfd = 1 ; /* stdout */
300
- #endif
294
+ dd .outfd = OUTFD (pstate ); /* stdout */
301
295
302
296
/* Parse command line parameters */
303
297
@@ -354,13 +348,13 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
354
348
}
355
349
}
356
350
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 ))
359
354
{
360
355
nsh_error (vtbl , g_fmtargrequired , g_dd );
361
356
goto errout_with_paths ;
362
357
}
363
- #endif
364
358
365
359
/* Allocate the I/O buffer */
366
360
@@ -373,18 +367,24 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
373
367
374
368
/* Open the input file */
375
369
376
- ret = dd_infopen (infile , & dd );
377
- if (ret < 0 )
370
+ if (infile )
378
371
{
379
- goto errout_with_alloc ;
372
+ ret = dd_infopen (infile , & dd );
373
+ if (ret < 0 )
374
+ {
375
+ goto errout_with_alloc ;
376
+ }
380
377
}
381
378
382
379
/* Open the output file */
383
380
384
- ret = dd_outfopen (outfile , & dd );
385
- if (ret < 0 )
381
+ if (outfile )
386
382
{
387
- goto errout_with_inf ;
383
+ ret = dd_outfopen (outfile , & dd );
384
+ if (ret < 0 )
385
+ {
386
+ goto errout_with_inf ;
387
+ }
388
388
}
389
389
390
390
/* Then perform the data transfer */
@@ -467,10 +467,16 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
467
467
}
468
468
469
469
errout_with_outf :
470
- close (dd .outfd );
470
+ if (outfile )
471
+ {
472
+ close (dd .outfd );
473
+ }
471
474
472
475
errout_with_inf :
473
- close (dd .infd );
476
+ if (infile )
477
+ {
478
+ close (dd .infd );
479
+ }
474
480
475
481
errout_with_alloc :
476
482
free (dd .buffer );
0 commit comments