mts@utai.UUCP (Martin Stanley) (05/08/85)
Does anyone know of a way to simulate the 4.2BSD trunc() call under System V Release 1? Thanks in advance. -- Martin Stanley Department of Computer Science University of Toronto Toronto, ON M5S 1A4 USENET: {ihnp4 decwrl utzoo uw-beaver}!utcsri!utai![ms!]mts CSNET: mts@toronto ARPANET: mts.toronto@csnet-relay Please use one of the above three addresses, not necessarily the address in the mail header.
donn@utah-gr.UUCP (Donn Seeley) (05/13/85)
From: mts@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@utah-cs.arpa 40 46' 6"N 111 50' 34"W (801) 581-5668 decvax!utah-cs!donn