GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/dba_write.c Lines: 28 36 77.8 %
Date: 2017-11-13 Branches: 15 26 57.7 %

Line Branch Exec Source
1
/*	$OpenBSD: dba_write.c,v 1.1 2016/08/01 10:32:39 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 *
17
 * Low-level functions for serializing allocation-based data to disk.
18
 * The interface is defined in "dba_write.h".
19
 */
20
#include <assert.h>
21
#include <endian.h>
22
#include <err.h>
23
#include <errno.h>
24
#include <fcntl.h>
25
#include <stdint.h>
26
#include <stdio.h>
27
28
#include "dba_write.h"
29
30
static FILE	*ofp;
31
32
33
int
34
dba_open(const char *fname)
35
{
36
70
	ofp = fopen(fname, "w");
37
35
	return ofp == NULL ? -1 : 0;
38
}
39
40
int
41
dba_close(void)
42
{
43
70
	return fclose(ofp) == EOF ? -1 : 0;
44
}
45
46
int32_t
47
dba_tell(void)
48
{
49
	long		 pos;
50
51
220696
	if ((pos = ftell(ofp)) == -1)
52
		err(1, "ftell");
53
110348
	if (pos >= INT32_MAX) {
54
		errno = EOVERFLOW;
55
		err(1, "ftell = %ld", pos);
56
	}
57
110348
	return pos;
58
}
59
60
void
61
dba_seek(int32_t pos)
62
{
63
5390
	if (fseek(ofp, pos, SEEK_SET) == -1)
64
		err(1, "fseek(%d)", pos);
65
2695
}
66
67
int32_t
68
dba_align(void)
69
{
70
	int32_t		 pos;
71
72
2590
	pos = dba_tell();
73
2836
	while (pos & 3) {
74
123
		dba_char_write('\0');
75
123
		pos++;
76
	}
77
1295
	return pos;
78
}
79
80
int32_t
81
dba_skip(int32_t nmemb, int32_t sz)
82
{
83
	const int32_t	 out[5] = {0, 0, 0, 0, 0};
84
	int32_t		 i, pos;
85
86
2730
	assert(sz >= 0);
87
1365
	assert(nmemb > 0);
88
1365
	assert(nmemb <= 5);
89
1365
	pos = dba_tell();
90
98520
	for (i = 0; i < sz; i++)
91
47895
		if (nmemb - fwrite(&out, sizeof(out[0]), nmemb, ofp))
92
			err(1, "fwrite");
93
1365
	return pos;
94
}
95
96
void
97
dba_char_write(int c)
98
{
99

303952
	if (putc(c, ofp) == EOF)
100
		err(1, "fputc");
101
75988
}
102
103
void
104
dba_str_write(const char *str)
105
{
106
127934
	if (fputs(str, ofp) == EOF)
107
		err(1, "fputs");
108
63967
	dba_char_write('\0');
109
63967
}
110
111
void
112
dba_int_write(int32_t i)
113
{
114
483364
	i = htobe32(i);
115
241682
	if (fwrite(&i, sizeof(i), 1, ofp) != 1)
116
		err(1, "fwrite");
117
241682
}