1 /* $Id: dba_write.c,v 1.3 2016/08/05 23:15:08 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".
26 #include <sys/endian.h>
28 #include <arpa/inet.h>
38 #include "dba_write.h"
44 dba_open(const char *fname
)
46 ofp
= fopen(fname
, "w");
47 return ofp
== NULL
? -1 : 0;
53 return fclose(ofp
) == EOF
? -1 : 0;
61 if ((pos
= ftell(ofp
)) == -1)
63 if (pos
>= INT32_MAX
) {
65 err(1, "ftell = %ld", pos
);
73 if (fseek(ofp
, pos
, SEEK_SET
) == -1)
74 err(1, "fseek(%d)", pos
);
91 dba_skip(int32_t nmemb
, int32_t sz
)
93 const int32_t out
[5] = {0, 0, 0, 0, 0};
100 for (i
= 0; i
< sz
; i
++)
101 if (nmemb
- fwrite(&out
, sizeof(out
[0]), nmemb
, ofp
))
107 dba_char_write(int c
)
109 if (putc(c
, ofp
) == EOF
)
114 dba_str_write(const char *str
)
116 if (fputs(str
, ofp
) == EOF
)
118 dba_char_write('\0');
122 dba_int_write(int32_t i
)
125 if (fwrite(&i
, sizeof(i
), 1, ofp
) != 1)