summaryrefslogtreecommitdiffstats
path: root/liblog_destroy_output.c
blob: 98bb2dd6899997c76e43d2a9556c5c6465b2292a (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST

int
liblog_destroy_output(struct liblog_output *output)
{
	int saved_errno = 0; /* initialised to silence false warning */
	int ret = 0;

	if (!output)
		return 0;

	if (output->sink_type == LIBLOG_FILE && output->sink.file.owns_fd) {
		saved_errno = errno;
		ret = close(output->sink.file.fd);
		output->sink.file.fd = -1;
	} else if (output->sink_type == LIBLOG_STREAM && output->sink.stream.owns_stream) {
		saved_errno = errno;
		ret = fclose(output->sink.stream.stream);
		output->sink.stream.stream = NULL;
#if EOF != -1
		if (ret == EOF)
			ret = -1;
#endif
	}

	if (ret && errno != EINTR) {
		ret = 0;
		errno = saved_errno;
	}

	return ret;
}

#else

int main(void) {return 0;} /* TODO test */

#endif