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