Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 9/27/83; site hplabs.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!ihnp4!zehntel!hplabs!Hpda From: Hpda@hplabs.UUCP (HP Data Systems) Newsgroups: net.bugs.4bsd Subject: dd(1) trashes file if input and output same Message-ID: <2766@hplabs.UUCP> Date: Fri, 23-Mar-84 20:51:11 EST Article-I.D.: hplabs.2766 Posted: Fri Mar 23 20:51:11 1984 Date-Received: Sun, 25-Mar-84 12:30:33 EST Organization: Hewlett Packard Labs, Palo Alto CA Lines: 53 Subject: dd(1) destroys file if input and output are the same. Index: /usr/src/bin/dd.c Submitter: Eric Wertz ( ..!ucbvax!hpda!eric, wertz@su-glacier.ARPA, eric@cmu-ee-faraday.ARPA ) Description: If file arguments given via "if=" and "of=" are the same, the file gets trashed. Programs like cp(1) usually catch this. Repeat-By: % cat foo This is a test file. % dd if=foo of=foo 0+0 records in 0+0 records out (oh dear, this doesn't look good...) % cat foo % (sure enough...) Fix: Add and to the list of include files. and insert code with change bars... if(ibf < 0) { perror(ifile); exit(0); } | if (ifile && ofile) { | struct stat sbuf_in, sbuf_out; | | /* Can I stat ifile ? */ | if ( stat(ifile,&sbuf_in) == -1 ) { | perror(ifile); | exit(0); | } | /* If ofile exists, test to see if it's the same as ifile */ | if ( stat(ofile,&sbuf_out) == 0 && | sbuf_in.st_dev == sbuf_out.st_dev && | sbuf_in.st_ino == sbuf_out.st_ino ) { | fprintf(stderr,"dd: can't copy file onto itself\n"); | exit(0); | } | } if (ofile) obf = creat(ofile, 0666); Comments: This bug occurs in System III, System V, System V.2, and 4.[12] BSD. Of course, there's no way to prevent... % dd if=foo >foo ...from trashing a file.