truncating a file opened via open()

Guy Harris guy at sun.uucp
Mon Jun 17 13:39:31 AEST 1985


> In article <786 at turtlevax.UUCP> ken at turtlevax.UUCP (Ken Turkowski) writes:
> >In article <340 at cmu-cs-edu1.ARPA> hua at cmu-cs-edu1.ARPA (Ernest Hua) writes:
> >>Does anyone have any idea how to truncate a file at the current point in
> >>writing if it is opened by open()?
> >
> >size = lseek(fd, 0L, 1);	/* tell(fd) */
> >ftruncate(fd, size);		/* int fd, size; */
> >
> >It's yet another undocumented feature on 4.2.

Undocumented?  Hardly.  See TRUNCATE(2) in the UNIX Programmer's Manual.

> OK, how about System III?  That's my problem.  Someone out there posted
> a program that uses ftruncate, and we don't have it.  Solutions are
> welcome!

There are precisely two solutions for non-4.2BSD systems:

1) implement (f)truncate on your system.  Any implementation on a non-4.2BSD
version of UNIX will probably move to any other non-4.2BSD version with
little difficulty.

2) Copy the entire file, up to the point of truncation, to a temporary file,
then copy it back to the original file.

The "f77" support library uses solution 2), except under 4.2BSD where it
uses solution 1); I believe "truncate" was introduced for the benefit of
"f77".

A comparision of solutions 1) and 2) may indicate why the Berkeley people
added "truncate".  Solution 2) is slow and ugly.

	Guy Harris



More information about the Comp.unix.wizards mailing list