etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / lib / isc / unix / stdio.c
bloba48eb14d6bf654f919e1daad1f3effff736939ab
1 /* $NetBSD: stdio.c,v 1.7 2015/07/08 17:29:00 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2007, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id */
22 #include <config.h>
24 #include <errno.h>
25 #include <unistd.h>
27 #include <isc/stdio.h>
28 #include <isc/stat.h>
29 #include <isc/util.h>
31 #include "errno2result.h"
33 isc_result_t
34 isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
35 FILE *f;
37 f = fopen(filename, mode);
38 if (f == NULL)
39 return (isc__errno2result(errno));
40 *fp = f;
41 return (ISC_R_SUCCESS);
44 isc_result_t
45 isc_stdio_close(FILE *f) {
46 int r;
48 r = fclose(f);
49 if (r == 0)
50 return (ISC_R_SUCCESS);
51 else
52 return (isc__errno2result(errno));
55 isc_result_t
56 isc_stdio_seek(FILE *f, off_t offset, int whence) {
57 int r;
59 #ifdef HAVE_FSEEKO
60 r = fseeko(f, offset, whence);
61 #else
62 r = fseek(f, offset, whence);
63 #endif
64 if (r == 0)
65 return (ISC_R_SUCCESS);
66 else
67 return (isc__errno2result(errno));
70 isc_result_t
71 isc_stdio_tell(FILE *f, off_t *offsetp) {
72 off_t r;
74 REQUIRE(offsetp != NULL);
76 #ifdef HAVE_FTELLO
77 r = ftello(f);
78 #else
79 r = ftell(f);
80 #endif
81 if (r >= 0) {
82 *offsetp = r;
83 return (ISC_R_SUCCESS);
84 } else
85 return (isc__errno2result(errno));
88 isc_result_t
89 isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
90 isc_result_t result = ISC_R_SUCCESS;
91 size_t r;
93 clearerr(f);
94 r = fread(ptr, size, nmemb, f);
95 if (r != nmemb) {
96 if (feof(f))
97 result = ISC_R_EOF;
98 else
99 result = isc__errno2result(errno);
101 if (nret != NULL)
102 *nret = r;
103 return (result);
106 isc_result_t
107 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
108 size_t *nret)
110 isc_result_t result = ISC_R_SUCCESS;
111 size_t r;
113 clearerr(f);
114 r = fwrite(ptr, size, nmemb, f);
115 if (r != nmemb)
116 result = isc__errno2result(errno);
117 if (nret != NULL)
118 *nret = r;
119 return (result);
122 isc_result_t
123 isc_stdio_flush(FILE *f) {
124 int r;
126 r = fflush(f);
127 if (r == 0)
128 return (ISC_R_SUCCESS);
129 else
130 return (isc__errno2result(errno));
134 * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
136 #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
137 #define ENOTSUP EOPNOTSUPP
138 #endif
140 isc_result_t
141 isc_stdio_sync(FILE *f) {
142 int r;
144 r = fsync(fileno(f));
146 * fsync is not supported on sockets and pipes which
147 * result in EINVAL / ENOTSUP.
149 if (r == 0 || errno == EINVAL || errno == ENOTSUP)
150 return (ISC_R_SUCCESS);
151 else
152 return (isc__errno2result(errno));