mv'ing files from a C program

Jeff Beadles jeff at onion.pdx.com
Sun Nov 18 06:30:18 AEST 1990


wdh at 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 at onion.pdx.com



More information about the Comp.unix.questions mailing list