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