Last set of CW Pro 5 projects (probably)
[python/dscho.git] / Doc / lib / libfcntl.tex
blobbf4f375a8bfa9dd5e02c4af69984698cc2e5e39d
1 \section{\module{fcntl} ---
2 The \function{fcntl()} and \function{ioctl()} system calls}
4 \declaremodule{builtin}{fcntl}
5 \platform{Unix}
6 \modulesynopsis{The \function{fcntl()} and \function{ioctl()} system calls.}
7 \sectionauthor{Jaap Vermeulen}{}
9 \indexii{UNIX@\UNIX{}}{file control}
10 \indexii{UNIX@\UNIX{}}{I/O control}
12 This module performs file control and I/O control on file descriptors.
13 It is an interface to the \cfunction{fcntl()} and \cfunction{ioctl()}
14 \UNIX{} routines. File descriptors can be obtained with the
15 \method{fileno()} method of a file or socket object.
17 The module defines the following functions:
20 \begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
21 Perform the requested operation on file descriptor \var{fd}.
22 The operation is defined by \var{op} and is operating system
23 dependent. Typically these codes can be retrieved from the library
24 module \module{FCNTL}\refstmodindex{FCNTL}. The argument \var{arg}
25 is optional, and defaults to the integer value \code{0}. When
26 present, it can either be an integer value, or a string. With
27 the argument missing or an integer value, the return value of this
28 function is the integer return value of the C \cfunction{fcntl()}
29 call. When the argument is a string it represents a binary
30 structure, e.g.\ created by \function{struct.pack()}. The binary
31 data is copied to a buffer whose address is passed to the C
32 \cfunction{fcntl()} call. The return value after a successful call
33 is the contents of the buffer, converted to a string object. The length
34 of the returned string will be the same as the length of the \var{arg}
35 argument. This is limited to 1024 bytes. If the information returned
36 in the buffer by the operating system is larger than 1024 bytes,
37 this is most likely to result in a segmentation violation or a more
38 subtle data corruption.
40 If the \cfunction{fcntl()} fails, an \exception{IOError} is
41 raised.
42 \end{funcdesc}
44 \begin{funcdesc}{ioctl}{fd, op, arg}
45 This function is identical to the \function{fcntl()} function, except
46 that the operations are typically defined in the library module
47 \module{IOCTL}.
48 \end{funcdesc}
50 \begin{funcdesc}{flock}{fd, op}
51 Perform the lock operation \var{op} on file descriptor \var{fd}.
52 See the \UNIX{} manual \manpage{flock}{3} for details. (On some
53 systems, this function is emulated using \cfunction{fcntl()}.)
54 \end{funcdesc}
56 \begin{funcdesc}{lockf}{fd, code, \optional{len, \optional{start, \optional{whence}}}}
57 This is a wrapper around the \constant{FCNTL.F_SETLK} and
58 \constant{FCNTL.F_SETLKW} \function{fcntl()} calls. See the \UNIX{}
59 manual for details.
60 \end{funcdesc}
62 If the library modules \module{FCNTL}\refstmodindex{FCNTL} or
63 \module{IOCTL}\refstmodindex{IOCTL} are missing, you can find the
64 opcodes in the C include files \code{<sys/fcntl.h>} and
65 \code{<sys/ioctl.h>}. You can create the modules yourself with the
66 \program{h2py} script, found in the \file{Tools/scripts/} directory.
69 Examples (all on a SVR4 compliant system):
71 \begin{verbatim}
72 import struct, fcntl, FCNTL
74 file = open(...)
75 rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1)
77 lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
78 rv = fcntl.fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
79 \end{verbatim}
81 Note that in the first example the return value variable \code{rv} will
82 hold an integer value; in the second example it will hold a string
83 value. The structure lay-out for the \var{lockdata} variable is
84 system dependent --- therefore using the \function{flock()} call may be
85 better.