[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/python/cpython/main/Modules/fcntlmodule.c [Back]  [Original]

/* fcntl module */

// Argument Clinic uses the internal C API
#ifndef Py_BUILD_CORE_BUILTIN
#  define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"

#include                 // EINTR
#include                 // fcntl()
#include                // memcpy()
#include             // ioctl()
#ifdef HAVE_SYS_FILE_H
#  include            // flock()
#endif
#ifdef HAVE_LINUX_FS_H
#  include 
#endif
#ifdef HAVE_STROPTS_H
#  include             // I_FLUSHBAND
#endif

#define GUARDSZ 8
// NUL followed by random bytes.
static const char guard[GUARDSZ] _Py_NONSTRING = "\x00\xfa\x69\xc4\x67\xa3\x6c\x58";

/*[clinic input]
module fcntl
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=124b58387c158179]*/

#include "clinic/fcntlmodule.c.h"

/*[clinic input]
fcntl.fcntl

    fd: fildes
    cmd as code: int
    arg: object(c_default='NULL') = 0
    /

Perform the operation cmd on file descriptor fd.

The values used for cmd are operating system dependent, and are
available as constants in the fcntl module, using the same names as used
in the relevant C header files.  The argument arg is optional, and
defaults to 0; it may be an integer, a bytes-like object or a string.
If arg is given as a string, it will be encoded to binary using the
UTF-8 encoding.

If the arg given is an integer or if none is specified, the result value
is an integer corresponding to the return value of the fcntl() call in
the C code.

If arg is given as a bytes-like object, the return value of fcntl() is a
bytes object of that length, containing the resulting value put in the
arg buffer by the operating system.
[clinic start generated code]*/

static PyObject *
fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
/*[clinic end generated code: output=888fc93b51c295bd input=77340720f11665da]*/
{
    int ret;
    int async_err = 0;

    if (PySys_Audit("fcntl.fcntl", "iiO", fd, code, arg ? arg : Py_None) < 0) {
        return NULL;
    }

    if (arg == NULL || PyIndex_Check(arg)) {
        unsigned int int_arg = 0;
        if (arg != NULL) {
            if (!PyArg_Parse(arg, "I", &int_arg)) {
                return NULL;
            }
        }

        do {
            Py_BEGIN_ALLOW_THREADS
            ret = fcntl(fd, code, (int)int_arg);
            Py_END_ALLOW_THREADS
        } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
        if (ret < 0) {
            return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
        }
        return PyLong_FromLong(ret);
    }
    if (PyUnicode_Check(arg) || PyObject_CheckBuffer(arg)) {
        Py_buffer view;
#define FCNTL_BUFSZ 1024
        /* argument plus NUL byte plus guard to detect a buffer overflow */
        char buf[FCNTL_BUFSZ+GUARDSZ];

        if (!PyArg_Parse(arg, "s*", &view)) {
            return NULL;
        }
        Py_ssize_t len = view.len;
        if (len 

Web Proxy Viewer  |  New URL  |  Original Page