aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/bzip2/bunzip2.c
blob: 653ebc33e820ff9dcbce4ddb5d70e6cbe5f1f04c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "bzlib.h"

static	Biobuf	bin;
static	int	debug;
static	int	verbose;
static	char	*delfile;
static	char	*infile;
static	int	bunzipf(char *file, int stdout);
static	int	bunzip(int ofd, char *ofile, Biobuf *bin);

void
usage(void)
{
	fprint(2, "usage: bunzip2 [-cvD] [file ...]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int i, ok, stdout;

	stdout = 0;
	ARGBEGIN{
	case 'D':
		debug++;
		break;
	case 'c':
		stdout++;
		break;
	case 'v':
		verbose++;
		break;
	}ARGEND

	if(argc == 0){
		Binit(&bin, 0, OREAD);
		infile = "<stdin>";
		ok = bunzip(1, "<stdout>", &bin);
	}else{
		ok = 1;
		for(i = 0; i < argc; i++)
			ok &= bunzipf(argv[i], stdout);
	}

	exits(ok ? nil: "errors");
}

static int
bunzipf(char *file, int stdout)
{
	char ofile[64], *s;
	int ofd, ifd, ok;

	infile = file;
	ifd = open(file, OREAD);
	if(ifd < 0){
		fprint(2, "gunzip: can't open %s: %r\n", file);
		return 0;
	}

	Binit(&bin, ifd, OREAD);
	if(Bgetc(&bin) != 'B' || Bgetc(&bin) != 'Z' || Bgetc(&bin) != 'h'){
		fprint(2, "bunzip2: %s is not a bzip2 file\n", file);
		Bterm(&bin);
		close(ifd);
		return 0;
	}
	Bungetc(&bin);
	Bungetc(&bin);
	Bungetc(&bin);

	if(stdout){
		ofd = 1;
		strcpy(ofile, "<stdout>");
	}else{
		s = strrchr(file, '/');
		if(s != nil)
			s++;
		else
			s = file;
		strecpy(ofile, ofile+sizeof ofile, s);
		s = strrchr(ofile, '.');
		if(s != nil && s != ofile && strcmp(s, ".bz2") == 0)
			*s = '\0';
		else if(s != nil && (strcmp(s, ".tbz") == 0 || strcmp(s, ".tbz2") == 0))
			strcpy(s, ".tar");
		else if(strcmp(file, ofile) == 0){
			fprint(2, "bunzip2: can't overwrite %s\n", file);
			Bterm(&bin);
			close(ifd);
			return 0;
		}

		ofd = create(ofile, OWRITE, 0666);
		if(ofd < 0){
			fprint(2, "bunzip2: can't create %s: %r\n", ofile);
			Bterm(&bin);
			close(ifd);
			return 0;
		}
		delfile = ofile;
	}

	ok = bunzip(ofd, ofile, &bin);
	Bterm(&bin);
	close(ifd);
	if(!ok){
		fprint(2, "bunzip2: can't write %s: %r\n", ofile);
		if(delfile)
			remove(delfile);
	}
	delfile = nil;
	if(!stdout && ofd >= 0)
		close(ofd);
	return ok;
}

static int
bunzip(int ofd, char *ofile, Biobuf *bin)
{
	int e, n, done, onemore;
	char buf[8192];
	char obuf[8192];
	Biobuf bout;
	bz_stream strm;

	USED(ofile);

	memset(&strm, 0, sizeof strm);
	BZ2_bzDecompressInit(&strm, verbose, 0);

	strm.next_in = buf;
	strm.avail_in = 0;
	strm.next_out = obuf;
	strm.avail_out = sizeof obuf;

	done = 0;
	Binit(&bout, ofd, OWRITE);

	/*
	 * onemore is a crummy hack to go 'round the loop
	 * once after we finish, to flush the output buffer.
	 */
	onemore = 1;
	SET(e);
	do {
		if(!done && strm.avail_in < sizeof buf) {
			if(strm.avail_in)
				memmove(buf, strm.next_in, strm.avail_in);
			
			n = Bread(bin, buf+strm.avail_in, sizeof(buf)-strm.avail_in);
			if(n <= 0)
				done = 1;
			else
				strm.avail_in += n;
			strm.next_in = buf;
		}
		if(strm.avail_out < sizeof obuf) {
			Bwrite(&bout, obuf, sizeof(obuf)-strm.avail_out);
			strm.next_out = obuf;
			strm.avail_out = sizeof obuf;
		}
		if(onemore == 0)
			break;
		if(strm.avail_in == 0 && strm.avail_out == sizeof obuf)
			break;
	} while((e=BZ2_bzDecompress(&strm)) == BZ_OK || onemore--);

	if(e != BZ_STREAM_END) {
		fprint(2, "bunzip2: decompress failed\n");
		return 0;
	}

	if(BZ2_bzDecompressEnd(&strm) != BZ_OK) {
		fprint(2, "bunzip2: decompress end failed (can't happen)\n");
		return 0;
	}

	Bterm(&bout);

	return 1;
}