Re: lockf missing ?

"Nils O." Selåsdal <[email protected]>
Newsgroups gmane.linux.lsb.discuss
Organization UtelSystems
Message-ID <1050046590.1229.11.camel@nos-rh>
On Thu, 10 Apr 2003, Nils O. [ISO-8859-1] Sel=E5sdal wrote:

>> Shouldn't the lockf be part of LSB ?

>Yes, and it is.
Ok. It's not always easy for me to determin what should/should not be
part of LSB, what's missing from lsbdev-base and so on.

>> The various flags (F_LOCK,F_TLOCK,F_ULOCK,F_TEST) also doesn't exist.

>Another bug that all of the review and testing missed. 8-(.

>The SUS says these flags belong in unistd.h, so attached is a unistd.h
>that contains them. Please let me know if this solves this problem.
Tried the new unistd.h with lsbdev-base 1.3
[noselasd@nos-rh utils]$ /opt/lsbdev-cc/bin/lsbcc     -o uniqnum
uniqnum.c
uniqnum.c: In function `my_lock':
uniqnum.c:10: too many arguments to function `lockf'
uniqnum.c: In function `my_unlock':
uniqnum.c:22: too many arguments to function `lockf'

lockf is defined as extern void lockf(void);
changing that to
int lockf(int fd, int cmd, off_t len);
makes it work.

Attached is uniqnum.c , the test program I'm trying, if anyone needs
a testcase.

-- 
Vennlig hilsen/Best Regards
Nils Olav Selåsdal
System Engineer
UtelSystems a/s

w w w . u t e l s y s t e m s . c o m
uniqnum.c (text/x-c, 1.3 KB)
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>


void my_lock( int fd )
{
   lseek( fd, 0L, 0 );   
   if ( lockf( fd, F_LOCK, 0L ) == -1 )
   {
      fprintf( stderr, "Cannot lock file\n" );
      exit(1);
   }
}


void my_unlock( int fd )
{
   lseek( fd, 0L, 0 );
   
   if ( lockf( fd, F_ULOCK, 0L ) == -1 )
   {
      fprintf( stderr, "Cannot unlock file\n" );
      exit(1);
   }
}


int main( int argc, char* argv[] )
{
   int fd;
   int bytes;
   int counter = 0;

   if (argc != 2)
   {
      printf("Usage: %s filename\n", argv[0]);
      exit(0);
   }

   fd = open(argv[1], O_RDWR | O_CREAT, 00700 );
   if (fd < 0)
   {
      fprintf( stderr, "Cannot open file\n" );
      exit(1);
   }
   
   my_lock( fd );
   
   lseek( fd, 0L, 0 );
   bytes = read( fd, &counter, sizeof(int) );
   if ( bytes  < 0 )
   { 
      fprintf( stderr, "Cannot read file\n" );
      exit(1);
   }
   else if (bytes == 0)
   {
      counter = 0; /* file is empty; start at zero */
   }

   counter++;
   if (counter >= 10000)
   {
      counter = 1;
   }
      
   lseek( fd, 0L, 0 );
   if ( write( fd, &counter, sizeof(int) ) <= 0 )
   { 
      fprintf( stderr, "Cannot write file\n" );
      exit(1);
   }
   my_unlock( fd );

   printf( "%d\n", counter );
   return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.