CAMLprim value ocaml_faad_mp4_open_read_fd(value metaonly, value fd) { CAMLparam2(metaonly, fd); CAMLlocal1(ans); mp4_t *mp = malloc(sizeof(mp4_t)); mp->fd = GET_FD(fd); mp->ff_cb.read = read_cb; mp->read_cb = 0; mp->ff_cb.write = write_cb; mp->write_cb = 0; mp->ff_cb.seek = seek_cb; mp->seek_cb = 0; mp->ff_cb.truncate = trunc_cb; mp->trunc_cb = 0; mp->ff_cb.user_data = mp; caml_enter_blocking_section(); if(Bool_val(metaonly)) mp->ff = mp4ff_open_read_metaonly(&mp->ff_cb); else mp->ff = mp4ff_open_read(&mp->ff_cb); caml_leave_blocking_section(); assert(mp->ff); ans = caml_alloc_custom(&mp4_ops, sizeof(mp4_t*), 1, 0); Mp4_val(ans) = mp; CAMLreturn(ans); }
CAMLprim value ocaml_duppy_write_ba(value _fd, value ba, value _ofs, value _len) { CAMLparam2(ba,_fd) ; int fd = GET_FD(_fd); long ofs = Long_val(_ofs); long len = Long_val(_len); void *buf = Caml_ba_data_val(ba); int ret; int written = 0; while (len > 0) { caml_enter_blocking_section(); ret = write(fd, buf+ofs, len); caml_leave_blocking_section(); if (ret == -1) { if ((errno == EAGAIN || errno == EWOULDBLOCK) && written > 0) break; uerror("write", Nothing); } written += ret; ofs += ret; len -= ret; } CAMLreturn(Val_long(written)); }
void* fdwatch_get_next_client_data( void ) { int fd; if ( next_ridx >= nreturned ) return (void*) -1; fd = GET_FD( next_ridx++ ); if ( fd < 0 || fd >= nfiles ) return (void*) 0; return fd_data[fd]; }
static int compressionfs_write(const char* path, const char* buf, size_t size, off_t offset, struct fuse_file_info *fi) { int n; n = pwrite(GET_FD(fi), buf, size, offset); if (n == -1) return -errno; return n; }
int _flsbuf(int ch, FILE *iop) /* flush (write) buffer, save ch, */ /* return EOF on failure */ { Uchar uch; do /* only loop if need to use _wrtchk() on non-_IOFBF */ { switch (iop->_flag & (_IOFBF | _IOLBF | _IONBF | _IOWRT | _IOEOF)) { case _IOFBF | _IOWRT: /* okay to do full-buffered case */ if (iop->_base != 0 && iop->_ptr > iop->_base) goto flush_putc; /* skip _wrtchk() */ break; case _IOLBF | _IOWRT: /* okay to do line-buffered case */ if (iop->_ptr >= _bufend(iop)) /* * which will recursively call * __flsbuf via putc because of no room * in the buffer for the character */ goto flush_putc; if ((*iop->_ptr++ = (unsigned char)ch) == '\n') (void) _xflsbuf(iop); iop->_cnt = 0; goto out; case _IONBF | _IOWRT: /* okay to do no-buffered case */ iop->_cnt = 0; uch = (unsigned char)ch; if (write(GET_FD(iop), (char *)&uch, 1) != 1) { if (!cancel_active()) iop->_flag |= _IOERR; return (EOF); } goto out; } if (_wrtchk(iop) != 0) /* check, correct permissions */ return (EOF); } while (iop->_flag & (_IOLBF | _IONBF)); flush_putc: (void) _xflsbuf(iop); (void) PUTC(ch, iop); /* recursive call */ out: /* necessary for putc() */ return ((iop->_flag & _IOERR) ? EOF : (unsigned char)ch); }
int setvbuf(FILE *iop, char *abuf, int type, size_t size) { Uchar *buf = (Uchar *)abuf; Uchar *temp; int sflag = iop->_flag & _IOMYBUF; rmutex_t *lk; int fd = GET_FD(iop); FLOCKFILE(lk, iop); iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF); switch (type) { /* note that the flags are the same as the possible values for type */ case _IONBF: iop->_flag |= _IONBF; /* file is unbuffered */ #ifndef _STDIO_ALLOCATE if (fd < 2) { /* use special buffer for std{in,out} */ buf = (fd == 0) ? _sibuf : _sobuf; size = BUFSIZ; } else /* needed for ifdef */ #endif if (fd < _NFILE) { buf = _smbuf[fd]; size = _SMBFSZ - PUSHBACK; } else if ((buf = malloc(_SMBFSZ * sizeof (Uchar))) != NULL) { iop->_flag |= _IOMYBUF; size = _SMBFSZ - PUSHBACK; } else { FUNLOCKFILE(lk); return (EOF); } break; case _IOLBF: case _IOFBF: iop->_flag |= type; /* buffer file */ /* * need at least an 8 character buffer for * out_of_sync concerns. */ if (size <= _SMBFSZ) { size = BUFSIZ; buf = NULL; } if (buf == NULL) { if ((buf = malloc(sizeof (Uchar) * (size + _SMBFSZ))) != NULL) iop->_flag |= _IOMYBUF; else { FUNLOCKFILE(lk); return (EOF); } } else size -= _SMBFSZ; break; default: FUNLOCKFILE(lk); return (EOF); } if (iop->_base != NULL && sflag) free((char *)iop->_base - PUSHBACK); temp = buf + PUSHBACK; iop->_base = temp; _setbufend(iop, temp + size); iop->_ptr = temp; iop->_cnt = 0; FUNLOCKFILE(lk); return (0); }
int fileno(FILE *iop) { return (GET_FD(iop)); }
#include "jni.h" #include "jni_util.h" #include "jvm.h" #include "io_util.h" #include "io_util_md.h" /* IO helper functions */ /** * 从文件中读取一个字节 */ jint readSingle(JNIEnv *env, jobject this, jfieldID fid) { jint nread; char ret; FD fd = GET_FD(this, fid); if (fd == -1) { JNU_ThrowIOException(env, "Stream Closed"); return -1; } nread = (jint)IO_Read(fd, &ret, 1); if (nread == 0) { /* EOF */ return -1; } else if (nread == JVM_IO_ERR) { /* error */ JNU_ThrowIOExceptionWithLastError(env, "Read error"); } else if (nread == JVM_IO_INTR) { JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL); } return ret & 0xFF;
/* fill buffer, return first character or EOF */ int _filbuf(FILE *iop) { ssize_t res; size_t nbyte; Uchar *endbuf; #ifdef _LP64 unsigned int flag; #else unsigned char flag; #endif if (!(iop->_flag & _IOREAD)) /* check, correct permissions */ { if (iop->_flag & _IORW) iop->_flag |= _IOREAD; /* change direction */ /* to read - fseek */ else { errno = EBADF; return (EOF); } } if (iop->_base == 0) { if ((endbuf = _findbuf(iop)) == 0) /* get buffer and */ /* end_of_buffer */ return (EOF); } else endbuf = _bufend(iop); /* * Flush all line-buffered streams before we * read no-buffered or line-buffered input. */ if (iop->_flag & (_IONBF | _IOLBF)) _flushlbf(); /* * Changed the get family fns in Solaris 10 to comply with the * 1990 C Standard and standards based upon it. If the * end-of-file indicator for the stream is set, or if the stream * is at end-of-file, the function will return EOF, and the file * position indicator for the stream will not be advanced. * Additional bytes appended to the file do not clear the EOF * indicator. */ if ((flag = iop->_flag) & _IOEOF) { if (_xpg4_check()) { /* * A previous read() has returned 0 (below), * therefore iop->_cnt was set to 0, and the EOF * indicator was set before returning EOF. Reset * iop->_cnt to 0; it has likely been changed by * a function such as getc(). */ iop->_cnt = 0; return (EOF); } } /* * Fill buffer or read 1 byte for unbuffered, handling any errors. */ iop->_ptr = iop->_base; if (flag & _IONBF) nbyte = 1; else nbyte = endbuf - iop->_base; if ((res = read(GET_FD(iop), (char *)iop->_base, nbyte)) > 0) { iop->_cnt = res - 1; return (*iop->_ptr++); } iop->_cnt = 0; if (res == 0) iop->_flag |= _IOEOF; else if (!cancel_active()) iop->_flag |= _IOERR; return (EOF); }