Back to index

courier  0.68.2
locklockf.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #if    HAVE_CONFIG_H
00008 #include      "config.h"
00009 #endif
00010 #include      <sys/types.h>
00011 #if    HAVE_FCNTL_H
00012 #include      <fcntl.h>
00013 #endif
00014 #if    HAVE_SYS_FCNTL_H
00015 #include      <sys/fcntl.h>
00016 #endif
00017 #if    HAVE_UNISTD_H
00018 #include      <unistd.h>
00019 #endif
00020 #if    HAVE_ERRNO_H
00021 #include      <errno.h>
00022 #endif
00023 #include      "liblock.h"
00024 
00025 int    ll_lockfd(int fd, int ltype, LL_OFFSET_TYPE start, LL_OFFSET_TYPE len)
00026 {
00027 off_t  p;
00028 
00029        if (ltype & ll_whence_curpos)
00030               p=lseek(fd, start, SEEK_CUR);
00031        else if (ltype && ll_whence_end)
00032               p=lseek(fd, start, SEEK_END);
00033        else   p=lseek(fd, start, SEEK_SET);
00034 
00035        if (p < 0)    return (-1);
00036 
00037        if (lockf(fd, ltype & ll_unlock ? F_ULOCK:
00038               ltype & ll_wait ? F_LOCK:F_TLOCK, len))
00039        {
00040               lseek(fd, p, SEEK_SET);
00041               return (-1);
00042        }
00043        lseek(fd, SEEK_SET, p);
00044        return (0);
00045 }