1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 PyDoc_STRVAR(fcntl_fcntl__doc__,
6 "fcntl($module, fd, cmd, arg=0, /)\n"
7 "--\n"
8 "\n"
9 "Perform the operation `cmd` on file descriptor fd.\n"
10 "\n"
11 "The values used for `cmd` are operating system dependent, and are available\n"
12 "as constants in the fcntl module, using the same names as used in\n"
13 "the relevant C header files. The argument arg is optional, and\n"
14 "defaults to 0; it may be an int or a string. If arg is given as a string,\n"
15 "the return value of fcntl is a string of that length, containing the\n"
16 "resulting value put in the arg buffer by the operating system. The length\n"
17 "of the arg string is not allowed to exceed 1024 bytes. If the arg given\n"
18 "is an integer or if none is specified, the result value is an integer\n"
19 "corresponding to the return value of the fcntl call in the C code.");
20
21 #define FCNTL_FCNTL_METHODDEF \
22 {"fcntl", (PyCFunction)(void(*)(void))fcntl_fcntl, METH_FASTCALL, fcntl_fcntl__doc__},
23
24 static PyObject *
25 fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg);
26
27 static PyObject *
fcntl_fcntl(PyObject * module,PyObject * const * args,Py_ssize_t nargs)28 fcntl_fcntl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
29 {
30 PyObject *return_value = NULL;
31 int fd;
32 int code;
33 PyObject *arg = NULL;
34
35 if (nargs < 2) {
36 PyErr_Format(PyExc_TypeError, "fcntl expected at least 2 arguments, got %zd", nargs);
37 goto exit;
38 }
39 if (nargs > 3) {
40 PyErr_Format(PyExc_TypeError, "fcntl expected at most 3 arguments, got %zd", nargs);
41 goto exit;
42 }
43 fd = PyObject_AsFileDescriptor(args[0]);
44 if (fd < 0) {
45 goto exit;
46 }
47 code = PyLong_AsInt(args[1]);
48 if (code == -1 && PyErr_Occurred()) {
49 goto exit;
50 }
51 if (nargs < 3) {
52 goto skip_optional;
53 }
54 arg = args[2];
55 skip_optional:
56 return_value = fcntl_fcntl_impl(module, fd, code, arg);
57
58 exit:
59 return return_value;
60 }
61
62 PyDoc_STRVAR(fcntl_ioctl__doc__,
63 "ioctl($module, fd, request, arg=0, mutate_flag=True, /)\n"
64 "--\n"
65 "\n"
66 "Perform the operation `request` on file descriptor `fd`.\n"
67 "\n"
68 "The values used for `request` are operating system dependent, and are available\n"
69 "as constants in the fcntl or termios library modules, using the same names as\n"
70 "used in the relevant C header files.\n"
71 "\n"
72 "The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
73 "buffer containing character data (most likely a string or an array).\n"
74 "\n"
75 "If the argument is a mutable buffer (such as an array) and if the\n"
76 "mutate_flag argument (which is only allowed in this case) is true then the\n"
77 "buffer is (in effect) passed to the operating system and changes made by\n"
78 "the OS will be reflected in the contents of the buffer after the call has\n"
79 "returned. The return value is the integer returned by the ioctl system\n"
80 "call.\n"
81 "\n"
82 "If the argument is a mutable buffer and the mutable_flag argument is false,\n"
83 "the behavior is as if a string had been passed.\n"
84 "\n"
85 "If the argument is an immutable buffer (most likely a string) then a copy\n"
86 "of the buffer is passed to the operating system and the return value is a\n"
87 "string of the same length containing whatever the operating system put in\n"
88 "the buffer. The length of the arg buffer in this case is not allowed to\n"
89 "exceed 1024 bytes.\n"
90 "\n"
91 "If the arg given is an integer or if none is specified, the result value is\n"
92 "an integer corresponding to the return value of the ioctl call in the C\n"
93 "code.");
94
95 #define FCNTL_IOCTL_METHODDEF \
96 {"ioctl", (PyCFunction)(void(*)(void))fcntl_ioctl, METH_FASTCALL, fcntl_ioctl__doc__},
97
98 static PyObject *
99 fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
100 PyObject *ob_arg, int mutate_arg);
101
102 static PyObject *
fcntl_ioctl(PyObject * module,PyObject * const * args,Py_ssize_t nargs)103 fcntl_ioctl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
104 {
105 PyObject *return_value = NULL;
106 int fd;
107 unsigned int code;
108 PyObject *ob_arg = NULL;
109 int mutate_arg = 1;
110
111 if (nargs < 2) {
112 PyErr_Format(PyExc_TypeError, "ioctl expected at least 2 arguments, got %zd", nargs);
113 goto exit;
114 }
115 if (nargs > 4) {
116 PyErr_Format(PyExc_TypeError, "ioctl expected at most 4 arguments, got %zd", nargs);
117 goto exit;
118 }
119 fd = PyObject_AsFileDescriptor(args[0]);
120 if (fd < 0) {
121 goto exit;
122 }
123 code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
124 if (code == (unsigned int)-1 && PyErr_Occurred()) {
125 goto exit;
126 }
127 if (nargs < 3) {
128 goto skip_optional;
129 }
130 ob_arg = args[2];
131 if (nargs < 4) {
132 goto skip_optional;
133 }
134 mutate_arg = PyObject_IsTrue(args[3]);
135 if (mutate_arg < 0) {
136 goto exit;
137 }
138 skip_optional:
139 return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
140
141 exit:
142 return return_value;
143 }
144
145 PyDoc_STRVAR(fcntl_flock__doc__,
146 "flock($module, fd, operation, /)\n"
147 "--\n"
148 "\n"
149 "Perform the lock operation `operation` on file descriptor `fd`.\n"
150 "\n"
151 "See the Unix manual page for flock(2) for details (On some systems, this\n"
152 "function is emulated using fcntl()).");
153
154 #define FCNTL_FLOCK_METHODDEF \
155 {"flock", (PyCFunction)(void(*)(void))fcntl_flock, METH_FASTCALL, fcntl_flock__doc__},
156
157 static PyObject *
158 fcntl_flock_impl(PyObject *module, int fd, int code);
159
160 static PyObject *
fcntl_flock(PyObject * module,PyObject * const * args,Py_ssize_t nargs)161 fcntl_flock(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
162 {
163 PyObject *return_value = NULL;
164 int fd;
165 int code;
166
167 if (nargs != 2) {
168 PyErr_Format(PyExc_TypeError, "flock expected 2 arguments, got %zd", nargs);
169 goto exit;
170 }
171 fd = PyObject_AsFileDescriptor(args[0]);
172 if (fd < 0) {
173 goto exit;
174 }
175 code = PyLong_AsInt(args[1]);
176 if (code == -1 && PyErr_Occurred()) {
177 goto exit;
178 }
179 return_value = fcntl_flock_impl(module, fd, code);
180
181 exit:
182 return return_value;
183 }
184
185 PyDoc_STRVAR(fcntl_lockf__doc__,
186 "lockf($module, fd, cmd, len=0, start=0, whence=0, /)\n"
187 "--\n"
188 "\n"
189 "A wrapper around the fcntl() locking calls.\n"
190 "\n"
191 "`fd` is the file descriptor of the file to lock or unlock, and operation is one\n"
192 "of the following values:\n"
193 "\n"
194 " LOCK_UN - unlock\n"
195 " LOCK_SH - acquire a shared lock\n"
196 " LOCK_EX - acquire an exclusive lock\n"
197 "\n"
198 "When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
199 "LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
200 "lock cannot be acquired, an OSError will be raised and the exception will\n"
201 "have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
202 "system -- for portability, check for either value).\n"
203 "\n"
204 "`len` is the number of bytes to lock, with the default meaning to lock to\n"
205 "EOF. `start` is the byte offset, relative to `whence`, to that the lock\n"
206 "starts. `whence` is as with fileobj.seek(), specifically:\n"
207 "\n"
208 " 0 - relative to the start of the file (SEEK_SET)\n"
209 " 1 - relative to the current buffer position (SEEK_CUR)\n"
210 " 2 - relative to the end of the file (SEEK_END)");
211
212 #define FCNTL_LOCKF_METHODDEF \
213 {"lockf", (PyCFunction)(void(*)(void))fcntl_lockf, METH_FASTCALL, fcntl_lockf__doc__},
214
215 static PyObject *
216 fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
217 PyObject *startobj, int whence);
218
219 static PyObject *
fcntl_lockf(PyObject * module,PyObject * const * args,Py_ssize_t nargs)220 fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
221 {
222 PyObject *return_value = NULL;
223 int fd;
224 int code;
225 PyObject *lenobj = NULL;
226 PyObject *startobj = NULL;
227 int whence = 0;
228
229 if (nargs < 2) {
230 PyErr_Format(PyExc_TypeError, "lockf expected at least 2 arguments, got %zd", nargs);
231 goto exit;
232 }
233 if (nargs > 5) {
234 PyErr_Format(PyExc_TypeError, "lockf expected at most 5 arguments, got %zd", nargs);
235 goto exit;
236 }
237 fd = PyObject_AsFileDescriptor(args[0]);
238 if (fd < 0) {
239 goto exit;
240 }
241 code = PyLong_AsInt(args[1]);
242 if (code == -1 && PyErr_Occurred()) {
243 goto exit;
244 }
245 if (nargs < 3) {
246 goto skip_optional;
247 }
248 lenobj = args[2];
249 if (nargs < 4) {
250 goto skip_optional;
251 }
252 startobj = args[3];
253 if (nargs < 5) {
254 goto skip_optional;
255 }
256 whence = PyLong_AsInt(args[4]);
257 if (whence == -1 && PyErr_Occurred()) {
258 goto exit;
259 }
260 skip_optional:
261 return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
262
263 exit:
264 return return_value;
265 }
266 /*[clinic end generated code: output=26793691ab1c75ba input=a9049054013a1b77]*/
267