Path: utzoo!attcan!uunet!mcvax!np1!nikhefh!gert From: gert@nikhefh.hep.nl (Gert Poletiek) Newsgroups: comp.sys.atari.st Subject: Re: Copying a file over itself Message-ID: <489@nikhefh.hep.nl> Date: 19 Jul 88 22:51:22 GMT References: <4507@pasteur.Berkeley.Edu> <565@philmds.UUCP> Reply-To: gert@nikhefh.hep.nl (Gert Poletiek) Organization: Nikhef-H, Amsterdam (the Netherlands). Lines: 95 In article <565@philmds.UUCP> leo@philmds.UUCP (Leo de Wit) writes: >In article <4507@pasteur.Berkeley.Edu> landay@cory.Berkeley.EDU (James A. Landay) writes: >>Last night (at 3AM) I accidently copied a file over itself. >>This seems to trash the file rather than touching the date >>or doing nothing. Will this be fixed in the new ROM version? (Atari?) > >Tried it with a 14K file (i.e. copying from the Desktop), but the >contents remained the same. The date was changed. >Then I tried it with a simple copy program, like this: > >#include >#include >#include > >main() >{ > int fdi, fdo, nread; > char buf[BUFSIZ]; > > fdi = Fopen("test.dat",O_RDONLY); > fdo = Fopen("test.dat",O_WRONLY|O_CREAT|O_TRUNC); > > while ((nread = Fread(fdi,BUFSIZ,buf)) > 0) { > Fwrite(fdo,nread,buf); > } > Fclose(fdi); > Fclose(fdo); >} > >Odd enough, even though O_TRUNC was specified the contents remained the >same (in Unix you would get an empty file). Also the date was not changed. >Interchanging the Fopen calls didn't make any difference. >So the next question is perhaps: what ROM version are you using now? > > Leo. > >B.T.W. What in the world made the GEMDOS writers decide to swap the >second and third parameter in the Fread and Fwrite calls, compared with >Unix read() and write() ? Since they 'leant' quite a few things from Unix, >wouldn't it have been easier to use the same parameter ordering? The GemDos Fopen call has no flag O_TRUNC. Sometimes GemDos is way off the Unix standard. (certainly in speed). In order to mimic the O_TRUNC you should have your own open call, something like this: open ( name, mode ) char *name; int mode; { int fd; struct statbuf statbuf; if ( mode & O_TRUNC ) { if ( stat ( name, &statbuf ) == 0 ) { if ( statbuf.attr & (F_VOLUME|F_DIR|F_READONLY) ) { errno = EACCDN; return ( OPENERR ); } if ( unlink ( name ) < 0 ) return ( OPENERR ); mode |= O_CREAT; } } fd = Fopen ( name, mode & 3 ); if ( fd < -3 && ( mode & O_CREAT ) ) /* try to create the file */ fd = Fcreate ( name, 0 ); if ( fd >= 0 && ( mode & O_APPEND ) ) /* seek to EOF */ Fseek ( 0L, fd, 2 ); if ( fd < -3 ) { errno = fd; return ( OPENERR ); } return ( fd ); } Also notice the nice effect of writing to a 1024 byte long file with read and write access on byte 24 for example. The file will remain 1024 bytes long! Gert Poletiek NIKHEF-H, Dutch National Institute for Nuclear and High Energy Physics Kruislaan 409, P.O.Box 41882, 1009 DB Amsterdam, The Netherlands UUCP: {decvax,cernvax,unido,seismo}!mcvax!nikhefh!gert bitnet: nikhefh!gert@mcvax.bitnet, U00025@hasara5.bitnet