annotate Modules/fcntlmodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents 09736aeab831
children 3ee9a3f60bc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* fcntl module */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
4 #include "Python.h"
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
6 #ifdef HAVE_UNISTD_H
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
7 #include <unistd.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
8 #endif
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
9
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
10 #ifdef HAVE_SYS_FILE_H
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
11 #include <sys/file.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
12 #endif
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
13
4623
1fab981465c8 Include <sys/ioctl.h>, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
14 #include <sys/ioctl.h>
3662
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
15 #include <fcntl.h>
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
16
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 /* fcntl(fd, opt, [arg]) */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
20 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
21 fcntl_fcntl(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 int fd;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 int code;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 int arg;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 int ret;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 char *str;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 int len;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 char buf[1024];
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
31 if (PyArg_ParseTuple(args, "iis#:fcntl", &fd, &code, &str, &len)) {
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if (len > sizeof buf) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
33 PyErr_SetString(PyExc_ValueError,
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
34 "fcntl string arg too long");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 memcpy(buf, str, len);
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
38 Py_BEGIN_ALLOW_THREADS
3153
9a896086d3c3 add BGN/END_SAVE macros around fcntl/ioctl calls
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
39 ret = fcntl(fd, code, buf);
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
40 Py_END_ALLOW_THREADS
3153
9a896086d3c3 add BGN/END_SAVE macros around fcntl/ioctl calls
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
41 if (ret < 0) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
42 PyErr_SetFromErrno(PyExc_IOError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
45 return PyString_FromStringAndSize(buf, len);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
48 PyErr_Clear();
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
49 arg = 0;
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
50 if (!PyArg_ParseTuple(args, "ii|i;fcntl requires 2 integers and optionally a third integer or a string",
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
51 &fd, &code, &arg)) {
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
52 return NULL;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
54 Py_BEGIN_ALLOW_THREADS
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 ret = fcntl(fd, code, arg);
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
56 Py_END_ALLOW_THREADS
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 if (ret < 0) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
58 PyErr_SetFromErrno(PyExc_IOError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
61 return PyInt_FromLong((long)ret);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
64 static char fcntl_doc [] =
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
65
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
66 "fcntl(fd, opt, [arg])\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
67 \n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
68 Perform the requested operation on file descriptor fd. The operation\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
69 is defined by op and is operating system dependent. Typically these\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
70 codes can be retrieved from the library module FCNTL. The argument arg\n\
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
71 is optional, and defaults to 0; it may be an int or a string. If arg is\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
72 given as a string, the return value of fcntl is a string of that length,\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
73 containing the resulting value put in the arg buffer by the operating system.\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
74 The length of the arg string is not allowed to exceed 1024 bytes. If the arg\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
75 given is an integer or if none is specified, the result value is an integer\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
76 corresponding to the return value of the fcntl call in the C code.";
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
77
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 /* ioctl(fd, opt, [arg]) */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
81 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
82 fcntl_ioctl(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 int fd;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 int code;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 int arg;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 int ret;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 char *str;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 int len;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 char buf[1024];
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
92 if (PyArg_ParseTuple(args, "iis#:ioctl", &fd, &code, &str, &len)) {
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if (len > sizeof buf) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
94 PyErr_SetString(PyExc_ValueError,
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
95 "ioctl string arg too long");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 memcpy(buf, str, len);
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
99 Py_BEGIN_ALLOW_THREADS
3153
9a896086d3c3 add BGN/END_SAVE macros around fcntl/ioctl calls
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
100 ret = ioctl(fd, code, buf);
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
101 Py_END_ALLOW_THREADS
3153
9a896086d3c3 add BGN/END_SAVE macros around fcntl/ioctl calls
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
102 if (ret < 0) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
103 PyErr_SetFromErrno(PyExc_IOError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
106 return PyString_FromStringAndSize(buf, len);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
109 PyErr_Clear();
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
110 arg = 0;
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
111 if (!PyArg_ParseTuple(args, "ii|i;ioctl requires 2 integers and optionally a third integer or a string",
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
112 &fd, &code, &arg)) {
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
113 return NULL;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
115 Py_BEGIN_ALLOW_THREADS
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 ret = ioctl(fd, code, arg);
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
117 Py_END_ALLOW_THREADS
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if (ret < 0) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
119 PyErr_SetFromErrno(PyExc_IOError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
122 return PyInt_FromLong((long)ret);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
125 static char ioctl_doc [] =
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
126 "ioctl(fd, opt, [arg])\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
127 \n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
128 Perform the requested operation on file descriptor fd. The operation\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
129 is defined by op and is operating system dependent. Typically these\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
130 codes can be retrieved from the library module IOCTL. The argument arg\n\
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
131 is optional, and defaults to 0; it may be an int or a string. If arg is\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
132 given as a string, the return value of ioctl is a string of that length,\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
133 containing the resulting value put in the arg buffer by the operating system.\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
134 The length of the arg string is not allowed to exceed 1024 bytes. If the arg\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
135 given is an integer or if none is specified, the result value is an integer\n\
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
136 corresponding to the return value of the ioctl call in the C code.";
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
137
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
139 /* flock(fd, operation) */
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
140
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
141 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
142 fcntl_flock(PyObject *self, PyObject *args)
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
143 {
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
144 int fd;
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
145 int code;
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
146 int ret;
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
147
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
148 if (!PyArg_ParseTuple(args, "ii:flock", &fd, &code))
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
149 return NULL;
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
150
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
151 #ifdef HAVE_FLOCK
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
152 Py_BEGIN_ALLOW_THREADS
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
153 ret = flock(fd, code);
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
154 Py_END_ALLOW_THREADS
3662
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
155 #else
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
156
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
157 #ifndef LOCK_SH
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
158 #define LOCK_SH 1 /* shared lock */
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
159 #define LOCK_EX 2 /* exclusive lock */
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
160 #define LOCK_NB 4 /* don't block when locking */
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
161 #define LOCK_UN 8 /* unlock */
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
162 #endif
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
163 {
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
164 struct flock l;
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
165 if (code == LOCK_UN)
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
166 l.l_type = F_UNLCK;
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
167 else if (code & LOCK_SH)
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
168 l.l_type = F_RDLCK;
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
169 else if (code & LOCK_EX)
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
170 l.l_type = F_WRLCK;
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
171 else {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
172 PyErr_SetString(PyExc_ValueError,
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
173 "unrecognized flock argument");
3662
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
174 return NULL;
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
175 }
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
176 l.l_whence = l.l_start = l.l_len = 0;
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
177 Py_BEGIN_ALLOW_THREADS
3662
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
178 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
179 Py_END_ALLOW_THREADS
3662
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
180 }
399470a958c2 A fcntl implementation for systems (like Solaris) without flock() call.
Guido van Rossum <guido@python.org>
parents: 3598
diff changeset
181 #endif /* HAVE_FLOCK */
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
182 if (ret < 0) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
183 PyErr_SetFromErrno(PyExc_IOError);
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
184 return NULL;
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
185 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
186 Py_INCREF(Py_None);
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
187 return Py_None;
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
188 }
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
189
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
190 static char flock_doc [] =
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
191 "flock(fd, operation)\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
192 \n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
193 Perform the lock operation op on file descriptor fd. See the Unix \n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
194 manual flock(3) for details. (On some systems, this function is\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
195 emulated using fcntl().)";
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
196
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
197
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
198 /* lockf(fd, operation) */
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
199 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
200 fcntl_lockf(PyObject *self, PyObject *args)
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
201 {
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
202 int fd, code, ret, whence = 0;
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
203 PyObject *lenobj = NULL, *startobj = NULL;
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
204
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9786
diff changeset
205 if (!PyArg_ParseTuple(args, "ii|OOi:lockf", &fd, &code,
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
206 &lenobj, &startobj, &whence))
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
207 return NULL;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
208
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
209 #ifndef LOCK_SH
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
210 #define LOCK_SH 1 /* shared lock */
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
211 #define LOCK_EX 2 /* exclusive lock */
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
212 #define LOCK_NB 4 /* don't block when locking */
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
213 #define LOCK_UN 8 /* unlock */
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
214 #endif
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
215 {
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
216 struct flock l;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
217 if (code == LOCK_UN)
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
218 l.l_type = F_UNLCK;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
219 else if (code & LOCK_SH)
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
220 l.l_type = F_RDLCK;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
221 else if (code & LOCK_EX)
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
222 l.l_type = F_WRLCK;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
223 else {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
224 PyErr_SetString(PyExc_ValueError,
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
225 "unrecognized flock argument");
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
226 return NULL;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
227 }
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
228 l.l_start = l.l_len = 0;
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
229 if (startobj != NULL) {
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
230 #if !defined(HAVE_LARGEFILE_SUPPORT)
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
231 l.l_start = PyInt_AsLong(startobj);
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
232 #else
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
233 l.l_start = PyLong_Check(startobj) ?
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
234 PyLong_AsLongLong(startobj) :
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
235 PyInt_AsLong(startobj);
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
236 #endif
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
237 if (PyErr_Occurred())
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
238 return NULL;
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
239 }
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
240 if (lenobj != NULL) {
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
241 #if !defined(HAVE_LARGEFILE_SUPPORT)
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
242 l.l_len = PyInt_AsLong(lenobj);
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
243 #else
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
244 l.l_len = PyLong_Check(lenobj) ?
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
245 PyLong_AsLongLong(lenobj) :
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
246 PyInt_AsLong(lenobj);
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
247 #endif
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
248 if (PyErr_Occurred())
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
249 return NULL;
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
250 }
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
251 l.l_whence = whence;
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
252 Py_BEGIN_ALLOW_THREADS
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
253 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
9786
40f723ac26e8 Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
254 Py_END_ALLOW_THREADS
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
255 }
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
256 if (ret < 0) {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
257 PyErr_SetFromErrno(PyExc_IOError);
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
258 return NULL;
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
259 }
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
260 Py_INCREF(Py_None);
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
261 return Py_None;
4293
69bbc21f55cf Added lockf() call
Guido van Rossum <guido@python.org>
parents: 3662
diff changeset
262 }
3598
6c9260ae0d16 Added flock().
Guido van Rossum <guido@python.org>
parents: 3153
diff changeset
263
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
264 static char lockf_doc [] =
16245
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
265 "lockf (fd, operation, length=0, start=0, whence=0)\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
266 \n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
267 This is essentially a wrapper around the fcntl() locking calls. fd is the\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
268 file descriptor of the file to lock or unlock, and operation is one of the\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
269 following values:\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
270 \n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
271 LOCK_UN - unlock\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
272 LOCK_SH - acquire a shared lock\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
273 LOCK_EX - acquire an exclusive lock\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
274 \n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
275 When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
276 LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
277 lock cannot be acquired, an IOError will be raised and the exception will\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
278 have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
279 system -- for portability, check for either value).\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
280 \n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
281 length is the number of bytes to lock, with the default meaning to lock to\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
282 EOF. start is the byte offset, relative to whence, to that the lock\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
283 starts. whence is as with fileobj.seek(), specifically:\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
284 \n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
285 0 - relative to the start of the file (SEEK_SET)\n\
09736aeab831 Let's keep things portable to non GCC compilers, please.
Sjoerd Mullender <sjoerd@acm.org>
parents: 16238
diff changeset
286 1 - relative to the current buffer position (SEEK_CUR)\n\
16238
b4d4f2021114 lockf_doc: a much better description of the lockf() function, based on
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
287 2 - relative to the end of the file (SEEK_END)";
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
288
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 /* List of functions */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
291 static PyMethodDef fcntl_methods[] = {
13814
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
292 {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
293 {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
294 {"flock", fcntl_flock, METH_VARARGS, flock_doc},
45d8f23df218 Rob Hooft (Patch #101046): use PyArg_ParseTuple everywhere.
Guido van Rossum <guido@python.org>
parents: 13651
diff changeset
295 {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 {NULL, NULL} /* sentinel */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 };
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
300 static char module_doc [] =
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
301
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
302 "This module performs file control and I/O control on file \n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
303 descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
304 routines. File descriptors can be obtained with the fileno() method of\n\
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
305 a file or socket object.";
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
306
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 /* Module initialisation */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
5912
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
309 static int
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
310 ins(PyObject* d, char* symbol, long value)
5912
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
311 {
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
312 PyObject* v = PyInt_FromLong(value);
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
313 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
314 return -1;
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
315
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
316 Py_DECREF(v);
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
317 return 0;
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
318 }
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
319
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
320 static int
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
321 all_ins(PyObject* d)
5912
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
322 {
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
323 if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
324 if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
325 if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
326 if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
6055
72d004971b26 Keep gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5912
diff changeset
327 return 0;
5912
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
328 }
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
329
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9506
diff changeset
330 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13519
diff changeset
331 initfcntl(void)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 {
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
333 PyObject *m, *d;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
9506
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
335 /* Create the module and add the functions and documentation */
04426224302a Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
336 m = Py_InitModule3("fcntl", fcntl_methods, module_doc);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 /* Add some symbolic constants to the module */
4757
e1f1725cae6d Grandly renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4623
diff changeset
339 d = PyModule_GetDict(m);
5912
f527c12f2e55 Add definitions for symbolic constants LOCK_{EX,NB,SH,UN}.
Guido van Rossum <guido@python.org>
parents: 4758
diff changeset
340 all_ins(d);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 }