Bug in open() function (?)

From: J.C. Wren (jcwren@jcwren.com)
Date: Fri Jul 11 2003 - 22:09:08 EST


        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/



This archive was generated by hypermail 2b29 : Tue Jul 15 2003 - 22:00:43 EST