OSDN Git Service

Moving libc_hidden_proto's into #ifdef UCLIBC_INTERNAL block
[uclinux-h8/uClibc.git] / libc / misc / file / lockf64.c
1 /* Copyright (C) 1994, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <_lfs_64.h>
20
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/syscall.h>
27
28 #ifdef __NR_fcntl64
29 #define flock flock64
30 #define fcntl fcntl64
31 #undef F_GETLK
32 #define F_GETLK F_GETLK64
33 #undef F_SETLK
34 #define F_SETLK F_SETLK64
35 libc_hidden_proto(fcntl64)
36 #else
37 libc_hidden_proto(fcntl)
38 #endif
39
40 /* Experimentally off - libc_hidden_proto(memset) */
41 libc_hidden_proto(getpid)
42
43 /* lockf is a simplified interface to fcntl's locking facilities.  */
44
45 libc_hidden_proto(lockf64)
46 int lockf64 (int fd, int cmd, off64_t len64)
47 {
48     struct flock fl;
49     off_t len = (off_t) len64;
50
51     if (len64 != (off64_t) len)
52     {
53         /* We can't represent the length.  */
54         __set_errno(EOVERFLOW);
55         return -1;
56     }
57
58     memset((char *) &fl, '\0', sizeof (fl));
59
60     /* lockf is always relative to the current file position.  */
61     fl.l_whence = SEEK_CUR;
62     fl.l_start = 0;
63     fl.l_len = len;
64
65     switch (cmd)
66     {
67         case F_TEST:
68             /* Test the lock: return 0 if FD is unlocked or locked by this process;
69                return -1, set errno to EACCES, if another process holds the lock.  */
70             fl.l_type = F_RDLCK;
71             if (fcntl (fd, F_GETLK, &fl) < 0)
72                 return -1;
73             if (fl.l_type == F_UNLCK || fl.l_pid == getpid ())
74                 return 0;
75             __set_errno(EACCES);
76             return -1;
77
78         case F_ULOCK:
79             fl.l_type = F_UNLCK;
80             cmd = F_SETLK;
81             break;
82         case F_LOCK:
83             fl.l_type = F_WRLCK;
84             cmd = F_SETLKW;
85             break;
86         case F_TLOCK:
87             fl.l_type = F_WRLCK;
88             cmd = F_SETLK;
89             break;
90
91         default:
92             __set_errno(EINVAL);
93             return -1;
94     }
95
96     return fcntl(fd, cmd, &fl);
97 }
98 libc_hidden_def(lockf64)