Debian Codegraph @

src/ikarus-io.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <netdb.h>
28 #include <string.h>
29 #include <netinet/in.h>
30 #include <dirent.h>
31 #include "ikarus-data.h"
32
33 extern ikptr ik_errno_to_code();
34
35 ikptr
36 ikrt_close_fd(ikptr fd /*, ikpcb* pcb */){
37 int err = close(unfix(fd));
38 if(err == -1){
39 return ik_errno_to_code();
40 } else {
41 return false_object;;
42 }
43 }
44
45 ikptr
46 ikrt_set_position(ikptr fd, ikptr pos /*, ikpcb* pcb */){
47 off_t offset = extract_num_longlong(pos);
48 off_t err = lseek(unfix(fd), offset, SEEK_SET);
49 if(err == -1){
50 return ik_errno_to_code();
51 } else {
52 return false_object;;
53 }
54 }
55
56
57 ikptr
58 ikrt_open_input_fd(ikptr fn /*, ikpcb* pcb */){
59 int fh = open((char*)(long)(fn+off_bytevector_data), O_RDONLY, 0);
60 if(fh >= 0){
61 return fix(fh);
62 } else {
63 return ik_errno_to_code();
64 }
65 }
66
67 ikptr
68 ikrt_open_output_fd(ikptr fn, ikptr ikopts /*, ikpcb* pcb */){
69 int opts = unfix(ikopts);
70 int mode = 0;
71 switch (opts){
72 /* mode 0: error if exists, create if does not exist */
73 case 0: mode = O_WRONLY | O_CREAT | O_EXCL; break;
74 /* mode 1: truncate if exists, error if not exists */
75 case 1: mode = O_WRONLY | O_TRUNC; break;
76 /* mode 2: truncate if exists, create if not exist */
77 case 2: mode = O_WRONLY | O_TRUNC | O_CREAT ; break;
78 /* mode 3: truncate if exists, error if not exists */
79 case 3: mode = O_WRONLY | O_TRUNC ; break;
80 case 4: mode = O_WRONLY | O_CREAT | O_EXCL ; break;
81 case 5: mode = O_WRONLY | O_CREAT ; break;
82 case 6: mode = O_WRONLY | O_CREAT ; break;
83 case 7: mode = O_WRONLY ; break;
84 }
85 int fh = open((char*)(long)(fn+off_bytevector_data),
86 mode,
87 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
88 if(fh >= 0){
89 return fix(fh);
90 } else {
91 return ik_errno_to_code();
92 }
93 }
94
95
96 ikptr
97 ikrt_read_fd(ikptr fd, ikptr bv, ikptr off, ikptr cnt /*, ikpcb* pcb */){
98 #if 0
99 fprintf(stderr, "READ: %d\n", unfix(fd));
100 #endif
101 ssize_t bytes =
102 read(unfix(fd),
103 (char*)(long)(bv+off_bytevector_data+unfix(off)),
104 unfix(cnt));
105 #if 0
106 fprintf(stderr, "BYTES: %d\n", bytes);
107 #endif
108 if(bytes >= 0){
109 return fix(bytes);
110 } else {
111 return ik_errno_to_code();
112 }
113 }
114
115 ikptr
116 ikrt_write_fd(ikptr fd, ikptr bv, ikptr off, ikptr cnt /*, ikpcb* pcb */){
117 #if 0
118 if (0) {
119 fprintf(stderr, "WRITE %d, %p %d %d %d\n",
120 unfix(fd),
121 bv,
122 unfix(ref(bv, off_bytevector_length)),
123 unfix(off),
124 unfix(cnt));
125 int i;
126 for(i=0; i<100; i++){
127 fprintf(stderr, "bv[%d]=0x%02x ", i,
128 ((char*)(bv+off_bytevector_data))[i]);
129 }
130 fprintf(stderr, "\n");
131 }
132 #endif
133 ssize_t bytes =
134 write(unfix(fd),
135 (char*)(long)(bv+off_bytevector_data+unfix(off)),
136 unfix(cnt));
137 if(bytes >= 0){
138 return fix(bytes);
139 } else {
140 return ik_errno_to_code();
141 }
142 }
143
144
145
146 static ikptr
147 do_connect(ikptr host, ikptr srvc, int socket_type){
148 struct addrinfo* info;
149 int err = getaddrinfo((char*)(long)(host+off_bytevector_data),
150 (char*)(long)(srvc+off_bytevector_data),
151 0,
152 &info);
153 if(err){
154 switch(err){
155 case EAI_SYSTEM: return ik_errno_to_code();
156 default: return false_object;
157 }
158 }
159 struct addrinfo* i = info;
160 ikptr sock = false_object;
161 while(i){
162 if(i->ai_socktype != socket_type){
163 i = i->ai_next;
164 } else {
165 int s = socket(i->ai_family, i->ai_socktype, i->ai_protocol);
166 if(s < 0){
167 sock = ik_errno_to_code();
168 i = i->ai_next;
169 } else {
170 int err = connect(s, i->ai_addr, i->ai_addrlen);
171 if(err < 0){
172 sock = ik_errno_to_code();
173 i = i->ai_next;
174 } else {
175 sock = fix(s);
176 i = 0;
177 }
178 }
179 }
180 }
181 freeaddrinfo(info);
182 return sock;
183 }
184
185 ikptr
186 ikrt_tcp_connect(ikptr host, ikptr srvc /*, ikpcb* pcb */){
187 return do_connect(host, srvc, SOCK_STREAM);
188 }
189
190 ikptr
191 ikrt_udp_connect(ikptr host, ikptr srvc /*, ikpcb* pcb */){
192 return do_connect(host, srvc, SOCK_DGRAM);
193 }
194
195 ikptr
196 ikrt_make_fd_nonblocking(ikptr fdptr /*, ikpcb* pcb */){
197 int fd = unfix(fdptr);
198 int err = fcntl(fd, F_SETFL, O_NONBLOCK);
199 if(err == -1){
200 return ik_errno_to_code();
201 }
202 return 0;
203 }
204
205 ikptr
206 ikrt_select(ikptr fds, ikptr rfds, ikptr wfds, ikptr xfds /*, ikpcb* pcb */){
207 int rv = select(unfix(fds),
208 (fd_set*)(rfds + off_bytevector_data),
209 (fd_set*)(wfds + off_bytevector_data),
210 (fd_set*)(xfds + off_bytevector_data),
211 NULL);
212 if(rv < 0){
213 return ik_errno_to_code();
214 }
215 return fix(rv);
216 }
217
218 ikptr
219 ikrt_listen(ikptr port /*, ikpcb* pcb */){
220
221 int sock = socket(AF_INET, SOCK_STREAM, 0);
222 if(sock < 0){
223 return ik_errno_to_code();
224 }
225
226 struct sockaddr_in servaddr;
227 memset(&servaddr, 0, sizeof(struct sockaddr_in));
228 servaddr.sin_family = AF_INET;
229 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
230 servaddr.sin_port = htons(unfix(port));
231
232 int err;
233
234 int reuse = 1;
235 err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
236 if(err < 0){
237 return ik_errno_to_code();
238 }
239
240
241 err = bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
242 if(err < 0){
243 return ik_errno_to_code();
244 }
245
246 err = listen(sock, 1024);
247 if(err < 0){
248 return ik_errno_to_code();
249 }
250 return fix(sock);
251 }
252
253 #if 0
254 not used
255 ikptr
256 ikrt_getsockname(ikptr s, ikpcb* pcb){
257 socklen_t size = sizeof(struct sockaddr);
258 ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+size))
259 + bytevector_tag;
260 int r = getsockname(unfix(s),
261 (struct sockaddr*)(bv+off_bytevector_data),
262 &size);
263 if(r == 0){
264 ref(bv, off_bytevector_length) = fix(size);
265 return bv;
266 } else {
267 return ik_errno_to_code();
268 }
269 }
270 #endif
271
272
273
274 ikptr
275 ikrt_accept(ikptr s, ikptr bv /*, ikpcb* pcb */){
276 socklen_t addrlen = unfix(ref(bv, off_bytevector_length));
277 int sock = accept(unfix(s),
278 (struct sockaddr*) (bv+off_bytevector_data),
279 &addrlen);
280 if(sock < 0){
281 return ik_errno_to_code();
282 }
283 ref(bv, off_bytevector_length) = fix(addrlen);
284 return fix(sock);
285 }
286
287 ikptr
288 ikrt_shutdown(ikptr s /*, ikpcb* pcb*/){
289 #ifdef __CYGWIN__
290 int err = close(unfix(s));
291 #else
292 int err = shutdown(unfix(s), SHUT_RDWR);
293 #endif
294 if(err < 0){
295 return ik_errno_to_code();
296 }
297 return 0;
298 }
299
300
301 static ikptr
302 timespec_bytevector(struct timespec* s, ikpcb* pcb) {
303 int len = sizeof(struct timespec);
304 ikptr r = ik_safe_alloc(pcb, align(disp_bytevector_data+len+3));
305 ref(r, 0) = fix(len+2);
306 *((char*)(r+disp_bytevector_data+0)) = sizeof(s->tv_sec);
307 *((char*)(r+disp_bytevector_data+1)) = sizeof(s->tv_nsec);
308 memcpy((char*)(r+disp_bytevector_data+2), s, len);
309 *((char*)(r+disp_bytevector_data+len+2)) = 0;
310 return r + bytevector_tag;
311 }
312
313
314 ikptr
315 ikrt_file_ctime2(ikptr filename, ikpcb* pcb){
316 struct stat s;
317 int err = stat((char*)(filename + off_bytevector_data), &s);
318 if(err) {
319 return ik_errno_to_code();
320 }
321 #if HAVE_STAT_ST_CTIMESPEC
322 return timespec_bytevector(&s.st_ctimespec, pcb);
323 #elif HAVE_STAT_ST_CTIM
324 return timespec_bytevector(&s.st_ctim, pcb);
325 #else
326 struct timespec ts;
327 ts.tv_sec = s.st_ctime;
328 ts.tv_nsec = 0;
329 return timespec_bytevector(&ts, pcb);
330 #endif
331 }
332
333 ikptr
334 ikrt_file_mtime2(ikptr filename, ikpcb* pcb){
335 struct stat s;
336 int err = stat((char*)(filename + off_bytevector_data), &s);
337 if(err) {
338 return ik_errno_to_code();
339 }
340 #if HAVE_STAT_ST_MTIMESPEC
341 return timespec_bytevector(&s.st_mtimespec, pcb);
342 #elif HAVE_STAT_ST_MTIM
343 return timespec_bytevector(&s.st_mtim, pcb);
344 #else
345 struct timespec ts;
346 ts.tv_sec = s.st_mtime;
347 ts.tv_nsec = 0;
348 return timespec_bytevector(&ts, pcb);
349 #endif
350 }
351
352
353
354
355 ikptr
356 ikrt_file_ctime(ikptr filename, ikptr res){
357 struct stat s;
358 int err = stat((char*)(filename + off_bytevector_data), &s);
359 if(err) {
360 return ik_errno_to_code();
361 }
362
363 ref(res, off_car) = fix(s.st_ctime);
364 ref(res, off_cdr) = 0;
365 return fix(0);
366 }
367
368 ikptr
369 ikrt_file_mtime(ikptr filename, ikptr res){
370 struct stat s;
371 int err = stat((char*)(filename + off_bytevector_data), &s);
372 if(err) {
373 return ik_errno_to_code();
374 }
375
376 ref(res, off_car) = fix(s.st_mtime);
377 ref(res, off_cdr) = 0;
378 return fix(0);
379 }
380
381
382 ikptr
383 ikrt_opendir(ikptr dirname, ikpcb* pcb){
384 DIR* d = opendir((char*)(dirname+off_bytevector_data));
385 if(d == NULL){
386 return ik_errno_to_code();
387 }
388 return(make_pointer((long)d, pcb));
389 }
390
391 ikptr
392 ikrt_readdir(ikptr ptr, ikpcb* pcb){
393 DIR* d = (DIR*) ref(ptr, off_pointer_data);
394 errno = 0;
395 struct dirent* ent = readdir(d);
396 if (ent == NULL){
397 return (errno ? ik_errno_to_code() : false_object);
398 }
399 int len = strlen(ent->d_name);
400 ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+len+1))
401 + bytevector_tag;
402 ref(bv, -bytevector_tag) = fix(len);
403 memcpy((char*)(bv+off_bytevector_data), ent->d_name, len+1);
404 return bv;
405 }
406
407 ikptr
408 ikrt_closedir(ikptr ptr, ikpcb* pcb){
409 DIR* d = (DIR*) ref(ptr, off_pointer_data);
410 int rv = closedir(d);
411 if (rv == -1){
412 return ik_errno_to_code();
413 }
414 return 0;
415 }