]> git.pond.sub.org Git - empserver/blob - src/lib/gen/lock.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / gen / lock.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  lock.c: Lock a file
29  * 
30  *  Known contributors to this file:
31  *      Doug Hay, 1998
32  */
33
34 #ifdef aix
35 #define L_SET 0
36 #endif /* aix */
37
38 #include "misc.h"
39 #include "gen.h"
40 #include "common.h"
41 #include <fcntl.h>
42
43 #ifdef sys5
44 #include <unistd.h>
45 #endif
46 #if !defined(L_SET) && !defined(_WIN32)
47 #include <sys/file.h>
48 #endif
49
50 #ifdef aix
51 #define L_SET 0
52 #endif /* aix */
53
54 #if defined(_WIN32)
55 #include <sys/locking.h>
56
57 int
58 file_lock(int fd)
59 {
60     if (_locking(fd, _LK_LOCK, 0) < 0) {
61         logerror("file lock (fd %d) failed", fd);
62         return 0;
63     }
64     return 1;
65 }
66
67 int
68 file_unlock(int fd)
69 {
70     if (_locking(fd, _LK_UNLCK, 0) < 0) {
71         logerror("file lock (fd %d) failed", fd);
72         return 0;
73     }
74     return 1;
75 }
76
77 #else
78
79 #ifndef NOFLOCK
80
81 int flock();
82
83 int
84 file_lock(int fd)
85 {
86     if (flock(fd, LOCK_EX) < 0) {
87         logerror("file lock (fd %d) failed", fd);
88         return 0;
89     }
90     return 1;
91 }
92
93 int
94 file_unlock(int fd)
95 {
96     if (flock(fd, LOCK_UN) < 0) {
97         logerror("file unlock (fd %d) failed", fd);
98         return 0;
99     }
100     return 1;
101 }
102
103 #else
104
105 int
106 file_lock(int fd)
107 {
108     struct flock lock;
109
110     lock.l_type = F_WRLCK;
111     lock.l_whence = L_SET;
112     lock.l_start = 0;
113     lock.l_len = 0;
114     lock.l_pid = 0;
115     if (fcntl(fd, F_SETLKW, &lock) < 0) {
116         logerror("file lock (fd %d) failed", fd);
117         return 0;
118     }
119     return 1;
120 }
121
122 int
123 file_unlock(int fd)
124 {
125     struct flock lock;
126
127     lock.l_type = F_UNLCK;
128     lock.l_whence = L_SET;
129     lock.l_start = 0;
130     lock.l_len = 0;
131     lock.l_pid = 0;
132     if (fcntl(fd, F_SETLKW, &lock) < 0) {
133         logerror("file unlock (fd %d) failed", fd);
134         return 0;
135     }
136     return 1;
137 }
138 #endif
139
140 #endif /* _WIN32 */