blob: c547ed0a477864170169a8859edc277ef5ab2f67 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* fcntl module */
26
27#include "allobjects.h"
28#include "modsupport.h"
29
Guido van Rossum3c0b79c1996-06-11 15:11:34 +000030#include <fcntl.h>
31
Guido van Rossum02975121992-08-17 08:55:12 +000032
33/* fcntl(fd, opt, [arg]) */
34
35static object *
36fcntl_fcntl(self, args)
37 object *self; /* Not used */
38 object *args;
39{
40 int fd;
41 int code;
42 int arg;
43 int ret;
44 char *str;
45 int len;
46 char buf[1024];
47
48 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
49 if (len > sizeof buf) {
50 err_setstr(ValueError, "fcntl string arg too long");
51 return NULL;
52 }
53 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +000054 BGN_SAVE
55 ret = fcntl(fd, code, buf);
56 END_SAVE
57 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +000058 err_errno(IOError);
59 return NULL;
60 }
61 return newsizedstringobject(buf, len);
62 }
63
64 err_clear();
65 if (getargs(args, "(ii)", &fd, &code))
66 arg = 0;
67 else {
68 err_clear();
69 if (!getargs(args, "(iii)", &fd, &code, &arg))
70 return NULL;
71 }
Guido van Rossum903f4871995-10-07 19:18:22 +000072 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000073 ret = fcntl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +000074 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000075 if (ret < 0) {
76 err_errno(IOError);
77 return NULL;
78 }
79 return newintobject((long)ret);
80}
81
82
83/* ioctl(fd, opt, [arg]) */
84
85static object *
86fcntl_ioctl(self, args)
87 object *self; /* Not used */
88 object *args;
89{
90 int fd;
91 int code;
92 int arg;
93 int ret;
94 char *str;
95 int len;
96 char buf[1024];
97
98 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
99 if (len > sizeof buf) {
100 err_setstr(ValueError, "ioctl string arg too long");
101 return NULL;
102 }
103 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +0000104 BGN_SAVE
105 ret = ioctl(fd, code, buf);
106 END_SAVE
107 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +0000108 err_errno(IOError);
109 return NULL;
110 }
111 return newsizedstringobject(buf, len);
112 }
113
114 err_clear();
115 if (getargs(args, "(ii)", &fd, &code))
116 arg = 0;
117 else {
118 err_clear();
119 if (!getargs(args, "(iii)", &fd, &code, &arg))
120 return NULL;
121 }
Guido van Rossum903f4871995-10-07 19:18:22 +0000122 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000123 ret = ioctl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +0000124 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000125 if (ret < 0) {
126 err_errno(IOError);
127 return NULL;
128 }
129 return newintobject((long)ret);
130}
131
132
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000133/* flock(fd, operation) */
134
135static object *
136fcntl_flock(self, args)
137 object *self; /* Not used */
138 object *args;
139{
140 int fd;
141 int code;
142 int ret;
143 FILE *f;
144
145 if (!getargs(args, "(ii)", &fd, &code))
146 return NULL;
147
148 BGN_SAVE
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000149#ifdef HAVE_FLOCK
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000150 ret = flock(fd, code);
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000151#else
152
153#ifndef LOCK_SH
154#define LOCK_SH 1 /* shared lock */
155#define LOCK_EX 2 /* exclusive lock */
156#define LOCK_NB 4 /* don't block when locking */
157#define LOCK_UN 8 /* unlock */
158#endif
159 {
160 struct flock l;
161 if (code == LOCK_UN)
162 l.l_type = F_UNLCK;
163 else if (code & LOCK_SH)
164 l.l_type = F_RDLCK;
165 else if (code & LOCK_EX)
166 l.l_type = F_WRLCK;
167 else {
168 err_setstr(ValueError, "unrecognized flock argument");
169 return NULL;
170 }
171 l.l_whence = l.l_start = l.l_len = 0;
172 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
173 }
174#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000175 END_SAVE
176 if (ret < 0) {
177 err_errno(IOError);
178 return NULL;
179 }
180 INCREF(None);
181 return None;
182}
183
184
185
Guido van Rossum02975121992-08-17 08:55:12 +0000186/* List of functions */
187
188static struct methodlist fcntl_methods[] = {
189 {"fcntl", fcntl_fcntl},
190 {"ioctl", fcntl_ioctl},
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000191 {"flock", fcntl_flock},
Guido van Rossum02975121992-08-17 08:55:12 +0000192 {NULL, NULL} /* sentinel */
193};
194
195
196/* Module initialisation */
197
198void
199initfcntl()
200{
201 object *m, *d;
202
203 /* Create the module and add the functions */
204 m = initmodule("fcntl", fcntl_methods);
205
206 /* Add some symbolic constants to the module */
207 d = getmoduledict(m);
208
209 /* Check for errors */
210 if (err_occurred())
211 fatal("can't initialize module fcntl");
212}