Article 8083 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:8083
Path: feenix.metronet.com!news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!swrinde!gatech!rutgers!flop.ENGR.ORST.EDU!gaia.ucs.orst.edu!ruby.oce.orst.edu!oce.orst.edu!stanley
From: bill@tardis.co.uk (William Hails)
Newsgroups: comp.lang.perl
Subject: Re: How do I use fcntl on HPUX 9.0?
Message-ID: <16195.9311171704@devone.tardis.co.uk>
Date: 17 Nov 93 17:04:27 GMT
Organization: Perl-Users Mail2News gateway
Lines: 71
NNTP-Posting-Host: ruby.oce.orst.edu
Originator: daemon@ruby.oce.orst.edu


dph@fc.hp.com (David Hannum) writes:
> I am trying to understand how to use the unix fcntl commands F_GETLK,
> F_SETLKW, and F_SETLK with the perl fcntl function.  In particular, 
> I don't understand how the perl SCALAR argument maps into the flock
> structure.  Also, when I look at the "sys/fcntl.ph" created by h2ph 
> and c2ph, I see no code where the flock structure was defined. 
> Should I see some corresponding perl code?  Could someone who has 
> utilized this function explain it to me?

> Dave Hannum

I've posted this before - and got lightly toasted for using an explicit
argument to pack() - but my c2ph doesn't work :--( . Anyway it at least
demonstrates using fcntl to do file locking.

require('lock.pl');

then call &lockfile(FILEHANDLE) and &unlockfile(FILEHANDLE)
(FILHANDLE is an open filehandle
    (in case you hadn't guessed (:-) (yes, I *like* lisp :-)))

8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----

# routines for getting locks in the absence of the flock function
#
# $Id: lock.pl,v 1.2 1993/08/23 15:25:38 bill Exp $
#
#

sub lockfile {
    package locker;
    local ($filename) = @_;
    local ($package) = caller;
    local ($handle) = (eval "$package'$filename");
    select((select($handle), $|=1)[0]);
    fcntl($handle, $F_SETLKW, $lock);
}

sub unlockfile {
    package locker;
    local ($filename) = @_;
    local ($package) = caller;
    local ($handle) = (eval "$package'$filename");
    fcntl($handle, $F_SETLK, $unlock);
}

package locker;

require('sys/fcntl.ph');

$F_SETLKW = &F_SETLKW;
$F_SETLK = &F_SETLK;

$lock = pack('s s l l s', &F_WRLCK, 0, 0, 0, 0);
$unlock = pack('s s l l s', &F_UNLCK, 0, 0, 0, 0);

1;

8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----
-- 

Cheers
        Bill

  ===========================================================================
 | Bill Hails <bill@tardis.co.uk>    | Reisner's rule of conceptual inertia: |
 | C.L.I. Connect Ltd.               |                                       |
 | 19, Quarry St., Guildford, Surrey | If you think big enough, you'll never |
 | GU1 3UY. Tel (UK) 0483 300 200    | have to do it.                        |
  ===========================================================================


