Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jun 1996 17:25:42 -0500 (CDT)
From:      Alex Nash <alex@zen.nash.org>
To:        pjf@cts.com
Cc:        bde@freebsd.org, dyson@freebsd.org, hackers@freebsd.org
Subject:   Re: bugs
Message-ID:  <199606022225.RAA23224@zen.nash.org>

next in thread | raw e-mail | index | archive | help
>Sorry, I screwed up my example.  Here's the real one:
>
>-------------------------
>#include <stdio.h>
>#include <sys/mman.h>
>#include <sys/file.h>
>
>main()
>{
>  int fd = open("newfile", O_RDWR|O_CREAT|O_EXCL, 0660);
>  char *buf;
>  ftruncate(fd, 100);
>  buf = mmap(NULL, 100, PROT_WRITE, MAP_SHARED, fd, 0);
>  printf("%lx\n", buf);
>  strcpy(buf, "hi!");
>}
>-------------------------
>
>This doesn't work either.  The ftruncate() appears not to work; the file
>is still zero length after the program crashes.  So perhaps the problem
>is with ftruncate() and not mmap().
>
>This works on all of our 10 existing UNIX platforms except BSDI and
>Linux.  BSDI has the same problem with ftruncate, I think; Linux's
>ftruncate works, but its mmap() appears to be totally broken, at least
>in 1.2.13.

TRUNCATE(2)               FreeBSD Programmer's Manual              TRUNCATE(2)
[...]
 DESCRIPTION
      Truncate() causes the file named by path or referenced by fd to be trun-
      cated to at most length bytes in size.
               ^^^^^^^

Richard Stevens (APUE) has the following notes on truncate (see page
92):

   These two functions are provided by SVR4 and 4.3+BSD.  They are not
   part of POSIX.1 or XPG3.

   SVR4 truncates or extends a file.  4.3+BSD only truncates a file with
   these functions -- they can't be used to extend a file.

As far as mmap is concerned, you cannot write beyond 'len' bytes as
specified in the call to mmap.  (In reality, under FreeBSD 2.1R you
can write up to the next 4K page and get away with it.  Linux 1.2.13
is even less stringent.)

Alex



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199606022225.RAA23224>