blob: 842e4369d6dc5f53e559177efb193fb3f82bce3f [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 Rossuma376cc51996-12-05 23:43:35 +000037#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
40
41#ifdef HAVE_SYS_FILE_H
42#include <sys/file.h>
43#endif
44
Guido van Rossum3d65fa31996-12-09 18:49:14 +000045#include <sys/ioctl.h>
Guido van Rossum3c0b79c1996-06-11 15:11:34 +000046#include <fcntl.h>
47
Guido van Rossum02975121992-08-17 08:55:12 +000048
49/* fcntl(fd, opt, [arg]) */
50
51static object *
52fcntl_fcntl(self, args)
53 object *self; /* Not used */
54 object *args;
55{
56 int fd;
57 int code;
58 int arg;
59 int ret;
60 char *str;
61 int len;
62 char buf[1024];
63
64 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
65 if (len > sizeof buf) {
66 err_setstr(ValueError, "fcntl string arg too long");
67 return NULL;
68 }
69 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +000070 BGN_SAVE
71 ret = fcntl(fd, code, buf);
72 END_SAVE
73 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +000074 err_errno(IOError);
75 return NULL;
76 }
77 return newsizedstringobject(buf, len);
78 }
79
80 err_clear();
81 if (getargs(args, "(ii)", &fd, &code))
82 arg = 0;
83 else {
84 err_clear();
85 if (!getargs(args, "(iii)", &fd, &code, &arg))
86 return NULL;
87 }
Guido van Rossum903f4871995-10-07 19:18:22 +000088 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000089 ret = fcntl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +000090 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000091 if (ret < 0) {
92 err_errno(IOError);
93 return NULL;
94 }
95 return newintobject((long)ret);
96}
97
98
99/* ioctl(fd, opt, [arg]) */
100
101static object *
102fcntl_ioctl(self, args)
103 object *self; /* Not used */
104 object *args;
105{
106 int fd;
107 int code;
108 int arg;
109 int ret;
110 char *str;
111 int len;
112 char buf[1024];
113
114 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
115 if (len > sizeof buf) {
116 err_setstr(ValueError, "ioctl string arg too long");
117 return NULL;
118 }
119 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +0000120 BGN_SAVE
121 ret = ioctl(fd, code, buf);
122 END_SAVE
123 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +0000124 err_errno(IOError);
125 return NULL;
126 }
127 return newsizedstringobject(buf, len);
128 }
129
130 err_clear();
131 if (getargs(args, "(ii)", &fd, &code))
132 arg = 0;
133 else {
134 err_clear();
135 if (!getargs(args, "(iii)", &fd, &code, &arg))
136 return NULL;
137 }
Guido van Rossum903f4871995-10-07 19:18:22 +0000138 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000139 ret = ioctl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +0000140 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000141 if (ret < 0) {
142 err_errno(IOError);
143 return NULL;
144 }
145 return newintobject((long)ret);
146}
147
148
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000149/* flock(fd, operation) */
150
151static object *
152fcntl_flock(self, args)
153 object *self; /* Not used */
154 object *args;
155{
156 int fd;
157 int code;
158 int ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000159
160 if (!getargs(args, "(ii)", &fd, &code))
161 return NULL;
162
163 BGN_SAVE
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000164#ifdef HAVE_FLOCK
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000165 ret = flock(fd, code);
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000166#else
167
168#ifndef LOCK_SH
169#define LOCK_SH 1 /* shared lock */
170#define LOCK_EX 2 /* exclusive lock */
171#define LOCK_NB 4 /* don't block when locking */
172#define LOCK_UN 8 /* unlock */
173#endif
174 {
175 struct flock l;
176 if (code == LOCK_UN)
177 l.l_type = F_UNLCK;
178 else if (code & LOCK_SH)
179 l.l_type = F_RDLCK;
180 else if (code & LOCK_EX)
181 l.l_type = F_WRLCK;
182 else {
183 err_setstr(ValueError, "unrecognized flock argument");
184 return NULL;
185 }
186 l.l_whence = l.l_start = l.l_len = 0;
187 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
188 }
189#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000190 END_SAVE
191 if (ret < 0) {
192 err_errno(IOError);
193 return NULL;
194 }
195 INCREF(None);
196 return None;
197}
198
Guido van Rossumc8643641996-09-11 23:17:20 +0000199/* lockf(fd, operation) */
200static object *
201fcntl_lockf(self, args)
202 object *self; /* Not used */
203 object *args;
204{
205 int fd, code, len = 0, start = 0, whence = 0, ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000206
Guido van Rossumc8643641996-09-11 23:17:20 +0000207 if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len,
208 &start, &whence))
209 return NULL;
210
211 BGN_SAVE
212#ifndef LOCK_SH
213#define LOCK_SH 1 /* shared lock */
214#define LOCK_EX 2 /* exclusive lock */
215#define LOCK_NB 4 /* don't block when locking */
216#define LOCK_UN 8 /* unlock */
217#endif
218 {
219 struct flock l;
220 if (code == LOCK_UN)
221 l.l_type = F_UNLCK;
222 else if (code & LOCK_SH)
223 l.l_type = F_RDLCK;
224 else if (code & LOCK_EX)
225 l.l_type = F_WRLCK;
226 else {
227 err_setstr(ValueError, "unrecognized flock argument");
228 return NULL;
229 }
230 l.l_len = len;
231 l.l_start = start;
232 l.l_whence = whence;
233 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
234 }
235 END_SAVE
236 if (ret < 0) {
237 err_errno(IOError);
238 return NULL;
239 }
240 INCREF(None);
241 return None;
242}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000243
Guido van Rossum02975121992-08-17 08:55:12 +0000244/* List of functions */
245
246static struct methodlist fcntl_methods[] = {
247 {"fcntl", fcntl_fcntl},
248 {"ioctl", fcntl_ioctl},
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000249 {"flock", fcntl_flock},
Guido van Rossumc8643641996-09-11 23:17:20 +0000250 {"lockf", fcntl_lockf, 1},
Guido van Rossum02975121992-08-17 08:55:12 +0000251 {NULL, NULL} /* sentinel */
252};
253
254
255/* Module initialisation */
256
257void
258initfcntl()
259{
260 object *m, *d;
261
262 /* Create the module and add the functions */
263 m = initmodule("fcntl", fcntl_methods);
264
265 /* Add some symbolic constants to the module */
266 d = getmoduledict(m);
267
268 /* Check for errors */
269 if (err_occurred())
270 fatal("can't initialize module fcntl");
271}