This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
ftruncate does not work with rw file handles
- From: Marc Pawlowsky <marcpawl at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Tue, 12 Dec 2017 15:08:00 -0500
- Subject: ftruncate does not work with rw file handles
- Authentication-results: sourceware.org; auth=none
Is it intentional that ftruncate does not work with read/write file
handles, or am I doing somthing wrong?
gcc t.cpp && ./a.out
mode=rw fd=3 err=-1 errno=22 Invalid argument
mode=w fd=4 err=0 errno=0 Success
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
static void test(const char* mode)
{
FILE* f = fopen("/tmp/tt", mode);
if (!f) {
printf("Unable to open file\n");
return;
}
int fd = fileno(f);
errno=0;
int err = ftruncate(fd,0);
printf("mode=%s fd=%d err=%d errno=%d %s\n",
mode, fd, err,
int(errno), strerror(errno));
}
int main(int,char**)
{
test("rw");
test("w");
return 0;
}