[net.bugs.4bsd] dd bug -- clobbers output file

matt@oddjob.UUCP (Matt Crawford) (04/18/85)

I can hardly believe that this hasn't been discovered before, but
since I've seen no mention of it I'm sending it out.

Subject: dd clobbers output file
Index:	bin/dd.c 4.2BSD

Description:
	dd truncates its output file before writing.  This is clearly
	not a `feature' because it makes the seek=n option useless.
Repeat-By:
	Create a file "infile" longer than, say, 50 bytes.
	  dd if=infile of=outfile bs=50 count=1
	  dd if=infile of=outfile bs=50 skip=1 seek=1
	outfile and infile should now be identical but in fact the
	first 50 bytes of outfile will be zeros.
Fix:
	dd opens its output file by calling creat().  This truncates.
	The later lseek() fills the first record with zeros.

RCS file: RCS/dd.c,v
retrieving revision 1.1
retrieving revision 1.2
diff  -r1.1 -r1.2
3a4
> #include <sys/file.h>
258c259
< 		obf = creat(ofile, 0666);
---
> 		obf = open(ofile, O_WRONLY|O_CREAT, 0666);


   "It's true, your honor.  This node has no disk."
_____________________________________________________
Matt		University	crawford@anl-mcs.arpa
Crawford	of Chicago	ihnp4!oddjob!matt

ggs@ulysses.UUCP (Griff Smith) (04/19/85)

> I can hardly believe that this hasn't been discovered before, but
> since I've seen no mention of it I'm sending it out.
> 
> Subject: dd clobbers output file
> Index:	bin/dd.c 4.2BSD
> 
> Description:
> 	dd truncates its output file before writing.  This is clearly
> 	not a `feature' because it makes the seek=n option useless.

	Not quite.  My most frequent use of the "seek" option is
	when trying to spot-weld slightly damaged disks.  The truncation
	feature does not apply to raw disks.

> 	dd opens its output file by calling creat().  This truncates.
> 	The later lseek() fills the first record with zeros.

	When dd was written, this was the only option.  The fancy
	"open" options are a fairly recent invention.  I suspect
	that the "seek" option was not really intended to be used
	with vanilla files.

> RCS file: RCS/dd.c,v
> retrieving revision 1.1
> retrieving revision 1.2
> diff  -r1.1 -r1.2
> 3a4
> > #include <sys/file.h>
> 258c259
> < 		obf = creat(ofile, 0666);
> ---
> > 		obf = open(ofile, O_WRONLY|O_CREAT, 0666);
> 

	Be advised that if any of you install this patch, your "dd"
	is broken for most normal applications.  It will fail to delete
	the trash after the last block written.  

	Rather than trying to patch the current dd, why not buy the
	one in the AT&T Tool Chest (cheap, nowhere near the cost of ksh).
	It is about ten times faster than the standard one when doing
	conversions such as EBCDIC to ASCII (X 10 is on a VAX, your
	mileage may vary).