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