Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* fcntl module */ |
| 3 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 6 | #ifdef HAVE_UNISTD_H |
| 7 | #include <unistd.h> |
| 8 | #endif |
| 9 | |
| 10 | #ifdef HAVE_SYS_FILE_H |
| 11 | #include <sys/file.h> |
| 12 | #endif |
| 13 | |
Guido van Rossum | 3d65fa3 | 1996-12-09 18:49:14 +0000 | [diff] [blame] | 14 | #include <sys/ioctl.h> |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 15 | #include <fcntl.h> |
| 16 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 17 | |
| 18 | /* fcntl(fd, opt, [arg]) */ |
| 19 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 20 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 21 | fcntl_fcntl(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 22 | { |
| 23 | int fd; |
| 24 | int code; |
| 25 | int arg; |
| 26 | int ret; |
| 27 | char *str; |
| 28 | int len; |
| 29 | char buf[1024]; |
| 30 | |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 31 | if (PyArg_ParseTuple(args, "iis#:fcntl", &fd, &code, &str, &len)) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 32 | if (len > sizeof buf) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 33 | PyErr_SetString(PyExc_ValueError, |
| 34 | "fcntl string arg too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 35 | return NULL; |
| 36 | } |
| 37 | memcpy(buf, str, len); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 38 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 39 | ret = fcntl(fd, code, buf); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 40 | Py_END_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 41 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 42 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 43 | return NULL; |
| 44 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 45 | return PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 48 | PyErr_Clear(); |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 49 | arg = 0; |
| 50 | if (!PyArg_ParseTuple(args, "ii|i;fcntl requires 2 integers and optionally a third integer or a string", |
| 51 | &fd, &code, &arg)) { |
| 52 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 53 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 54 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 55 | ret = fcntl(fd, code, arg); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 56 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 57 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 58 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 61 | return PyInt_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 64 | static char fcntl_doc [] = |
| 65 | |
| 66 | "fcntl(fd, opt, [arg])\n\ |
| 67 | \n\ |
| 68 | Perform the requested operation on file descriptor fd. The operation\n\ |
| 69 | is defined by op and is operating system dependent. Typically these\n\ |
| 70 | codes can be retrieved from the library module FCNTL. The argument arg\n\ |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 71 | is optional, and defaults to 0; it may be an int or a string. If arg is\n\ |
| 72 | given as a string, the return value of fcntl is a string of that length,\n\ |
| 73 | containing the resulting value put in the arg buffer by the operating system.\n\ |
| 74 | The length of the arg string is not allowed to exceed 1024 bytes. If the arg\n\ |
| 75 | given is an integer or if none is specified, the result value is an integer\n\ |
| 76 | corresponding to the return value of the fcntl call in the C code."; |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 78 | |
| 79 | /* ioctl(fd, opt, [arg]) */ |
| 80 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 81 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 82 | fcntl_ioctl(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 83 | { |
| 84 | int fd; |
| 85 | int code; |
| 86 | int arg; |
| 87 | int ret; |
| 88 | char *str; |
| 89 | int len; |
| 90 | char buf[1024]; |
| 91 | |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 92 | if (PyArg_ParseTuple(args, "iis#:ioctl", &fd, &code, &str, &len)) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 93 | if (len > sizeof buf) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 94 | PyErr_SetString(PyExc_ValueError, |
| 95 | "ioctl string arg too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | memcpy(buf, str, len); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 99 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 100 | ret = ioctl(fd, code, buf); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 101 | Py_END_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 102 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 103 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 106 | return PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 109 | PyErr_Clear(); |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 110 | arg = 0; |
| 111 | if (!PyArg_ParseTuple(args, "ii|i;ioctl requires 2 integers and optionally a third integer or a string", |
| 112 | &fd, &code, &arg)) { |
| 113 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 114 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 115 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 116 | ret = ioctl(fd, code, arg); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 117 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 118 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 119 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 122 | return PyInt_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 125 | static char ioctl_doc [] = |
| 126 | "ioctl(fd, opt, [arg])\n\ |
| 127 | \n\ |
| 128 | Perform the requested operation on file descriptor fd. The operation\n\ |
| 129 | is defined by op and is operating system dependent. Typically these\n\ |
| 130 | codes can be retrieved from the library module IOCTL. The argument arg\n\ |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 131 | is optional, and defaults to 0; it may be an int or a string. If arg is\n\ |
| 132 | given as a string, the return value of ioctl is a string of that length,\n\ |
| 133 | containing the resulting value put in the arg buffer by the operating system.\n\ |
| 134 | The length of the arg string is not allowed to exceed 1024 bytes. If the arg\n\ |
| 135 | given is an integer or if none is specified, the result value is an integer\n\ |
| 136 | corresponding to the return value of the ioctl call in the C code."; |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 139 | /* flock(fd, operation) */ |
| 140 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 141 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 142 | fcntl_flock(PyObject *self, PyObject *args) |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 143 | { |
| 144 | int fd; |
| 145 | int code; |
| 146 | int ret; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 148 | if (!PyArg_ParseTuple(args, "ii:flock", &fd, &code)) |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 149 | return NULL; |
| 150 | |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 151 | #ifdef HAVE_FLOCK |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 152 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 153 | ret = flock(fd, code); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 154 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 155 | #else |
| 156 | |
| 157 | #ifndef LOCK_SH |
| 158 | #define LOCK_SH 1 /* shared lock */ |
| 159 | #define LOCK_EX 2 /* exclusive lock */ |
| 160 | #define LOCK_NB 4 /* don't block when locking */ |
| 161 | #define LOCK_UN 8 /* unlock */ |
| 162 | #endif |
| 163 | { |
| 164 | struct flock l; |
| 165 | if (code == LOCK_UN) |
| 166 | l.l_type = F_UNLCK; |
| 167 | else if (code & LOCK_SH) |
| 168 | l.l_type = F_RDLCK; |
| 169 | else if (code & LOCK_EX) |
| 170 | l.l_type = F_WRLCK; |
| 171 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 172 | PyErr_SetString(PyExc_ValueError, |
| 173 | "unrecognized flock argument"); |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
| 176 | l.l_whence = l.l_start = l.l_len = 0; |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 177 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 178 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 179 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 180 | } |
| 181 | #endif /* HAVE_FLOCK */ |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 182 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 183 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 186 | Py_INCREF(Py_None); |
| 187 | return Py_None; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 190 | static char flock_doc [] = |
| 191 | "flock(fd, operation)\n\ |
| 192 | \n\ |
| 193 | Perform the lock operation op on file descriptor fd. See the Unix \n\ |
| 194 | manual flock(3) for details. (On some systems, this function is\n\ |
| 195 | emulated using fcntl().)"; |
| 196 | |
| 197 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 198 | /* lockf(fd, operation) */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 199 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 200 | fcntl_lockf(PyObject *self, PyObject *args) |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 201 | { |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 202 | int fd, code, ret, whence = 0; |
| 203 | PyObject *lenobj = NULL, *startobj = NULL; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 205 | if (!PyArg_ParseTuple(args, "ii|OOi:lockf", &fd, &code, |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 206 | &lenobj, &startobj, &whence)) |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 207 | return NULL; |
| 208 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 209 | #ifndef LOCK_SH |
| 210 | #define LOCK_SH 1 /* shared lock */ |
| 211 | #define LOCK_EX 2 /* exclusive lock */ |
| 212 | #define LOCK_NB 4 /* don't block when locking */ |
| 213 | #define LOCK_UN 8 /* unlock */ |
| 214 | #endif |
| 215 | { |
| 216 | struct flock l; |
| 217 | if (code == LOCK_UN) |
| 218 | l.l_type = F_UNLCK; |
| 219 | else if (code & LOCK_SH) |
| 220 | l.l_type = F_RDLCK; |
| 221 | else if (code & LOCK_EX) |
| 222 | l.l_type = F_WRLCK; |
| 223 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 224 | PyErr_SetString(PyExc_ValueError, |
| 225 | "unrecognized flock argument"); |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 228 | l.l_start = l.l_len = 0; |
| 229 | if (startobj != NULL) { |
| 230 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 231 | l.l_start = PyInt_AsLong(startobj); |
| 232 | #else |
| 233 | l.l_start = PyLong_Check(startobj) ? |
| 234 | PyLong_AsLongLong(startobj) : |
| 235 | PyInt_AsLong(startobj); |
| 236 | #endif |
| 237 | if (PyErr_Occurred()) |
| 238 | return NULL; |
| 239 | } |
| 240 | if (lenobj != NULL) { |
| 241 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 242 | l.l_len = PyInt_AsLong(lenobj); |
| 243 | #else |
| 244 | l.l_len = PyLong_Check(lenobj) ? |
| 245 | PyLong_AsLongLong(lenobj) : |
| 246 | PyInt_AsLong(lenobj); |
| 247 | #endif |
| 248 | if (PyErr_Occurred()) |
| 249 | return NULL; |
| 250 | } |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 251 | l.l_whence = whence; |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 252 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 253 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 254 | Py_END_ALLOW_THREADS |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 255 | } |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 256 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 257 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 258 | return NULL; |
| 259 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 260 | Py_INCREF(Py_None); |
| 261 | return Py_None; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 262 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 264 | static char lockf_doc [] = |
Sjoerd Mullender | 82e00d6 | 2001-01-25 10:10:39 +0000 | [diff] [blame] | 265 | "lockf (fd, operation, length=0, start=0, whence=0)\n\ |
| 266 | \n\ |
| 267 | This is essentially a wrapper around the fcntl() locking calls. fd is the\n\ |
| 268 | file descriptor of the file to lock or unlock, and operation is one of the\n\ |
| 269 | following values:\n\ |
| 270 | \n\ |
| 271 | LOCK_UN - unlock\n\ |
| 272 | LOCK_SH - acquire a shared lock\n\ |
| 273 | LOCK_EX - acquire an exclusive lock\n\ |
| 274 | \n\ |
| 275 | When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with\n\ |
| 276 | LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\ |
| 277 | lock cannot be acquired, an IOError will be raised and the exception will\n\ |
| 278 | have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\ |
| 279 | system -- for portability, check for either value).\n\ |
| 280 | \n\ |
| 281 | length is the number of bytes to lock, with the default meaning to lock to\n\ |
| 282 | EOF. start is the byte offset, relative to whence, to that the lock\n\ |
| 283 | starts. whence is as with fileobj.seek(), specifically:\n\ |
| 284 | \n\ |
| 285 | 0 - relative to the start of the file (SEEK_SET)\n\ |
| 286 | 1 - relative to the current buffer position (SEEK_CUR)\n\ |
Barry Warsaw | bd3dc1f | 2001-01-25 00:20:13 +0000 | [diff] [blame] | 287 | 2 - relative to the end of the file (SEEK_END)"; |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 289 | /* List of functions */ |
| 290 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 291 | static PyMethodDef fcntl_methods[] = { |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 292 | {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc}, |
| 293 | {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc}, |
| 294 | {"flock", fcntl_flock, METH_VARARGS, flock_doc}, |
| 295 | {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 296 | {NULL, NULL} /* sentinel */ |
| 297 | }; |
| 298 | |
| 299 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 300 | static char module_doc [] = |
| 301 | |
| 302 | "This module performs file control and I/O control on file \n\ |
| 303 | descriptors. It is an interface to the fcntl() and ioctl() Unix\n\ |
| 304 | routines. File descriptors can be obtained with the fileno() method of\n\ |
| 305 | a file or socket object."; |
| 306 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 307 | /* Module initialisation */ |
| 308 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 309 | static int |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 310 | ins(PyObject* d, char* symbol, long value) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 311 | { |
| 312 | PyObject* v = PyInt_FromLong(value); |
| 313 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 314 | return -1; |
| 315 | |
| 316 | Py_DECREF(v); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 321 | all_ins(PyObject* d) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 322 | { |
| 323 | if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1; |
| 324 | if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1; |
| 325 | if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1; |
| 326 | if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1; |
Guido van Rossum | 7c14103 | 1997-08-15 02:52:08 +0000 | [diff] [blame] | 327 | return 0; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 330 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 331 | initfcntl(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 332 | { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 333 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 335 | /* Create the module and add the functions and documentation */ |
| 336 | m = Py_InitModule3("fcntl", fcntl_methods, module_doc); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 337 | |
| 338 | /* Add some symbolic constants to the module */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 339 | d = PyModule_GetDict(m); |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 340 | all_ins(d); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 341 | } |