develooper Front page | perl.perl5.porters | Postings from September 2003

[perl #23813] misleading example in perldoc -f flock

Thread Previous | Thread Next
September 15, 2003 12:50
[perl #23813] misleading example in perldoc -f flock
Message ID:
# New Ticket Created by 
# Please include the string:  [perl #23813]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
generated with the help of perlbug 1.34 running under perl v5.8.0.

[Please enter your report here]

The output of "perldoc -f flock" includes the following example

       Here's a mailbox appender for BSD systems.

           use Fcntl ':flock'; # import LOCK_* constants

           sub lock {
               # and, in case someone appended
               # while we were waiting...
               seek(MBOX, 0, 2);

           sub unlock {

           open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
                   or die "Can't open mailbox: $!";

           print MBOX $msg,"\n\n";

... which appears to have been written by someone under the
misapprehension that opening a file for append is the same as
opening for write and then seeking to the end.

This is not the case.  When a file is opened in append mode, all
writes are appended to the then end of file, irrespective of the
current file position.  From fopen(3) on FreeBSD:

``a''  Open for writing.  The file is created if it does not
       exist.  The stream is positioned at the end of the file.
       Subsequent writes to the file will always end up at the
       then current end of file, irrespective of any intervening
       fseek(3) or similar.

and the corresponding extract from open(2):

    Opening a file with O_APPEND set causes each write on the
    file to be appended to the end.

I've confirmed this behavior on FreeBSD by adding a sleep to the
example and running several copies of it in parallel.  The
messages are all appended to the end with the seek() commented
out, or even replaced with a seek() to the middle or the start of
the file.

Reading this example a couple of years ago left me with a false
impression of how O_APPEND works, and I've only just now stumbled
across the truth.

[Please do not change anything below this line]
Site configuration information for perl v5.8.0:

Configured by nick at Mon Jul 22 22:30:59 BST 2002.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
    osname=freebsd, osvers=4.6-release-p1, archname=i386-freebsd
    uname='freebsd 4.6-release-p1 freebsd 4.6-release-p1 #0: mon jul 1 11:11:48 bst 2002 root@:asyncobjasyncsrcsyslaptop i386 '
    config_args='-des -Dprefix=/u/usr/local/perl-5.8.0'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='cc', ccflags ='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -I/usr/local/include',
    cppflags='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.95.3 20010315 (release) [FreeBSD]', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags ='-Wl,-E  -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib
    libs=-lgdbm -lm -lc -lcrypt -lutil
    perllibs=-lm -lc -lcrypt -lutil
    libc=, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-DPIC -fpic', lddlflags='-shared  -L/usr/local/lib'

Locally applied patches:

@INC for perl v5.8.0:

Environment for perl v5.8.0:
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About