File: ikarus-io.c

package info (click to toggle)
ikarus 0.0.3+bzr.2010.01.26-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid
  • size: 39,868 kB
  • ctags: 9,284
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 264; awk: 186; perl: 66
file content (415 lines) | stat: -rw-r--r-- 9,980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 *  Ikarus Scheme -- A compiler for R6RS Scheme.
 *  Copyright (C) 2006,2007,2008  Abdulaziz Ghuloum
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *  
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <netinet/in.h>
#include <dirent.h>
#include "ikarus-data.h"

extern ikptr ik_errno_to_code();

ikptr
ikrt_close_fd(ikptr fd /*, ikpcb* pcb */){
  int err = close(unfix(fd));
  if(err == -1){
    return ik_errno_to_code();
  } else {
    return false_object;;
  }
}

ikptr
ikrt_set_position(ikptr fd, ikptr pos /*, ikpcb* pcb */){
  off_t offset = extract_num_longlong(pos);
  off_t err = lseek(unfix(fd), offset, SEEK_SET);
  if(err == -1){
    return ik_errno_to_code();
  } else {
    return false_object;;
  }
}


ikptr
ikrt_open_input_fd(ikptr fn /*, ikpcb* pcb */){
  int fh = open((char*)(long)(fn+off_bytevector_data), O_RDONLY, 0);
  if(fh >= 0){
    return fix(fh);
  } else {
    return ik_errno_to_code();
  }
}

