blob: d08123a22ad72317555ba34661f61e686ffe8c29 [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum02975121992-08-17 08:55:12 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum02975121992-08-17 08:55:12 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum02975121992-08-17 08:55:12 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum02975121992-08-17 08:55:12 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum02975121992-08-17 08:55:12 +000029
30******************************************************************/
31
32/* fcntl module */
33
34#include "allobjects.h"
35#include "modsupport.h"
36
Guido van Rossuma376cc51996-12-05 23:43:35 +000037#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
40
41#ifdef HAVE_SYS_FILE_H
42#include <sys/file.h>
43#endif
44
Guido van Rossum3c0b79c1996-06-11 15:11:34 +000045#include <fcntl.h>
46
Guido van Rossum02975121992-08-17 08:55:12 +000047
48/* fcntl(fd, opt, [arg]) */
49
50static object *
51fcntl_fcntl(self, args)
52 object *self; /* Not used */
53 object *args;
54{
55 int fd;
56 int code;
57 int arg;
58 int ret;
59 char *str;
60 int len;
61 char buf[1024];
62
63 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
64 if (len > sizeof buf) {
65 err_setstr(ValueError, "fcntl string arg too long");
66 return NULL;
67 }
68 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +000069 BGN_SAVE
70 ret = fcntl(fd, code, buf);
71 END_SAVE
72 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +000073 err_errno(IOError);
74 return NULL;
75 }
76 return newsizedstringobject(buf, len);
77 }
78
79 err_clear();
80 if (getargs(args, "(ii)", &fd, &code))
81 arg = 0;
82 else {
83 err_clear();
84 if (!getargs(args, "(iii)", &fd, &code, &arg))
85 return NULL;
86 }
Guido van Rossum903f4871995-10-07 19:18:22 +000087 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000088 ret = fcntl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +000089 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000090 if (ret < 0) {
91 err_errno(IOError);
92 return NULL;
93 }
94 return newintobject((long)ret);
95}
96
97
98/* ioctl(fd, opt, [arg]) */
99
100static object *
101fcntl_ioctl(self, args)
102 object *self; /* Not used */
103 object *args;
104{
105 int fd;
106 int code;
107 int arg;
108 int ret;
109 char *str;
110 int len;
111 char buf[1024];
112
113 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
114 if (len > sizeof buf) {
115 err_setstr(ValueError, "ioctl string arg too long");
116 return NULL;
117 }
118 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +0000119 BGN_SAVE
120 ret = ioctl(fd, code, buf);
121 END_SAVE
122 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +0000123 err_errno(IOError);
124 return NULL;
125 }
126 return newsizedstringobject(buf, len);
127 }
128
129 err_clear();
130 if (getargs(args, "(ii)", &fd, &code))
131 arg = 0;
132 else {
133 err_clear();
134 if (!getargs(args, "(iii)", &fd, &code, &arg))
135 return NULL;
136 }
Guido van Rossum903f4871995-10-07 19:18:22 +0000137 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000138 ret = ioctl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +0000139 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000140 if (ret < 0) {
141 err_errno(IOError);
142 return NULL;
143 }
144 return newintobject((long)ret);
145}
146
147
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000148/* flock(fd, operation) */
149
150static object *
151fcntl_flock(self, args)
152 object *self; /* Not used */
153 object *args;
154{
155 int fd;
156 int code;
157 int ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000158
159 if (!getargs(args, "(ii)", &fd, &code))
160 return NULL;
161
162 BGN_SAVE
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000163#ifdef HAVE_FLOCK
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000164 ret = flock(fd, code);
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000165#else
166
167#ifndef LOCK_SH
168#define LOCK_SH 1 /* shared lock */
169#define LOCK_EX 2 /* exclusive lock */
170#define LOCK_NB 4 /* don't block when locking */
171#define LOCK_UN 8 /* unlock */
172#endif
173 {
174 struct flock l;
175 if (code == LOCK_UN)
176 l.l_type = F_UNLCK;
177 else if (code & LOCK_SH)
178 l.l_type = F_RDLCK;
179 else if (code & LOCK_EX)
180 l.l_type = F_WRLCK;
181 else {
182 err_setstr(ValueError, "unrecognized flock argument");
183 return NULL;
184 }
185 l.l_whence = l.l_start = l.l_len = 0;
186 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
187 }
188#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000189 END_SAVE
190 if (ret < 0) {
191 err_errno(IOError);
192 return NULL;
193 }
194 INCREF(None);
195 return None;
196}
197
Guido van Rossumc8643641996-09-11 23:17:20 +0000198/* lockf(fd, operation) */
199static object *
200fcntl_lockf(self, args)
201 object *self; /* Not used */
202 object *args;
203{
204 int fd, code, len = 0, start = 0, whence = 0, ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000205
Guido van Rossumc8643641996-09-11 23:17:20 +0000206 if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len,
207 &start, &whence))
208 return NULL;
209
210 BGN_SAVE
211#ifndef LOCK_SH
212#define LOCK_SH 1 /* shared lock */
213#define LOCK_EX 2 /* exclusive lock */
214#define LOCK_NB 4 /* don't block when locking */
215#define LOCK_UN 8 /* unlock */
216#endif
217 {
218 struct flock l;
219 if (code == LOCK_UN)
220 l.l_type = F_UNLCK;
221 else if (code & LOCK_SH)
222 l.l_type = F_RDLCK;
223 else if (code & LOCK_EX)
224 l.l_type = F_WRLCK;
225 else {
226 err_setstr(ValueError, "unrecognized flock argument");
227 return NULL;
228 }
229 l.l_len = len;
230 l.l_start = start;
231 l.l_whence = whence;
232 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
233 }
234 END_SAVE
235 if (ret < 0) {
236 err_errno(IOError);
237 return NULL;
238 }
239 INCREF(None);
240 return None;
241}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000242
Guido van Rossum02975121992-08-17 08:55:12 +0000243/* List of functions */
244
245static struct methodlist fcntl_methods[] = {
246 {"fcntl", fcntl_fcntl},
247 {"ioctl", fcntl_ioctl},
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000248 {"flock", fcntl_flock},
Guido van Rossumc8643641996-09-11 23:17:20 +0000249 {"lockf", fcntl_lockf, 1},
Guido van Rossum02975121992-08-17 08:55:12 +0000250 {NULL, NULL} /* sentinel */
251};
252
253
254/* Module initialisation */
255
256void
257initfcntl()
258{
259 object *m, *d;
260
261 /* Create the module and add the functions */
262 m = initmodule("fcntl", fcntl_methods);
263
264 /* Add some symbolic constants to the module */
265 d = getmoduledict(m);
266
267 /* Check for errors */
268 if (err_occurred())
269 fatal("can't initialize module fcntl");
270}