[comp.lang.c] Truncating an existing file

nto0302@dsacg3.dsac.dla.mil (Bob Fisher) (04/24/91)

How can I truncate the end of a large file without copying the part
to be preserved to a new file and then doing a remove/rename?

This is for SVR3 but may need ported to BSD4.3.

The only truncating capability I find is when I open the file for output.
-- 
Bob Fisher
US Defense Logistics Agency Systems Automation Center
DSAC-TOL, Box 1605, Columbus, OH 43216-5002     614-238-9071 (AV 850-9071)
bfisher@dsac.dla.mil		osu-cis!dsacg1!bfisher

henry@zoo.toronto.edu (Henry Spencer) (04/25/91)

In article <3172@dsacg3.dsac.dla.mil> nto0302@dsacg3.dsac.dla.mil (Bob Fisher) writes:
>How can I truncate the end of a large file without copying the part
>to be preserved to a new file and then doing a remove/rename?

ANSI C provides no way to do this.

Nor, more to the point of your query although less appropriate to this
newsgroup, does System V.
-- 
And the bean-counter replied,           | Henry Spencer @ U of Toronto Zoology
"beans are more important".             |  henry@zoo.toronto.edu  utzoo!henry

jeenglis@alcor.usc.edu (Joe English) (04/25/91)

nto0302@dsacg3.dsac.dla.mil (Bob Fisher) writes:
>How can I truncate the end of a large file without copying the part
>to be preserved to a new file and then doing a remove/rename?
>
>This is for SVR3 but may need ported to BSD4.3.

Check for a system call called 'truncate' or 'ftruncate'.
SunOS has these, but the man pages are (as usual) unclear
as to whether they're from BSD, System V, both, or neither.
I seem to remember seeing this under both BSD4.3 and SVR3.

This brings up another question:  I was looking under the
man page for 'fcntl' to see if it had a truncate option,
and found the following under the description of F_SETLK:


     A shared or exclusive lock is either advisory  or  mandatory
     depending on the mode bits of the file containing the locked
     segment.  The lock is mandatory if the set-GID bit (S_ISGID)
     is  set  and  the  group execute bit (S_IXGRP) is clear (see
     stat(2V) for information about mode bits).   Otherwise,  the
     lock is advisory.


This sounds really weird.  Why should locking behaviour depend 
on the setgid bit?   Is this just a SunOS quirk?

Followups to comp.unix.questions....


--Joe English

  jeenglis@alcor.usc.edu