1 /* $NetBSD: stdio.c,v 1.7 2015/07/08 17:29:00 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2007, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20 /* Id */
21
22 #include <config.h>
23
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include <isc/stdio.h>
28 #include <isc/stat.h>
29 #include <isc/util.h>
30
31 #include "errno2result.h"
32
33 isc_result_t
isc_stdio_open(const char * filename,const char * mode,FILE ** fp)34 isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
35 FILE *f;
36
37 f = fopen(filename, mode);
38 if (f == NULL)
39 return (isc__errno2result(errno));
40 *fp = f;
41 return (ISC_R_SUCCESS);
42 }
43
44 isc_result_t
isc_stdio_close(FILE * f)45 isc_stdio_close(FILE *f) {
46 int r;
47
48 r = fclose(f);
49 if (r == 0)
50 return (ISC_R_SUCCESS);
51 else
52 return (isc__errno2result(errno));
53 }
54
55 isc_result_t
isc_stdio_seek(FILE * f,off_t offset,int whence)56 isc_stdio_seek(FILE *f, off_t offset, int whence) {
57 int r;
58
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));
68 }
69
70 isc_result_t
isc_stdio_tell(FILE * f,off_t * offsetp)71 isc_stdio_tell(FILE *f, off_t *offsetp) {
72 off_t r;
73
74 REQUIRE(offsetp != NULL);
75
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));
86 }
87
88 isc_result_t
isc_stdio_read(void * ptr,size_t size,size_t nmemb,FILE * f,size_t * nret)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;
92
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);
100 }
101 if (nret != NULL)
102 *nret = r;
103 return (result);
104 }
105
106 isc_result_t
isc_stdio_write(const void * ptr,size_t size,size_t nmemb,FILE * f,size_t * nret)107 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
108 size_t *nret)
109 {
110 isc_result_t result = ISC_R_SUCCESS;
111 size_t r;
112
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);
120 }
121
122 isc_result_t
isc_stdio_flush(FILE * f)123 isc_stdio_flush(FILE *f) {
124 int r;
125
126 r = fflush(f);
127 if (r == 0)
128 return (ISC_R_SUCCESS);
129 else
130 return (isc__errno2result(errno));
131 }
132
133 /*
134 * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
135 */
136 #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
137 #define ENOTSUP EOPNOTSUPP
138 #endif
139
140 isc_result_t
isc_stdio_sync(FILE * f)141 isc_stdio_sync(FILE *f) {
142 int r;
143
144 r = fsync(fileno(f));
145 /*
146 * fsync is not supported on sockets and pipes which
147 * result in EINVAL / ENOTSUP.
148 */
149 if (r == 0 || errno == EINVAL || errno == ENOTSUP)
150 return (ISC_R_SUCCESS);
151 else
152 return (isc__errno2result(errno));
153 }
154
155