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!floyd!harpo!seismo!hao!hplabs!Hpda From: Hpda@hplabs.UUCP (HP Data Systems) Newsgroups: net.bugs.4bsd Subject: Re: dd(1) trashes file if input and output same Message-ID: <2804@hplabs.UUCP> Date: Tue, 27-Mar-84 13:10:30 EST Article-I.D.: hplabs.2804 Posted: Tue Mar 27 13:10:30 1984 Date-Received: Fri, 30-Mar-84 01:32:25 EST References: <2766@hplabs.UUCP>, <2310@watcgl.UUCP> Organization: Hewlett Packard Labs, Palo Alto CA Lines: 26 [Sorry if anyone sees this post twice...] The case of "dd if=foo >foo" (which is equivalent to "cat foo >foo") is STILL a lose. CSH opens the output file (the result of the redirection) before the command is invoked - whether it be dd(1) or cat(1) or whatever(9). The result is a truncated file. However you do get a reprieve if noclobber is set. cat(1) chooses to tell you about your mistake, but you can still kiss your file goodbye. % cat foo beef % cat foo >foo cat: input foo is output % cat foo % (Where's the beef ?) About copying from "special" devices, point taken. I was lazy. The test should be made for S_IFCHR or S_IFBLK as in cat(1). Hack in the added tests with change bars (is this sufficient ?): if ( stat(ofile,&sbuf_out) == 0 && | sbuf_in.st_mode != S_IFCHR && | sbuf_in.st_mode != S_IFBLK && sbuf_in.st_dev == sbuf_out.st_dev && sbuf_in.st_ino == sbuf_out.st_ino ) {