sft@ihlpa.ATT.COM (Scott Thompson) (05/13/89)
I want to read, process and write, a possibly smaller (truncate), mail file in a single locked open. Currently I can read, process and write the file originally opened with fopen and "r+", but it will not truncate if the new file is shorter; I also tried other combinations along with open, but they truncate the file prior to reading! This is for use under SYSV for file locking a mail file. Any and all suggestions will be appreciated. Thanks -- -- Scott Thompson (IH 6W-207), AT&T Bell Labs, Naperville, Il. 60566 VOICE: (312)-979-2237 UUCP: ...!att!ihlpa!sft
kucharsk@uts.amdahl.com (William Kucharski) (05/13/89)
This was posted before in a discussion about providing a work-alike of BSD
ftruncate(2) in SYSV, but here goes again:
The routine depends on the undocumented fcntl F_FREESP, which is present in
SVR3.1 and above. The fcntl frees file space starting at the location
indicated by fl.l_start.
#include <sys/types.h>
#include <fcntl.h>
int
truncate(fd, length)
int fd;
off_t length;
{
struct flock fl;
/* truncate file to length <length> */
fl.l_whence = 0;
fl.l_len = 0;
fl.l_start = length; /* make offset "start" the new EOF */
fl.l_type = F_WRLCK; /* why not write lock it? */
if (fcntl(fd, F_FREESP, &fl) < 0)
return(-1);
return(0);
}
--
William Kucharski
ARPA: kucharsk@uts.amdahl.com
UUCP: ...!{ames,decwrl,sun,uunet}!amdahl!kucharsk
Disclaimer: The opinions expressed above are my own, and may not agree with
those of any other sentient being, not to mention those of my
employer. So there.