stdio.c revision 1.3 1 /* $NetBSD: stdio.c,v 1.3 2025/01/26 16:25:38 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 #include <errno.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19
20 #include <isc/stdio.h>
21 #include <isc/util.h>
22
23 #include "errno2result.h"
24
25 isc_result_t
26 isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
27 FILE *f;
28
29 f = fopen(filename, mode);
30 if (f == NULL) {
31 return isc__errno2result(errno);
32 }
33 *fp = f;
34 return ISC_R_SUCCESS;
35 }
36
37 isc_result_t
38 isc_stdio_close(FILE *f) {
39 int r;
40
41 r = fclose(f);
42 if (r == 0) {
43 return ISC_R_SUCCESS;
44 } else {
45 return isc__errno2result(errno);
46 }
47 }
48
49 isc_result_t
50 isc_stdio_seek(FILE *f, off_t offset, int whence) {
51 int r;
52
53 r = fseeko(f, offset, whence);
54 if (r == 0) {
55 return ISC_R_SUCCESS;
56 } else {
57 return isc__errno2result(errno);
58 }
59 }
60
61 isc_result_t
62 isc_stdio_tell(FILE *f, off_t *offsetp) {
63 off_t r;
64
65 REQUIRE(offsetp != NULL);
66
67 r = ftello(f);
68 if (r >= 0) {
69 *offsetp = r;
70 return ISC_R_SUCCESS;
71 } else {
72 return isc__errno2result(errno);
73 }
74 }
75
76 isc_result_t
77 isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
78 isc_result_t result = ISC_R_SUCCESS;
79 size_t r;
80
81 clearerr(f);
82 r = fread(ptr, size, nmemb, f);
83 if (r != nmemb) {
84 if (feof(f)) {
85 result = ISC_R_EOF;
86 } else {
87 result = isc__errno2result(errno);
88 }
89 }
90 SET_IF_NOT_NULL(nret, r);
91 return result;
92 }
93
94 isc_result_t
95 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
96 size_t *nret) {
97 isc_result_t result = ISC_R_SUCCESS;
98 size_t r;
99
100 clearerr(f);
101 r = fwrite(ptr, size, nmemb, f);
102 if (r != nmemb) {
103 result = isc__errno2result(errno);
104 }
105 SET_IF_NOT_NULL(nret, r);
106 return result;
107 }
108
109 isc_result_t
110 isc_stdio_flush(FILE *f) {
111 int r;
112
113 r = fflush(f);
114 if (r == 0) {
115 return ISC_R_SUCCESS;
116 } else {
117 return isc__errno2result(errno);
118 }
119 }
120
121 /*
122 * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
123 */
124 #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
125 #define ENOTSUP EOPNOTSUPP
126 #endif /* if defined(EOPNOTSUPP) && !defined(ENOTSUP) */
127
128 isc_result_t
129 isc_stdio_sync(FILE *f) {
130 struct stat buf;
131 int r;
132
133 if (fstat(fileno(f), &buf) != 0) {
134 return isc__errno2result(errno);
135 }
136
137 /*
138 * Only call fsync() on regular files.
139 */
140 if ((buf.st_mode & S_IFMT) != S_IFREG) {
141 return ISC_R_SUCCESS;
142 }
143
144 r = fsync(fileno(f));
145 if (r == 0) {
146 return ISC_R_SUCCESS;
147 } else {
148 return isc__errno2result(errno);
149 }
150 }
151