Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!van-bc!ubc-cs!uw-beaver!zephyr.ens.tek.com!tektronix!nosun!qiclab!onion!jeff From: jeff@onion.pdx.com (Jeff Beadles) Newsgroups: comp.unix.questions Subject: Re: mv'ing files from a C program Message-ID: <1990Nov17.193018.12525@onion.pdx.com> Date: 17 Nov 90 19:30:18 GMT References: <22@mixcom.UUCP> <1990Nov15.221117.9711@holos0.uucp> Distribution: na Lines: 65 wdh@holos0.uucp (Weaver Hickerson) writes: >main() >{ >link("Oldfile","Newfile"); /* link the Newfile with Oldfile */ >unlink("Oldfile"); /* unlink the oldfile */ > > /* link and unlink are described in your Programmer's ref manual */ >} >-- >-Weaver Hickerson Voice (404) 496-1358 : ..!edu!gatech!holos0!wdh Bleep! Wrong answer Weaver. What if the files are on two seperate filesystems? Guess what? You just deleted the original without making a copy! I think that the users wouldn't appreciate you for this. In a pseudo-sorta way, here's how a move command should work if you are going to go thru the hassles of actually writing one. (system("mv ...") is easier, cleaner, and clearer IMHO) This is just for a simple mv of two files. It doesn't take into consideration things like "mv /etc/passwd /tmp" where the dest is a directory. Figure that one out by yourself... :-) [Note: // does not imply C++, it's just easier to type :-] mv(src,dest) { // Easiest case, where the link works if link(src,dest) == success unlink(src) exit // If the error was not that it was across a filesystem, then it's fatal if error != EXDEV error- can't do it. exit with warning // At this point, we know that it's a semi-legal operation, // just that it's across a filesystem. open(src,"r") // If file "dest" exists, and can be deleted (ie: not a directory) // then unlink the dest file after opening the src file. open(dest,"w") // read and copy src to dest (Checking for errors and stuff) // close src and dest (Checking for errors and stuff) // change permissions on dest to match src. // if all was ok, unlink src. (If not, you MIGHT need to unlink dest) exit and bug-out. } Note, that this is a REAL rough draft of how mv would work if I wrote one from scratch. It's NOT complete, nor is it intended to be. -Jeff -- Jeff Beadles jeff@onion.pdx.com