blob: d2efd60311ab7fb540e22318547a3e663c9cdf17 [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
30
31/* fcntl(fd, opt, [arg]) */
32
33static object *
34fcntl_fcntl(self, args)
35 object *self; /* Not used */
36 object *args;
37{
38 int fd;
39 int code;
40 int arg;
41 int ret;
42 char *str;
43 int len;
44 char buf[1024];
45
46 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
47 if (len > sizeof buf) {
48 err_setstr(ValueError, "fcntl string arg too long");
49 return NULL;
50 }
51 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +000052 BGN_SAVE
53 ret = fcntl(fd, code, buf);
54 END_SAVE
55 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +000056 err_errno(IOError);
57 return NULL;
58 }
59 return newsizedstringobject(buf, len);
60 }
61
62 err_clear();
63 if (getargs(args, "(ii)", &fd, &code))
64 arg = 0;
65 else {
66 err_clear();
67 if (!getargs(args, "(iii)", &fd, &code, &arg))
68 return NULL;
69 }
Guido van Rossum903f4871995-10-07 19:18:22 +000070 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000071 ret = fcntl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +000072 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +000073 if (ret < 0) {
74 err_errno(IOError);
75 return NULL;
76 }
77 return newintobject((long)ret);
78}
79
80
81/* ioctl(fd, opt, [arg]) */
82
83static object *
84fcntl_ioctl(self, args)
85 object *self; /* Not used */
86 object *args;
87{
88 int fd;
89 int code;
90 int arg;
91 int ret;
92 char *str;
93 int len;
94 char buf[1024];
95
96 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
97 if (len > sizeof buf) {
98 err_setstr(ValueError, "ioctl string arg too long");
99 return NULL;
100 }
101 memcpy(buf, str, len);
Guido van Rossum903f4871995-10-07 19:18:22 +0000102 BGN_SAVE
103 ret = ioctl(fd, code, buf);
104 END_SAVE
105 if (ret < 0) {
Guido van Rossum02975121992-08-17 08:55:12 +0000106 err_errno(IOError);
107 return NULL;
108 }
109 return newsizedstringobject(buf, len);
110 }
111
112 err_clear();
113 if (getargs(args, "(ii)", &fd, &code))
114 arg = 0;
115 else {
116 err_clear();
117 if (!getargs(args, "(iii)", &fd, &code, &arg))
118 return NULL;
119 }
Guido van Rossum903f4871995-10-07 19:18:22 +0000120 BGN_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000121 ret = ioctl(fd, code, arg);
Guido van Rossum903f4871995-10-07 19:18:22 +0000122 END_SAVE
Guido van Rossum02975121992-08-17 08:55:12 +0000123 if (ret < 0) {
124 err_errno(IOError);
125 return NULL;
126 }
127 return newintobject((long)ret);
128}
129
130
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000131/* flock(fd, operation) */
132
133static object *
134fcntl_flock(self, args)
135 object *self; /* Not used */
136 object *args;
137{
138 int fd;
139 int code;
140 int ret;
141 FILE *f;
142
143 if (!getargs(args, "(ii)", &fd, &code))
144 return NULL;
145
146 BGN_SAVE
147 ret = flock(fd, code);
148 END_SAVE
149 if (ret < 0) {
150 err_errno(IOError);
151 return NULL;
152 }
153 INCREF(None);
154 return None;
155}
156
157
158
Guido van Rossum02975121992-08-17 08:55:12 +0000159/* List of functions */
160
161static struct methodlist fcntl_methods[] = {
162 {"fcntl", fcntl_fcntl},
163 {"ioctl", fcntl_ioctl},
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000164 {"flock", fcntl_flock},
Guido van Rossum02975121992-08-17 08:55:12 +0000165 {NULL, NULL} /* sentinel */
166};
167
168
169/* Module initialisation */
170
171void
172initfcntl()
173{
174 object *m, *d;
175
176 /* Create the module and add the functions */
177 m = initmodule("fcntl", fcntl_methods);
178
179 /* Add some symbolic constants to the module */
180 d = getmoduledict(m);
181
182 /* Check for errors */
183 if (err_occurred())
184 fatal("can't initialize module fcntl");
185}