blob: ae6bcdd4d0bff6e1d37b18f71848dfdc174a9753 [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum02975121992-08-17 08:55:12 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum02975121992-08-17 08:55:12 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum02975121992-08-17 08:55:12 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum02975121992-08-17 08:55:12 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum02975121992-08-17 08:55:12 +000029
30******************************************************************/
31
32/* fcntl module */
33
34#include "allobjects.h"
35#include "modsupport.h"
36
Guido van Rossum3c0b79c1996-06-11 15:11:34 +000037#include <fcntl.h>
38
Guido van Rossum02975121992-08-17 08:55:12 +000039
40/* fcntl(fd, opt, [arg]) */
41
42static object *
43fcntl_fcntl(self, args)
44 object *self; /* Not used */
45 object *args;
46{
47 int fd;
48 int code;
49 int arg;
50 int ret;
51 char *str;
52 int len;
53 char buf[1024];
54
55 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
56 if (len > sizeof buf) {
57 err_setstr(ValueError, "fcntl string arg too long");
58 return NULL;
59 }
60 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +000061 BGN_SAVE
62 ret = fcntl(fd, code, buf);
63 END_SAVE
64 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +000065 err_errno(IOError);
66 return NULL;
67 }
68 return newsizedstringobject(buf, len);
69 }
70
71 err_clear();
72 if (getargs(args, "(ii)", &fd, &code))
73 arg = 0;
74 else {
75 err_clear();
76 if (!getargs(args, "(iii)", &fd, &code, &arg))
77 return NULL;
78 }
Guido van Rossum903f4871995-10-07 19:18:22 +000079 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000080 ret = fcntl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +000081 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000082 if (ret < 0) {
83 err_errno(IOError);
84 return NULL;
85 }
86 return newintobject((long)ret);
87}
88
89
90/* ioctl(fd, opt, [arg]) */
91
92static object *
93fcntl_ioctl(self, args)
94 object *self; /* Not used */
95 object *args;
96{
97 int fd;
98 int code;
99 int arg;
100 int ret;
101 char *str;
102 int len;
103 char buf[1024];
104
105 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
106 if (len > sizeof buf) {
107 err_setstr(ValueError, "ioctl string arg too long");
108 return NULL;
109 }
110 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +0000111 BGN_SAVE
112 ret = ioctl(fd, code, buf);
113 END_SAVE
114 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +0000115 err_errno(IOError);
116 return NULL;
117 }
118 return newsizedstringobject(buf, len);
119 }
120
121 err_clear();
122 if (getargs(args, "(ii)", &fd, &code))
123 arg = 0;
124 else {
125 err_clear();
126 if (!getargs(args, "(iii)", &fd, &code, &arg))
127 return NULL;
128 }
Guido van Rossum903f4871995-10-07 19:18:22 +0000129 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000130 ret = ioctl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +0000131 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000132 if (ret < 0) {
133 err_errno(IOError);
134 return NULL;
135 }
136 return newintobject((long)ret);
137}
138
139
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000140/* flock(fd, operation) */
141
142static object *
143fcntl_flock(self, args)
144 object *self; /* Not used */
145 object *args;
146{
147 int fd;
148 int code;
149 int ret;
150 FILE *f;
151
152 if (!getargs(args, "(ii)", &fd, &code))
153 return NULL;
154
155 BGN_SAVE
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000156#ifdef HAVE_FLOCK
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000157 ret = flock(fd, code);
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000158#else
159
160#ifndef LOCK_SH
161#define LOCK_SH 1 /* shared lock */
162#define LOCK_EX 2 /* exclusive lock */
163#define LOCK_NB 4 /* don't block when locking */
164#define LOCK_UN 8 /* unlock */
165#endif
166 {
167 struct flock l;
168 if (code == LOCK_UN)
169 l.l_type = F_UNLCK;
170 else if (code & LOCK_SH)
171 l.l_type = F_RDLCK;
172 else if (code & LOCK_EX)
173 l.l_type = F_WRLCK;
174 else {
175 err_setstr(ValueError, "unrecognized flock argument");
176 return NULL;
177 }
178 l.l_whence = l.l_start = l.l_len = 0;
179 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
180 }
181#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000182 END_SAVE
183 if (ret < 0) {
184 err_errno(IOError);
185 return NULL;
186 }
187 INCREF(None);
188 return None;
189}
190
Guido van Rossumc8643641996-09-11 23:17:20 +0000191/* lockf(fd, operation) */
192static object *
193fcntl_lockf(self, args)
194 object *self; /* Not used */
195 object *args;
196{
197 int fd, code, len = 0, start = 0, whence = 0, ret;
198 FILE *f;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000199
Guido van Rossumc8643641996-09-11 23:17:20 +0000200 if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len,
201 &start, &whence))
202 return NULL;
203
204 BGN_SAVE
205#ifndef LOCK_SH
206#define LOCK_SH 1 /* shared lock */
207#define LOCK_EX 2 /* exclusive lock */
208#define LOCK_NB 4 /* don't block when locking */
209#define LOCK_UN 8 /* unlock */
210#endif
211 {
212 struct flock l;
213 if (code == LOCK_UN)
214 l.l_type = F_UNLCK;
215 else if (code & LOCK_SH)
216 l.l_type = F_RDLCK;
217 else if (code & LOCK_EX)
218 l.l_type = F_WRLCK;
219 else {
220 err_setstr(ValueError, "unrecognized flock argument");
221 return NULL;
222 }
223 l.l_len = len;
224 l.l_start = start;
225 l.l_whence = whence;
226 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
227 }
228 END_SAVE
229 if (ret < 0) {
230 err_errno(IOError);
231 return NULL;
232 }
233 INCREF(None);
234 return None;
235}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000236
Guido van Rossum02975121992-08-17 08:55:12 +0000237/* List of functions */
238
239static struct methodlist fcntl_methods[] = {
240 {"fcntl", fcntl_fcntl},
241 {"ioctl", fcntl_ioctl},
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000242 {"flock", fcntl_flock},
Guido van Rossumc8643641996-09-11 23:17:20 +0000243 {"lockf", fcntl_lockf, 1},
Guido van Rossum02975121992-08-17 08:55:12 +0000244 {NULL, NULL} /* sentinel */
245};
246
247
248/* Module initialisation */
249
250void
251initfcntl()
252{
253 object *m, *d;
254
255 /* Create the module and add the functions */
256 m = initmodule("fcntl", fcntl_methods);
257
258 /* Add some symbolic constants to the module */
259 d = getmoduledict(m);
260
261 /* Check for errors */
262 if (err_occurred())
263 fatal("can't initialize module fcntl");
264}