blob: 229958532da05ed497ba9f17c29a226e793b9c16 [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
131/* List of functions */
132
133static struct methodlist fcntl_methods[] = {
134 {"fcntl", fcntl_fcntl},
135 {"ioctl", fcntl_ioctl},
136 {NULL, NULL} /* sentinel */
137};
138
139
140/* Module initialisation */
141
142void
143initfcntl()
144{
145 object *m, *d;
146
147 /* Create the module and add the functions */
148 m = initmodule("fcntl", fcntl_methods);
149
150 /* Add some symbolic constants to the module */
151 d = getmoduledict(m);
152
153 /* Check for errors */
154 if (err_occurred())
155 fatal("can't initialize module fcntl");
156}