static void fdPop(FD_t fd) { if (fd == NULL || fd->nfps < 0) return; fdSetIo(fd, NULL); fdSetFp(fd, NULL); fdSetFdno(fd, -1); fd->nfps--; }
static void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno) { if (fd == NULL || fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1)) return; fd->nfps++; fdSetIo(fd, io); fdSetFp(fd, fp); fdSetFdno(fd, fdno); }
static FD_t bzdFdopen(FD_t fd, int fdno, const char * fmode) { BZFILE *bzfile = BZ2_bzdopen(fdno, fmode); if (bzfile == NULL) return NULL; fdSetFdno(fd, -1); /* XXX skip the fdio close */ fdPush(fd, bzdio, bzfile, fdno); /* Push bzdio onto stack */ return fd; }
static FD_t gzdFdopen(FD_t fd, int fdno, const char *fmode) { gzFile gzfile = gzdopen(fdno, fmode); if (gzfile == NULL) return NULL; fdSetFdno(fd, -1); /* XXX skip the fdio close */ fdPush(fd, gzdio, gzfile, fdno); /* Push gzdio onto stack */ return fd; }
FD_t fdDup(int fdno) { FD_t fd; int nfdno; if ((nfdno = dup(fdno)) < 0) return NULL; fd = fdNew(NULL); fdSetFdno(fd, nfdno); DBGIO(fd, (stderr, "==> fdDup(%d) fd %p %s\n", fdno, (fd ? fd : NULL), fdbg(fd))); return fd; }
static FD_t lzdFdopen(FD_t fd, const char * fmode) { int fdno; LZFILE *lzfile; if (fd == NULL || fmode == NULL) return NULL; fdno = fdFileno(fd); fdSetFdno(fd, -1); /* XXX skip the fdio close */ if (fdno < 0) return NULL; lzfile = lzdopen(fdno, fmode); if (lzfile == NULL) return NULL; fdPush(fd, xzdio, lzfile, fdno); return fdLink(fd); }
static int fdClose(FD_t fd) { int fdno; int rc; if (fd == NULL) return -2; fdno = fdFileno(fd); fdSetFdno(fd, -1); rc = ((fdno >= 0) ? close(fdno) : -2); fdFree(fd); return rc; }
/*@-globuse@*/ static /*@null@*/ FD_t xzdFdopen(void * cookie, const char * fmode) /*@globals fileSystem, internalState @*/ /*@modifies fileSystem, internalState @*/ { FD_t fd = c2f(cookie); int fdno = fdFileno(fd); XZFILE *xzfile; assert(fmode != NULL); fdSetFdno(fd, -1); /* XXX skip the fdio close */ if (fdno < 0) return NULL; xzfile = xzdopen(fdno, fmode); if (xzfile == NULL) return NULL; fdPush(fd, xzdio, xzfile, fdno); return fdLink(fd, "xzdFdopen"); }
static FD_t bzdFdopen(FD_t fd, const char * fmode) { int fdno; BZFILE *bzfile; if (fd == NULL || fmode == NULL) return NULL; fdno = fdFileno(fd); fdSetFdno(fd, -1); /* XXX skip the fdio close */ if (fdno < 0) return NULL; bzfile = BZ2_bzdopen(fdno, fmode); if (bzfile == NULL) return NULL; fdPush(fd, bzdio, bzfile, fdno); /* Push bzdio onto stack */ return fdLink(fd); }
static FD_t gzdFdopen(FD_t fd, const char *fmode) { int fdno; gzFile gzfile; if (fd == NULL || fmode == NULL) return NULL; fdno = fdFileno(fd); fdSetFdno(fd, -1); /* XXX skip the fdio close */ if (fdno < 0) return NULL; gzfile = gzdopen(fdno, fmode); if (gzfile == NULL) return NULL; fdPush(fd, gzdio, gzfile, fdno); /* Push gzdio onto stack */ return fdLink(fd); }
static FD_t fdOpen(const char *path, int flags, mode_t mode) { FD_t fd; int fdno; fdno = open(path, flags, mode); if (fdno < 0) return NULL; if (fcntl(fdno, F_SETFD, FD_CLOEXEC)) { (void) close(fdno); return NULL; } fd = fdNew(path); fdSetFdno(fd, fdno); fd->flags = flags; return fd; }