#include "gzip.h"
-static void
-to_devnull(int fd)
+static void to_devnull(int fd)
{
int devnull = open("/dev/null", fd ? O_WRONLY : O_RDONLY);
close(devnull);
}
-void *
-gzip_thread(void *ptr)
+void *gzip_thread(void *ptr)
{
struct gzip_handle *zh = ptr;
char buf[4096];
zh->wfd = -1;
}
-int
-gzip_exec(struct gzip_handle *zh, const char *filename)
+int gzip_exec(struct gzip_handle *zh, const char *filename)
{
- int rpipe[2] = { -1, -1 }, wpipe[2] = { -1, -1 };
- struct sigaction pipe_sa = { .sa_handler = SIG_IGN };
+ int rpipe[2] = { -1, -1 }, wpipe[2] = {
+ -1, -1};
+ struct sigaction pipe_sa = {.sa_handler = SIG_IGN };
zh->rfd = -1;
zh->wfd = -1;
zh->pid = vfork();
switch (zh->pid) {
- case -1:
- return -1;
-
- case 0:
- to_devnull(STDERR_FILENO);
-
- if (filename) {
- to_devnull(STDIN_FILENO);
- }
- else {
- dup2(wpipe[0], STDIN_FILENO);
- close(wpipe[0]);
- close(wpipe[1]);
- }
-
- dup2(rpipe[1], STDOUT_FILENO);
- close(rpipe[0]);
- close(rpipe[1]);
-
- execlp("gzip", "gzip", "-d", "-c", filename, NULL);
- exit(-1);
-
- default:
- zh->rfd = rpipe[0];
- zh->wfd = wpipe[1];
-
- fcntl(zh->rfd, F_SETFD, fcntl(zh->rfd, F_GETFD) | FD_CLOEXEC);
- close(rpipe[1]);
-
- if (zh->wfd >= 0) {
- fcntl(zh->wfd, F_SETFD, fcntl(zh->wfd, F_GETFD) | FD_CLOEXEC);
- close(wpipe[0]);
- pthread_create(&zh->thread, NULL, gzip_thread, zh);
- }
+ case -1:
+ return -1;
+
+ case 0:
+ to_devnull(STDERR_FILENO);
+
+ if (filename) {
+ to_devnull(STDIN_FILENO);
+ } else {
+ dup2(wpipe[0], STDIN_FILENO);
+ close(wpipe[0]);
+ close(wpipe[1]);
+ }
+
+ dup2(rpipe[1], STDOUT_FILENO);
+ close(rpipe[0]);
+ close(rpipe[1]);
+
+ execlp("gzip", "gzip", "-d", "-c", filename, NULL);
+ exit(-1);
+
+ default:
+ zh->rfd = rpipe[0];
+ zh->wfd = wpipe[1];
+
+ fcntl(zh->rfd, F_SETFD, fcntl(zh->rfd, F_GETFD) | FD_CLOEXEC);
+ close(rpipe[1]);
+
+ if (zh->wfd >= 0) {
+ fcntl(zh->wfd, F_SETFD,
+ fcntl(zh->wfd, F_GETFD) | FD_CLOEXEC);
+ close(wpipe[0]);
+ pthread_create(&zh->thread, NULL, gzip_thread, zh);
+ }
}
return 0;
}
-ssize_t
-gzip_read(struct gzip_handle *zh, char *buf, ssize_t len)
+ssize_t gzip_read(struct gzip_handle * zh, char *buf, ssize_t len)
{
ssize_t ret;
return ret;
}
-ssize_t
-gzip_copy(struct gzip_handle *zh, FILE *out, ssize_t len)
+ssize_t gzip_copy(struct gzip_handle * zh, FILE * out, ssize_t len)
{
char buf[4096];
ssize_t rlen, total = 0;
while (len > 0) {
rlen = gzip_read(zh, buf,
- (len > sizeof(buf)) ? sizeof(buf) : len);
+ (len > sizeof(buf)) ? sizeof(buf) : len);
if (rlen <= 0)
break;
return total;
}
-FILE *
-gzip_fdopen(struct gzip_handle *zh, const char *filename)
+FILE *gzip_fdopen(struct gzip_handle * zh, const char *filename)
{
memset(zh, 0, sizeof(*zh));
return fdopen(zh->rfd, "r");
}
-int
-gzip_close(struct gzip_handle *zh)
+int gzip_close(struct gzip_handle *zh)
{
int code = -1;