trunc() call for Sys V?

Donn Seeley donn at utah-gr.UUCP
Mon May 13 16:05:26 AEST 1985


	From: mts at utai.UUCP (Martin Stanley)

	Does anyone know of a way to simulate the 4.2BSD trunc() call
	under System V Release 1?  Thanks in advance.

Ah, the truncate() system call.  I know a fun story about it -- perhaps
it's even true...  I won't guarantee that it will solve your problem,
but it will at least give you something to think about.

Fortran 77 has an ENDFILE statement which permits you to write an EOF
on a file, possibly after seeking to an arbitrary position.  4.1 BSD,
like System Vr1 (I presume), had no way to shorten a file other than by
truncating it to zero length, which wasn't quite good enough.  I found
out how the 4.1 BSD f77 compiler handled the problem when one of our
users came to me complaining that their program ran out of file
descriptors when trying to execute an ENDFILE statement.  The following
algorithm was used:

	Open a temporary file (bomb if no more descriptors)
	Use fread() and fwrite() to copy data from the file to be
		truncated, up to the new length
	Fork (try as many as ten times); the parent waits
	The child attempts to exec '/bin/cp tmpfile oldfile', bombing
		with the immortal error 'no cp for trunc' if it fails
	Clean up after the temporary file

ENDFILE operations tended to be rather expensive...  I'm not sure who
came up with this code (Peter Weinberger?) but it certainly was, um,
interesting...

Rumor has it that truncate() was added primarily to placate f77 users,

Donn Seeley    University of Utah CS Dept    donn at utah-cs.arpa
40 46' 6"N 111 50' 34"W    (801) 581-5668    decvax!utah-cs!donn



More information about the Comp.unix.wizards mailing list