static void pipe_intrin (void) { int fds[2]; SLFile_FD_Type *f0; SLFile_FD_Type *f1; while (-1 == pipe (fds)) { if (errno == EINTR) { if (-1 != SLang_handle_interrupt ()) continue; } SLerrno_set_errno (errno); SLang_verror (SL_OS_Error, "pipe failed: %s", SLerrno_strerror(errno)); return; } f0 = SLfile_create_fd ("*pipe*", fds[0]); f1 = SLfile_create_fd ("*pipe*", fds[1]); if ((NULL != f0) && (NULL != f1)) { /* Ignore errors and allow the free_fd routines to clean up */ (void) SLfile_push_fd (f0); (void) SLfile_push_fd (f1); } SLfile_free_fd (f1); SLfile_free_fd (f0); }
static int fdtype_datatype_deref (SLtype type) { SLFile_FD_Type *f; int status; int fd; (void) type; if (-1 == SLang_pop_int (&fd)) return -1; #ifdef F_GETFL while (-1 == fcntl (fd, F_GETFL)) { if (is_interrupt (errno, 1)) continue; return SLang_push_null (); } #endif f = find_chained_fd (fd); if (f != NULL) return SLfile_push_fd (f); /* The descriptor is valid, but we have no record of what it is. So make sure * it is not automatically closed. */ if (NULL == (f = SLfile_create_fd (NULL, fd))) return -1; f->flags |= _SLFD_NO_AUTO_CLOSE; status = SLfile_push_fd (f); SLfile_free_fd (f); return status; }
static void posix_fileno (void) { FILE *fp; SLang_MMT_Type *mmt; int fd; SLFile_FD_Type *f; SLFUTURE_CONST char *name; if (-1 == SLang_pop_fileptr (&mmt, &fp)) { SLang_push_null (); return; } name = SLang_get_name_from_fileptr (mmt); fd = fileno (fp); f = SLfile_create_fd (name, fd); if (f != NULL) { /* prevent fd from being closed when it goes out of scope */ f->flags |= _SLFD_NO_AUTO_CLOSE; f->close = dummy_close; } SLang_free_mmt (mmt); if (-1 == SLfile_push_fd (f)) SLang_push_null (); SLfile_free_fd (f); }
SLFile_FD_Type *SLfile_dup_fd (SLFile_FD_Type *f0) { SLFile_FD_Type *f; int fd0, fd; if (f0 == NULL) return NULL; if (-1 == get_fd (f0, &fd0)) return NULL; if (f0->dup != NULL) return (*f0->dup)(f0->clientdata); while (-1 == (fd = dup (fd0))) { if (is_interrupt (errno, 1)) continue; return NULL; } if (NULL == (f = SLfile_create_fd (f0->name, fd))) { while ((-1 == close (fd)) && is_interrupt (errno, 1)) ; return NULL; } return f; }
static SLFile_FD_Type *socket_to_fd (Socket_Type *s) { SLFile_FD_Type *f; if (NULL == (f = SLfile_create_fd ("*socket*", s->fd))) return NULL; (void) SLfile_set_clientdata (f, free_socket_callback, (VOID_STAR)s, Socket_Type_Id); (void) SLfile_set_close_method (f, close_socket_callback); return f; }
static void posix_open (void) { char *file; int mode, flags; SLFile_FD_Type *f; switch (SLang_Num_Function_Args) { case 3: if (-1 == pop_string_int_int (&file, &flags, &mode)) { SLang_push_null (); return; } break; case 2: default: if (-1 == pop_string_int (&file, &flags)) return; mode = 0777; break; } f = SLfile_create_fd (file, -1); if (f == NULL) { SLang_free_slstring (file); SLang_push_null (); return; } SLang_free_slstring (file); while (-1 == (f->fd = open (f->name, flags, mode))) { if (is_interrupt (errno, 1)) continue; SLfile_free_fd (f); SLang_push_null (); return; } if (-1 == SLfile_push_fd (f)) SLang_push_null (); SLfile_free_fd (f); }