ikptr
ikrt_open_output_fd(ikptr fn, ikptr ikopts /*, ikpcb* pcb */){
  int opts = unfix(ikopts);
  int mode = 0;
  switch (opts){
    /* mode 0: error if exists, create if does not exist */
    case 0: mode = O_WRONLY | O_CREAT | O_EXCL; break;
    /* mode 1: truncate if exists, error if not exists */
    case 1: mode = O_WRONLY | O_TRUNC; break;
    /* mode 2: truncate if exists, create if not exist */
    case 2: mode = O_WRONLY | O_TRUNC | O_CREAT ; break;
    /* mode 3: truncate if exists, error if not exists */
    case 3: mode = O_WRONLY | O_TRUNC ; break;
    case 4: mode = O_WRONLY | O_CREAT | O_EXCL ; break;
    case 5: mode = O_WRONLY | O_CREAT ; break;
    case 6: mode = O_WRONLY | O_CREAT ; break;
    case 7: mode = O_WRONLY ; break;
  }
  int fh = open((char*)(long)(fn+off_bytevector_data), 
                mode,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if(fh >= 0){
    return fix(fh);
  } else {
    return ik_errno_to_code();
  }
}


ikptr
ikrt_read_fd(ikptr fd, ikptr bv, ikptr off, ikptr cnt /*, ikpcb* pcb */){
#if 0
  fprintf(stderr, "READ: %d\n", unfix(fd));
#endif
  ssize_t bytes = 
   read(unfix(fd),
        (char*)(long)(bv+off_bytevector_data+unfix(off)), 
        unfix(cnt));
#if 0
  fprintf(stderr, "BYTES: %d\n", bytes);
#endif
  if(bytes >= 0){
    return fix(bytes);
  } else {
    return ik_errno_to_code();
  }
}

ikptr
ikrt_write_fd(ikptr fd, ikptr bv, ikptr off, ikptr cnt /*, ikpcb* pcb */){
#if 0
  if (0) {
    fprintf(stderr, "WRITE %d, %p %d %d %d\n", 
         unfix(fd),
         bv,
         unfix(ref(bv, off_bytevector_length)),
         unfix(off),
        unfix(cnt));
    int i;
    for(i=0; i<100; i++){
      fprintf(stderr, "bv[%d]=0x%02x ", i,
             ((char*)(bv+off_bytevector_data))[i]);
    }
    fprintf(stderr, "\n");
  }
#endif
  ssize_t bytes = 
   write(unfix(fd),
         (char*)(long)(bv+off_bytevector_data+unfix(off)), 
         unfix(cnt));
  if(bytes >= 0){
    return fix(bytes);
  } else {
    return ik_errno_to_code();
  }
}



static ikptr
do_connect(ikptr host, ikptr srvc, int socket_type){
  struct addrinfo* info;
  int err = getaddrinfo((char*)(long)(host+off_bytevector_data),
                        (char*)(long)(srvc+off_bytevector_data),
                        0,
                        &info);
  if(err){
    switch(err){
      case EAI_SYSTEM: return ik_errno_to_code();
      default: return false_object;
    }
  }
  struct addrinfo* i = info;
  ikptr sock = false_object;
  while(i){
    if(i->ai_socktype != socket_type){
      i = i->ai_next;
    } else {
      int s = socket(i->ai_family, i->ai_socktype, i->ai_protocol);
      if(s < 0){
        sock = ik_errno_to_code();
        i = i->ai_next;
      } else {
        int err = connect(s, i->ai_addr, i->ai_addrlen);
        if(err < 0){
          sock = ik_errno_to_code();
          i = i->ai_next;
        } else {
          sock = fix(s);
          i = 0;
        }
      }
    }
  }
  freeaddrinfo(info);
  return sock;
}

ikptr
ikrt_tcp_connect(ikptr host, ikptr srvc /*, ikpcb* pcb */){
  return do_connect(host, srvc, SOCK_STREAM);
}

ikptr
ikrt_udp_connect(ikptr host, ikptr srvc /*, ikpcb* pcb */){
  return do_connect(host, srvc, SOCK_DGRAM);
}

ikptr 
ikrt_make_fd_nonblocking(ikptr fdptr /*, ikpcb* pcb */){
  int fd = unfix(fdptr);
  int err = fcntl(fd, F_SETFL, O_NONBLOCK);
  if(err == -1){
    return ik_errno_to_code();
  }
  return 0;
}

ikptr 
ikrt_select(ikptr fds, ikptr rfds, ikptr wfds, ikptr xfds /*, ikpcb* pcb */){
  int rv = select(unfix(fds),
                  (fd_set*)(rfds + off_bytevector_data),
                  (fd_set*)(wfds + off_bytevector_data),
                  (fd_set*)(xfds + off_bytevector_data),
                  NULL);
  if(rv < 0){
    return ik_errno_to_code();
  } 
  return fix(rv);
}

ikptr
ikrt_listen(ikptr port /*, ikpcb* pcb */){
  
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if(sock < 0){
    return ik_errno_to_code();
  }

  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(struct sockaddr_in));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(unfix(port));

  int err;

  int reuse = 1;
  err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
  if(err < 0){
    return ik_errno_to_code();
  }


  err = bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if(err < 0){
    return ik_errno_to_code();
  }

  err = listen(sock, 1024);
  if(err < 0){
    return ik_errno_to_code();
  }
  return fix(sock);
}

#if 0 
not used
ikptr 
ikrt_getsockname(ikptr s, ikpcb* pcb){
  socklen_t size = sizeof(struct sockaddr);
  ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+size))
             + bytevector_tag;
  int r = getsockname(unfix(s),
                     (struct sockaddr*)(bv+off_bytevector_data),
                     &size);
  if(r == 0){
    ref(bv, off_bytevector_length) = fix(size);
    return bv;
  } else {
    return ik_errno_to_code();
  }
}
#endif



ikptr
ikrt_accept(ikptr s, ikptr bv /*, ikpcb* pcb */){
  socklen_t addrlen = unfix(ref(bv, off_bytevector_length));
  int sock = accept(unfix(s),
                    (struct sockaddr*) (bv+off_bytevector_data),
                    &addrlen);
  if(sock < 0){
    return ik_errno_to_code();
  } 
  ref(bv, off_bytevector_length) = fix(addrlen);
  return fix(sock);
}

