bclose() the fd is left open. Especially useful for buffering stdin/out.
struct obuck_header h;
byte *e;
- in = bfdopen(0, 4096);
+ in = bfdopen_shared(0, 4096);
obuck_init(1);
do
{
}
while (e);
obuck_cleanup();
- /* bclose(in) not done, we don't want fd 0 closed */
+ bclose(in);
}
static void
struct fb_file {
struct fastbuf fb;
int fd; /* File descriptor, -1 if not a real file */
- int is_temp_file; /* Is a temporary file, delete on close */
+ int is_temp_file; /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
};
#define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
struct fastbuf *bopen(byte *name, uns mode, uns buffer);
struct fastbuf *bopen_tmp(uns buffer);
struct fastbuf *bfdopen(int fd, uns buffer);
+struct fastbuf *bfdopen_shared(int fd, uns buffer);
void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
#define FB_IS_TEMP_FILE(f) FB_FILE(f)->is_temp_file
static void
bfd_close(struct fastbuf *f)
{
- close(FB_FILE(f)->fd);
- if (FB_FILE(f)->is_temp_file && unlink(f->name) < 0)
- die("unlink(%s): %m", f->name);
+ switch (FB_FILE(f)->is_temp_file)
+ {
+ case 1:
+ if (unlink(f->name) < 0)
+ log(L_ERROR, "unlink(%s): %m", f->name);
+ case 0:
+ close(FB_FILE(f)->fd);
+ }
xfree(f);
}
return bfdopen_internal(fd, buffer, x);
}
+struct fastbuf *
+bfdopen_shared(int fd, uns buffer)
+{
+ struct fastbuf *f = bfdopen(fd, buffer);
+ FB_FILE(f)->is_temp_file = -1;
+ return f;
+}
+
void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
{
uns rf = f->bstop - f->bptr;