]>
git.cameronkatri.com Git - mandoc.git/blob - dba_write.c
1 /* $Id: dba_write.c,v 1.2 2016/07/20 00:23:14 schwarze Exp $ */
3 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 * Low-level functions for serializing allocation-based data to disk.
18 * The interface is defined in "dba_write.h".
32 #include "dba_write.h"
38 dba_open(const char *fname
)
40 ofp
= fopen(fname
, "w");
41 return ofp
== NULL
? -1 : 0;
47 return fclose(ofp
) == EOF
? -1 : 0;
55 if ((pos
= ftell(ofp
)) == -1)
57 if (pos
>= INT32_MAX
) {
59 err(1, "ftell = %ld", pos
);
67 if (fseek(ofp
, pos
, SEEK_SET
) == -1)
68 err(1, "fseek(%d)", pos
);
85 dba_skip(int32_t nmemb
, int32_t sz
)
87 const int32_t out
[5] = {0, 0, 0, 0, 0};
94 for (i
= 0; i
< sz
; i
++)
95 if (nmemb
- fwrite(&out
, sizeof(out
[0]), nmemb
, ofp
))
101 dba_char_write(int c
)
103 if (putc(c
, ofp
) == EOF
)
108 dba_str_write(const char *str
)
110 if (fputs(str
, ofp
) == EOF
)
112 dba_char_write('\0');
116 dba_int_write(int32_t i
)
119 if (fwrite(&i
, sizeof(i
), 1, ofp
) != 1)