summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorThorsten Lockert <tholo@cvs.openbsd.org>1996-09-22 20:05:20 +0000
committerThorsten Lockert <tholo@cvs.openbsd.org>1996-09-22 20:05:20 +0000
commitb94f37f55f7022871e1921d7578e3a9b689a522a (patch)
tree309980d8a91bbb2e260c5bff79eaaee9e1a8f648 /usr.bin
parent99b0cb76e2303dd1d20a32461a8ab6aa1b70e0aa (diff)
Add zdopen(); open a file descriptor
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/compress/zopen.315
-rw-r--r--usr.bin/compress/zopen.c54
2 files changed, 65 insertions, 4 deletions
diff --git a/usr.bin/compress/zopen.3 b/usr.bin/compress/zopen.3
index 7feb5b367b3..a8f95941bf4 100644
--- a/usr.bin/compress/zopen.3
+++ b/usr.bin/compress/zopen.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: zopen.3,v 1.2 1996/06/26 05:32:21 deraadt Exp $
+.\" $OpenBSD: zopen.3,v 1.3 1996/09/22 20:05:18 tholo Exp $
.\" $NetBSD: zopen.3,v 1.3 1995/03/26 09:44:49 glass Exp $
.\"
.\" Copyright (c) 1992, 1993
@@ -44,6 +44,8 @@
.Fd #include <stdio.h>
.Ft FILE *
.Fn zopen "const char *path" "const char *mode" "int bits"
+.Ft FILE *
+.Fn zdopen "int fd" "const char *mode" "int bits"
.Sh DESCRIPTION
The
.Fn zopen
@@ -52,6 +54,13 @@ opens the compressed file whose name is the string pointed to by
.Fa path
and associates a stream with it.
.Pp
+The
+.Fn zdopen
+function
+opens the compressed file whose file descriptor is given in
+.Fa fd
+and associates a stream with it.
+.Pp
The argument
.Fa mode
points to one of the following one-character strings:
@@ -137,6 +146,8 @@ first appeared in 4.4BSD.
.Sh BUGS
The
.Fn zopen
-function
+and
+.Fn zdopen
+functions
may not be portable to systems other than
.Bx .
diff --git a/usr.bin/compress/zopen.c b/usr.bin/compress/zopen.c
index 14090d44ec4..dcf742c78d1 100644
--- a/usr.bin/compress/zopen.c
+++ b/usr.bin/compress/zopen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: zopen.c,v 1.2 1996/06/26 05:32:22 deraadt Exp $ */
+/* $OpenBSD: zopen.c,v 1.3 1996/09/22 20:05:19 tholo Exp $ */
/* $NetBSD: zopen.c,v 1.5 1995/03/26 09:44:53 glass Exp $ */
/*-
@@ -42,7 +42,7 @@
#if 0
static char sccsid[] = "@(#)zopen.c 8.1 (Berkeley) 6/27/93";
#else
-static char rcsid[] = "$OpenBSD: zopen.c,v 1.2 1996/06/26 05:32:22 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: zopen.c,v 1.3 1996/09/22 20:05:19 tholo Exp $";
#endif
#endif /* LIBC_SCCS and not lint */
@@ -698,6 +698,56 @@ cl_hash(zs, cl_hsize) /* Reset code table. */
}
FILE *
+zdopen(fd, mode, bits)
+ int fd;
+ const char *mode;
+ int bits;
+{
+ struct s_zstate *zs;
+
+ if (mode[0] != 'r' && mode[0] != 'w' || mode[1] != '\0' ||
+ bits < 0 || bits > BITS) {
+ errno = EINVAL;
+ return (NULL);
+ }
+
+ if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
+ return (NULL);
+
+ maxbits = bits ? bits : BITS; /* User settable max # bits/code. */
+ maxmaxcode = 1 << maxbits; /* Should NEVER generate this code. */
+ hsize = HSIZE; /* For dynamic table sizing. */
+ free_ent = 0; /* First unused entry. */
+ block_compress = BLOCK_MASK;
+ clear_flg = 0;
+ ratio = 0;
+ checkpoint = CHECK_GAP;
+ in_count = 1; /* Length of input. */
+ out_count = 0; /* # of codes output (for debugging). */
+ state = S_START;
+ roffset = 0;
+ size = 0;
+
+ /*
+ * Layering compress on top of stdio in order to provide buffering,
+ * and ensure that reads and write work with the data specified.
+ */
+ if ((fp = fdopen(fd, mode)) == NULL) {
+ free(zs);
+ return (NULL);
+ }
+ switch (*mode) {
+ case 'r':
+ zmode = 'r';
+ return (funopen(zs, zread, NULL, NULL, zclose));
+ case 'w':
+ zmode = 'w';
+ return (funopen(zs, NULL, zwrite, NULL, zclose));
+ }
+ /* NOTREACHED */
+}
+
+FILE *
zopen(fname, mode, bits)
const char *fname, *mode;
int bits;