Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0297512

Browse files
committed
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
1 parent 94390a4 commit 0297512

2 files changed

Lines changed: 593 additions & 0 deletions

File tree

Modules/fcntlmodule.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/***********************************************************
2+
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3+
Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
11+
supporting documentation, and that the names of Stichting Mathematisch
12+
Centrum or CWI not be used in advertising or publicity pertaining to
13+
distribution of the software without specific, written prior permission.
14+
15+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21+
OF 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+
33+
static object *
34+
fcntl_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);
52+
if (fcntl(fd, code, buf) < 0) {
53+
err_errno(IOError);
54+
return NULL;
55+
}
56+
return newsizedstringobject(buf, len);
57+
}
58+
59+
err_clear();
60+
if (getargs(args, "(ii)", &fd, &code))
61+
arg = 0;
62+
else {
63+
err_clear();
64+
if (!getargs(args, "(iii)", &fd, &code, &arg))
65+
return NULL;
66+
}
67+
ret = fcntl(fd, code, arg);
68+
if (ret < 0) {
69+
err_errno(IOError);
70+
return NULL;
71+
}
72+
return newintobject((long)ret);
73+
}
74+
75+
76+
/* ioctl(fd, opt, [arg]) */
77+
78+
static object *
79+
fcntl_ioctl(self, args)
80+
object *self; /* Not used */
81+
object *args;
82+
{
83+
int fd;
84+
int code;
85+
int arg;
86+
int ret;
87+
char *str;
88+
int len;
89+
char buf[1024];
90+
91+
if (getargs(args, "(iis#)", &fd, &code, &str, &len)) {
92+
if (len > sizeof buf) {
93+
err_setstr(ValueError, "ioctl string arg too long");
94+
return NULL;
95+
}
96+
memcpy(buf, str, len);
97+
if (ioctl(fd, code, buf) < 0) {
98+
err_errno(IOError);
99+
return NULL;
100+
}
101+
return newsizedstringobject(buf, len);
102+
}
103+
104+
err_clear();
105+
if (getargs(args, "(ii)", &fd, &code))
106+
arg = 0;
107+
else {
108+
err_clear();
109+
if (!getargs(args, "(iii)", &fd, &code, &arg))
110+
return NULL;
111+
}
112+
ret = ioctl(fd, code, arg);
113+
if (ret < 0) {
114+
err_errno(IOError);
115+
return NULL;
116+
}
117+
return newintobject((long)ret);
118+
}
119+
120+
121+
/* List of functions */
122+
123+
static struct methodlist fcntl_methods[] = {
124+
{"fcntl", fcntl_fcntl},
125+
{"ioctl", fcntl_ioctl},
126+
{NULL, NULL} /* sentinel */
127+
};
128+
129+
130+
/* Module initialisation */
131+
132+
void
133+
initfcntl()
134+
{
135+
object *m, *d;
136+
137+
/* Create the module and add the functions */
138+
m = initmodule("fcntl", fcntl_methods);
139+
140+
/* Add some symbolic constants to the module */
141+
d = getmoduledict(m);
142+
143+
/* Check for errors */
144+
if (err_occurred())
145+
fatal("can't initialize module fcntl");
146+
}

0 commit comments

Comments
 (0)