lkml.org 
[lkml]   [2003]   [Jul]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectBug in open() function (?)
Date
	I was playing around today and found that if an existing file is opened with 
O_TRUNC | O_RDONLY, the existing file is truncated. This is contrary to the
documentation for "man 2 open". Which behavior is correct, the man page, or
what actually happens? Or wold this be considered a glibc/libc problem?
This is on a stock 2.5.74 kernel.

'man 2 open', on O_TRUNC: If the file already exists and is a regular file
and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be
truncated to length 0.

--John

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>

int main (int argc, char **argv)
{
int fd;

if ((fd = open ("test", O_TRUNC | O_RDONLY)) == -1)
{
printf ("%d:%s\n", errno, strerror (errno));
exit (1);
}

close (fd);

exit (0);
}

[bash] cc test.c
[bash] ls -l >test
[bash] ls -l test
-rw-r--r-- 1 jcw users 195 Jul 11 23:06 test
[bash] ./a.out
[bash] ls -l test
-rw-r--r-- 1 jcw users 0 Jul 11 23:06 test

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:46    [W:0.592 / U:0.576 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site