GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/compress/nullopen.c Lines: 0 54 0.0 %
Date: 2017-11-07 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: nullopen.c,v 1.6 2016/09/03 11:41:10 tedu Exp $	*/
2
3
/*
4
 * Copyright (c) 2003 Can Erkin Acar
5
 * Copyright (c) 1997 Michael Shalayeff
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
#include <sys/types.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <errno.h>
35
#include <unistd.h>
36
#include "compress.h"
37
38
typedef struct {
39
	off_t 	  total_in;
40
	off_t 	  total_out;
41
	int   	  fd;
42
	int       gotmagic;
43
	char	  mode;
44
} null_stream;
45
46
char null_magic[2];
47
48
49
void *
50
null_ropen(int fd, char *name, int gotmagic)
51
{
52
	null_stream *s;
53
54
	if (fd < 0)
55
		return NULL;
56
57
	if ((s = calloc(1, sizeof(null_stream))) == NULL)
58
		return NULL;
59
60
	s->fd = fd;
61
	s->gotmagic = gotmagic;
62
	s->total_in = s->total_out = 0;
63
	s->mode = 'r';
64
65
	return s;
66
}
67
68
void *
69
null_wopen(int fd, char *name, int bits, u_int32_t mtime)
70
{
71
	null_stream *s;
72
73
	if (fd < 0)
74
		return NULL;
75
76
	if ((s = calloc(1, sizeof(null_stream))) == NULL)
77
		return NULL;
78
79
	s->fd = fd;
80
	s->gotmagic = 0;
81
	s->total_in = s->total_out = 0;
82
	s->mode = 'w';
83
84
	return s;
85
}
86
87
int
88
null_close(void *cookie, struct z_info *info, const char *name, struct stat *sb)
89
{
90
	null_stream *s = (null_stream*) cookie;
91
	int err = 0;
92
93
	if (s == NULL)
94
		return -1;
95
96
97
	if (info != NULL) {
98
		info->mtime = 0;
99
		info->crc =  (u_int32_t)-1;
100
		info->hlen = 0;
101
		info->total_in = (off_t) s->total_in;
102
		info->total_out = (off_t) s->total_out;
103
	}
104
105
	setfile(name, s->fd, sb);
106
	err = close(s->fd);
107
108
	free(s);
109
110
	return err;
111
}
112
113
int
114
null_flush(void *cookie, int flush)
115
{
116
	null_stream *s = (null_stream*)cookie;
117
118
	if (s == NULL || s->mode != 'w') {
119
		errno = EBADF;
120
		return (-1);
121
	}
122
123
	return 0;
124
}
125
126
int
127
null_read(void *cookie, char *buf, int len)
128
{
129
	null_stream *s = (null_stream*)cookie;
130
131
	if (s == NULL) {
132
		errno = EBADF;
133
		return (-1);
134
	}
135
	if (s->gotmagic) {
136
		if (len < 2) {
137
			errno = EBADF;
138
			return (-1);
139
		}
140
141
		buf[0] = null_magic[0];
142
		buf[1] = null_magic[1];
143
		s->gotmagic = 0;
144
145
		return (2);
146
	}
147
148
	return read(s->fd, buf, len);
149
}
150
151
int
152
null_write(void *cookie, const char *buf, int len)
153
{
154
	null_stream *s = (null_stream*)cookie;
155
156
	if (s == NULL) {
157
		errno = EBADF;
158
		return (-1);
159
	}
160
161
	return write(s->fd, buf, len);
162
}