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 * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 30 | fcntl_fcntl(self, args) |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 31 | PyObject *self; /* Not used */ |
| 32 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 33 | { |
| 34 | int fd; |
| 35 | int code; |
| 36 | int arg; |
| 37 | int ret; |
| 38 | char *str; |
| 39 | int len; |
| 40 | char buf[1024]; |
| 41 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 42 | if (PyArg_Parse(args, "(iis#)", &fd, &code, &str, &len)) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 43 | if (len > sizeof buf) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 44 | PyErr_SetString(PyExc_ValueError, |
| 45 | "fcntl string arg too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 46 | return NULL; |
| 47 | } |
| 48 | memcpy(buf, str, len); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 49 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 50 | ret = fcntl(fd, code, buf); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 51 | Py_END_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 52 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 53 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 56 | return PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 59 | PyErr_Clear(); |
| 60 | if (PyArg_Parse(args, "(ii)", &fd, &code)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 61 | arg = 0; |
| 62 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 63 | PyErr_Clear(); |
| 64 | if (!PyArg_Parse(args, "(iii)", &fd, &code, &arg)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 67 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 68 | ret = fcntl(fd, code, arg); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 69 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 70 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 71 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 74 | return PyInt_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 77 | static char fcntl_doc [] = |
| 78 | |
| 79 | "fcntl(fd, opt, [arg])\n\ |
| 80 | \n\ |
| 81 | Perform the requested operation on file descriptor fd. The operation\n\ |
| 82 | is defined by op and is operating system dependent. Typically these\n\ |
| 83 | codes can be retrieved from the library module FCNTL. The argument arg\n\ |
| 84 | is optional, and defaults to 0; it may be an int or a string."; |
| 85 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 86 | |
| 87 | /* ioctl(fd, opt, [arg]) */ |
| 88 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 89 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 90 | fcntl_ioctl(self, args) |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 91 | PyObject *self; /* Not used */ |
| 92 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 93 | { |
| 94 | int fd; |
| 95 | int code; |
| 96 | int arg; |
| 97 | int ret; |
| 98 | char *str; |
| 99 | int len; |
| 100 | char buf[1024]; |
| 101 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 102 | if (PyArg_Parse(args, "(iis#)", &fd, &code, &str, &len)) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 103 | if (len > sizeof buf) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 104 | PyErr_SetString(PyExc_ValueError, |
| 105 | "ioctl string arg too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
| 108 | memcpy(buf, str, len); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 109 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 110 | ret = ioctl(fd, code, buf); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 111 | Py_END_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 112 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 113 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 116 | return PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 119 | PyErr_Clear(); |
| 120 | if (PyArg_Parse(args, "(ii)", &fd, &code)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 121 | arg = 0; |
| 122 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 123 | PyErr_Clear(); |
| 124 | if (!PyArg_Parse(args, "(iii)", &fd, &code, &arg)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 125 | return NULL; |
| 126 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 127 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 128 | ret = ioctl(fd, code, arg); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 129 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 130 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 131 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 134 | return PyInt_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 137 | static char ioctl_doc [] = |
| 138 | "ioctl(fd, opt, [arg])\n\ |
| 139 | \n\ |
| 140 | Perform the requested operation on file descriptor fd. The operation\n\ |
| 141 | is defined by op and is operating system dependent. Typically these\n\ |
| 142 | codes can be retrieved from the library module IOCTL. The argument arg\n\ |
| 143 | is optional, and defaults to 0; it may be an int or a string."; |
| 144 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 146 | /* flock(fd, operation) */ |
| 147 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 148 | static PyObject * |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 149 | fcntl_flock(self, args) |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 150 | PyObject *self; /* Not used */ |
| 151 | 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 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 157 | if (!PyArg_Parse(args, "(ii)", &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 * |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 209 | fcntl_lockf(self, args) |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 210 | PyObject *self; /* Not used */ |
| 211 | PyObject *args; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 212 | { |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 213 | int fd, code, ret, whence = 0; |
| 214 | PyObject *lenobj = NULL, *startobj = NULL; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 216 | if (!PyArg_ParseTuple(args, "ii|OOi:lockf", &fd, &code, |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 217 | &lenobj, &startobj, &whence)) |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 218 | return NULL; |
| 219 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 220 | #ifndef LOCK_SH |
| 221 | #define LOCK_SH 1 /* shared lock */ |
| 222 | #define LOCK_EX 2 /* exclusive lock */ |
| 223 | #define LOCK_NB 4 /* don't block when locking */ |
| 224 | #define LOCK_UN 8 /* unlock */ |
| 225 | #endif |
| 226 | { |
| 227 | struct flock l; |
| 228 | if (code == LOCK_UN) |
| 229 | l.l_type = F_UNLCK; |
| 230 | else if (code & LOCK_SH) |
| 231 | l.l_type = F_RDLCK; |
| 232 | else if (code & LOCK_EX) |
| 233 | l.l_type = F_WRLCK; |
| 234 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 235 | PyErr_SetString(PyExc_ValueError, |
| 236 | "unrecognized flock argument"); |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 239 | l.l_start = l.l_len = 0; |
| 240 | if (startobj != NULL) { |
| 241 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 242 | l.l_start = PyInt_AsLong(startobj); |
| 243 | #else |
| 244 | l.l_start = PyLong_Check(startobj) ? |
| 245 | PyLong_AsLongLong(startobj) : |
| 246 | PyInt_AsLong(startobj); |
| 247 | #endif |
| 248 | if (PyErr_Occurred()) |
| 249 | return NULL; |
| 250 | } |
| 251 | if (lenobj != NULL) { |
| 252 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 253 | l.l_len = PyInt_AsLong(lenobj); |
| 254 | #else |
| 255 | l.l_len = PyLong_Check(lenobj) ? |
| 256 | PyLong_AsLongLong(lenobj) : |
| 257 | PyInt_AsLong(lenobj); |
| 258 | #endif |
| 259 | if (PyErr_Occurred()) |
| 260 | return NULL; |
| 261 | } |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 262 | l.l_whence = whence; |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 263 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 264 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 265 | Py_END_ALLOW_THREADS |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 266 | } |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 267 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 268 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 269 | return NULL; |
| 270 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 271 | Py_INCREF(Py_None); |
| 272 | return Py_None; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 273 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 275 | static char lockf_doc [] = |
| 276 | "lockf (fd, operation)\n\ |
| 277 | \n\ |
| 278 | This is a wrapper around the FCNTL.F_SETLK and FCNTL.F_SETLKW fcntl()\n\ |
| 279 | calls. See the Unix manual for details."; |
| 280 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 281 | /* List of functions */ |
| 282 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 283 | static PyMethodDef fcntl_methods[] = { |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 284 | {"fcntl", fcntl_fcntl, 0, fcntl_doc}, |
| 285 | {"ioctl", fcntl_ioctl, 0, ioctl_doc}, |
| 286 | {"flock", fcntl_flock, 0, flock_doc}, |
| 287 | {"lockf", fcntl_lockf, 1, lockf_doc}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 288 | {NULL, NULL} /* sentinel */ |
| 289 | }; |
| 290 | |
| 291 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 292 | static char module_doc [] = |
| 293 | |
| 294 | "This module performs file control and I/O control on file \n\ |
| 295 | descriptors. It is an interface to the fcntl() and ioctl() Unix\n\ |
| 296 | routines. File descriptors can be obtained with the fileno() method of\n\ |
| 297 | a file or socket object."; |
| 298 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 299 | /* Module initialisation */ |
| 300 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 301 | static int |
| 302 | ins(d, symbol, value) |
| 303 | PyObject* d; |
| 304 | char* symbol; |
| 305 | long value; |
| 306 | { |
| 307 | PyObject* v = PyInt_FromLong(value); |
| 308 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 309 | return -1; |
| 310 | |
| 311 | Py_DECREF(v); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int |
| 316 | all_ins(d) |
| 317 | PyObject* d; |
| 318 | { |
| 319 | if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1; |
| 320 | if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1; |
| 321 | if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1; |
| 322 | if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1; |
Guido van Rossum | 7c14103 | 1997-08-15 02:52:08 +0000 | [diff] [blame] | 323 | return 0; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 326 | DL_EXPORT(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 327 | initfcntl() |
| 328 | { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 329 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 331 | /* Create the module and add the functions and documentation */ |
| 332 | m = Py_InitModule3("fcntl", fcntl_methods, module_doc); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 333 | |
| 334 | /* Add some symbolic constants to the module */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 335 | d = PyModule_GetDict(m); |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 336 | all_ins(d); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 337 | |
| 338 | /* Check for errors */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 339 | if (PyErr_Occurred()) |
| 340 | Py_FatalError("can't initialize module fcntl"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 341 | } |