ikptr
ikrt_shutdown(ikptr s /*, ikpcb* pcb*/){
#ifdef __CYGWIN__
  int err = close(unfix(s));
#else
  int err = shutdown(unfix(s), SHUT_RDWR);
#endif
  if(err < 0){
    return ik_errno_to_code();
  } 
  return 0;
}


static ikptr 
timespec_bytevector(struct timespec* s, ikpcb* pcb) {
  int len = sizeof(struct timespec);
  ikptr r = ik_safe_alloc(pcb, align(disp_bytevector_data+len+3));
  ref(r, 0) = fix(len+2);
  *((char*)(r+disp_bytevector_data+0)) = sizeof(s->tv_sec);
  *((char*)(r+disp_bytevector_data+1)) = sizeof(s->tv_nsec);
  memcpy((char*)(r+disp_bytevector_data+2), s, len);
  *((char*)(r+disp_bytevector_data+len+2)) = 0;
  return r + bytevector_tag;
}


ikptr
ikrt_file_ctime2(ikptr filename, ikpcb* pcb){
  struct stat s;
  int err = stat((char*)(filename + off_bytevector_data), &s);
  if(err) {
    return ik_errno_to_code();
  }
#if HAVE_STAT_ST_CTIMESPEC
  return timespec_bytevector(&s.st_ctimespec, pcb);
#elif HAVE_STAT_ST_CTIM
  return timespec_bytevector(&s.st_ctim, pcb);
#else
  struct timespec ts;
  ts.tv_sec = s.st_ctime;
  ts.tv_nsec = 0;
  return timespec_bytevector(&ts, pcb);
#endif
}

ikptr
ikrt_file_mtime2(ikptr filename, ikpcb* pcb){
  struct stat s;
  int err = stat((char*)(filename + off_bytevector_data), &s);
  if(err) {
    return ik_errno_to_code();
  }
#if HAVE_STAT_ST_MTIMESPEC
  return timespec_bytevector(&s.st_mtimespec, pcb);
#elif HAVE_STAT_ST_MTIM
  return timespec_bytevector(&s.st_mtim, pcb);
#else
  struct timespec ts;
  ts.tv_sec = s.st_mtime;
  ts.tv_nsec = 0;
  return timespec_bytevector(&ts, pcb);
#endif
}




ikptr
ikrt_file_ctime(ikptr filename, ikptr res){
  struct stat s;
  int err = stat((char*)(filename + off_bytevector_data), &s);
  if(err) {
    return ik_errno_to_code();
  }
  
  ref(res, off_car) = fix(s.st_ctime);
  ref(res, off_cdr) = 0;
  return fix(0);
}

ikptr
ikrt_file_mtime(ikptr filename, ikptr res){
  struct stat s;
  int err = stat((char*)(filename + off_bytevector_data), &s);
  if(err) {
    return ik_errno_to_code();
  }
  
  ref(res, off_car) = fix(s.st_mtime);
  ref(res, off_cdr) = 0;
  return fix(0);
}


ikptr
ikrt_opendir(ikptr dirname, ikpcb* pcb){
  DIR* d = opendir((char*)(dirname+off_bytevector_data));
  if(d == NULL){
    return ik_errno_to_code();
  }
  return(make_pointer((long)d, pcb));
}

ikptr
ikrt_readdir(ikptr ptr, ikpcb* pcb){
  DIR* d = (DIR*) ref(ptr, off_pointer_data);
  errno = 0;
  struct dirent* ent = readdir(d);
  if (ent == NULL){
    return (errno ? ik_errno_to_code() : false_object);
  }
  int len = strlen(ent->d_name);
  ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+len+1))
             + bytevector_tag;
  ref(bv, -bytevector_tag) = fix(len);
  memcpy((char*)(bv+off_bytevector_data), ent->d_name, len+1);
  return bv;
}

ikptr
ikrt_closedir(ikptr ptr, ikpcb* pcb){
  DIR* d = (DIR*) ref(ptr, off_pointer_data);
  int rv = closedir(d);
  if (rv == -1){
    return ik_errno_to_code();
  }
  return 0;
}