1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
40 #ifdef HAVE_SYS_FILE_H
44 #include <sys/ioctl.h>
48 /* fcntl(fd, opt, [arg]) */
51 fcntl_fcntl(self
, args
)
52 PyObject
*self
; /* Not used */
63 if (PyArg_Parse(args
, "(iis#)", &fd
, &code
, &str
, &len
)) {
64 if (len
> sizeof buf
) {
65 PyErr_SetString(PyExc_ValueError
,
66 "fcntl string arg too long");
69 memcpy(buf
, str
, len
);
70 Py_BEGIN_ALLOW_THREADS
71 ret
= fcntl(fd
, code
, buf
);
74 PyErr_SetFromErrno(PyExc_IOError
);
77 return PyString_FromStringAndSize(buf
, len
);
81 if (PyArg_Parse(args
, "(ii)", &fd
, &code
))
85 if (!PyArg_Parse(args
, "(iii)", &fd
, &code
, &arg
))
88 Py_BEGIN_ALLOW_THREADS
89 ret
= fcntl(fd
, code
, arg
);
92 PyErr_SetFromErrno(PyExc_IOError
);
95 return PyInt_FromLong((long)ret
);
98 static char fcntl_doc
[] =
100 "fcntl(fd, opt, [arg])\n\
102 Perform the requested operation on file descriptor fd. The operation\n\
103 is defined by op and is operating system dependent. Typically these\n\
104 codes can be retrieved from the library module FCNTL. The argument arg\n\
105 is optional, and defaults to 0; it may be an int or a string.";
108 /* ioctl(fd, opt, [arg]) */
111 fcntl_ioctl(self
, args
)
112 PyObject
*self
; /* Not used */
123 if (PyArg_Parse(args
, "(iis#)", &fd
, &code
, &str
, &len
)) {
124 if (len
> sizeof buf
) {
125 PyErr_SetString(PyExc_ValueError
,
126 "ioctl string arg too long");
129 memcpy(buf
, str
, len
);
130 Py_BEGIN_ALLOW_THREADS
131 ret
= ioctl(fd
, code
, buf
);
134 PyErr_SetFromErrno(PyExc_IOError
);
137 return PyString_FromStringAndSize(buf
, len
);
141 if (PyArg_Parse(args
, "(ii)", &fd
, &code
))
145 if (!PyArg_Parse(args
, "(iii)", &fd
, &code
, &arg
))
148 Py_BEGIN_ALLOW_THREADS
149 ret
= ioctl(fd
, code
, arg
);
152 PyErr_SetFromErrno(PyExc_IOError
);
155 return PyInt_FromLong((long)ret
);
158 static char ioctl_doc
[] =
159 "ioctl(fd, opt, [arg])\n\
161 Perform the requested operation on file descriptor fd. The operation\n\
162 is defined by op and is operating system dependent. Typically these\n\
163 codes can be retrieved from the library module IOCTL. The argument arg\n\
164 is optional, and defaults to 0; it may be an int or a string.";
167 /* flock(fd, operation) */
170 fcntl_flock(self
, args
)
171 PyObject
*self
; /* Not used */
178 if (!PyArg_Parse(args
, "(ii)", &fd
, &code
))
182 Py_BEGIN_ALLOW_THREADS
183 ret
= flock(fd
, code
);
188 #define LOCK_SH 1 /* shared lock */
189 #define LOCK_EX 2 /* exclusive lock */
190 #define LOCK_NB 4 /* don't block when locking */
191 #define LOCK_UN 8 /* unlock */
197 else if (code
& LOCK_SH
)
199 else if (code
& LOCK_EX
)
202 PyErr_SetString(PyExc_ValueError
,
203 "unrecognized flock argument");
206 l
.l_whence
= l
.l_start
= l
.l_len
= 0;
207 Py_BEGIN_ALLOW_THREADS
208 ret
= fcntl(fd
, (code
& LOCK_NB
) ? F_SETLK
: F_SETLKW
, &l
);
211 #endif /* HAVE_FLOCK */
213 PyErr_SetFromErrno(PyExc_IOError
);
220 static char flock_doc
[] =
221 "flock(fd, operation)\n\
223 Perform the lock operation op on file descriptor fd. See the Unix \n\
224 manual flock(3) for details. (On some systems, this function is\n\
225 emulated using fcntl().)";
228 /* lockf(fd, operation) */
230 fcntl_lockf(self
, args
)
231 PyObject
*self
; /* Not used */
234 int fd
, code
, ret
, whence
= 0;
235 PyObject
*lenobj
= NULL
, *startobj
= NULL
;
237 if (!PyArg_ParseTuple(args
, "ii|OOi", &fd
, &code
,
238 &lenobj
, &startobj
, &whence
))
242 #define LOCK_SH 1 /* shared lock */
243 #define LOCK_EX 2 /* exclusive lock */
244 #define LOCK_NB 4 /* don't block when locking */
245 #define LOCK_UN 8 /* unlock */
251 else if (code
& LOCK_SH
)
253 else if (code
& LOCK_EX
)
256 PyErr_SetString(PyExc_ValueError
,
257 "unrecognized flock argument");
260 l
.l_start
= l
.l_len
= 0;
261 if (startobj
!= NULL
) {
262 #if !defined(HAVE_LARGEFILE_SUPPORT)
263 l
.l_start
= PyInt_AsLong(startobj
);
265 l
.l_start
= PyLong_Check(startobj
) ?
266 PyLong_AsLongLong(startobj
) :
267 PyInt_AsLong(startobj
);
269 if (PyErr_Occurred())
272 if (lenobj
!= NULL
) {
273 #if !defined(HAVE_LARGEFILE_SUPPORT)
274 l
.l_len
= PyInt_AsLong(lenobj
);
276 l
.l_len
= PyLong_Check(lenobj
) ?
277 PyLong_AsLongLong(lenobj
) :
278 PyInt_AsLong(lenobj
);
280 if (PyErr_Occurred())
284 Py_BEGIN_ALLOW_THREADS
285 ret
= fcntl(fd
, (code
& LOCK_NB
) ? F_SETLK
: F_SETLKW
, &l
);
289 PyErr_SetFromErrno(PyExc_IOError
);
296 static char lockf_doc
[] =
297 "lockf (fd, operation)\n\
299 This is a wrapper around the FCNTL.F_SETLK and FCNTL.F_SETLKW fcntl()\n\
300 calls. See the Unix manual for details.";
302 /* List of functions */
304 static PyMethodDef fcntl_methods
[] = {
305 {"fcntl", fcntl_fcntl
, 0, fcntl_doc
},
306 {"ioctl", fcntl_ioctl
, 0, ioctl_doc
},
307 {"flock", fcntl_flock
, 0, flock_doc
},
308 {"lockf", fcntl_lockf
, 1, lockf_doc
},
309 {NULL
, NULL
} /* sentinel */
313 static char module_doc
[] =
315 "This module performs file control and I/O control on file \n\
316 descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
317 routines. File descriptors can be obtained with the fileno() method of\n\
318 a file or socket object.";
320 /* Module initialisation */
323 ins(d
, symbol
, value
)
328 PyObject
* v
= PyInt_FromLong(value
);
329 if (!v
|| PyDict_SetItemString(d
, symbol
, v
) < 0)
340 if (ins(d
, "LOCK_SH", (long)LOCK_SH
)) return -1;
341 if (ins(d
, "LOCK_EX", (long)LOCK_EX
)) return -1;
342 if (ins(d
, "LOCK_NB", (long)LOCK_NB
)) return -1;
343 if (ins(d
, "LOCK_UN", (long)LOCK_UN
)) return -1;
352 /* Create the module and add the functions and documentation */
353 m
= Py_InitModule3("fcntl", fcntl_methods
, module_doc
);
355 /* Add some symbolic constants to the module */
356 d
= PyModule_GetDict(m
);
359 /* Check for errors */
360 if (PyErr_Occurred())
361 Py_FatalError("can't initialize module fcntl");