Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84 SMI; site sun.uucp Path: utzoo!watmath!clyde!burl!ulysses!gamma!epsilon!zeta!sabre!bellcore!decvax!decwrl!sun!guy From: guy@sun.uucp (Guy Harris) Newsgroups: net.unix-wizards Subject: Re: truncating a file opened via open() Message-ID: <2315@sun.uucp> Date: Sun, 16-Jun-85 23:39:31 EDT Article-I.D.: sun.2315 Posted: Sun Jun 16 23:39:31 1985 Date-Received: Wed, 19-Jun-85 03:37:15 EDT References: <340@cmu-cs-edu1.ARPA> <786@turtlevax.UUCP> <217@luke.UUCP> Organization: Sun Microsystems, Inc. Lines: 33 > In article <786@turtlevax.UUCP> ken@turtlevax.UUCP (Ken Turkowski) writes: > >In article <340@cmu-cs-edu1.ARPA> hua@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