xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/unix/stdio.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: stdio.c,v 1.2 2024/08/18 20:47:16 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 2000, 2001  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id */
21897be3a4Schristos 
22897be3a4Schristos #include <config.h>
23897be3a4Schristos 
24897be3a4Schristos #include <errno.h>
25897be3a4Schristos #include <unistd.h>
26897be3a4Schristos 
27897be3a4Schristos #include <isc/stdio.h>
28897be3a4Schristos #include <isc/stat.h>
29897be3a4Schristos 
30897be3a4Schristos #include "errno2result.h"
31897be3a4Schristos 
32897be3a4Schristos isc_result_t
33897be3a4Schristos isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
34897be3a4Schristos 	FILE *f;
35897be3a4Schristos 
36897be3a4Schristos 	f = fopen(filename, mode);
37897be3a4Schristos 	if (f == NULL)
38897be3a4Schristos 		return (isc__errno2result(errno));
39897be3a4Schristos 	*fp = f;
40897be3a4Schristos 	return (ISC_R_SUCCESS);
41897be3a4Schristos }
42897be3a4Schristos 
43897be3a4Schristos isc_result_t
44897be3a4Schristos isc_stdio_close(FILE *f) {
45897be3a4Schristos 	int r;
46897be3a4Schristos 
47897be3a4Schristos 	r = fclose(f);
48897be3a4Schristos 	if (r == 0)
49897be3a4Schristos 		return (ISC_R_SUCCESS);
50897be3a4Schristos 	else
51897be3a4Schristos 		return (isc__errno2result(errno));
52897be3a4Schristos }
53897be3a4Schristos 
54897be3a4Schristos isc_result_t
55897be3a4Schristos isc_stdio_seek(FILE *f, long offset, int whence) {
56897be3a4Schristos 	int r;
57897be3a4Schristos 
58897be3a4Schristos 	r = fseek(f, offset, whence);
59897be3a4Schristos 	if (r == 0)
60897be3a4Schristos 		return (ISC_R_SUCCESS);
61897be3a4Schristos 	else
62897be3a4Schristos 		return (isc__errno2result(errno));
63897be3a4Schristos }
64897be3a4Schristos 
65897be3a4Schristos isc_result_t
66897be3a4Schristos isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
67897be3a4Schristos 	isc_result_t result = ISC_R_SUCCESS;
68897be3a4Schristos 	size_t r;
69897be3a4Schristos 
70897be3a4Schristos 	clearerr(f);
71897be3a4Schristos 	r = fread(ptr, size, nmemb, f);
72897be3a4Schristos 	if (r != nmemb) {
73897be3a4Schristos 		if (feof(f))
74897be3a4Schristos 			result = ISC_R_EOF;
75897be3a4Schristos 		else
76897be3a4Schristos 			result = isc__errno2result(errno);
77897be3a4Schristos 	}
78897be3a4Schristos 	if (nret != NULL)
79897be3a4Schristos 		*nret = r;
80897be3a4Schristos 	return (result);
81897be3a4Schristos }
82897be3a4Schristos 
83897be3a4Schristos isc_result_t
84897be3a4Schristos isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
85897be3a4Schristos 	       size_t *nret)
86897be3a4Schristos {
87897be3a4Schristos 	isc_result_t result = ISC_R_SUCCESS;
88897be3a4Schristos 	size_t r;
89897be3a4Schristos 
90897be3a4Schristos 	clearerr(f);
91897be3a4Schristos 	r = fwrite(ptr, size, nmemb, f);
92897be3a4Schristos 	if (r != nmemb)
93897be3a4Schristos 		result = isc__errno2result(errno);
94897be3a4Schristos 	if (nret != NULL)
95897be3a4Schristos 		*nret = r;
96897be3a4Schristos 	return (result);
97897be3a4Schristos }
98897be3a4Schristos 
99897be3a4Schristos isc_result_t
100897be3a4Schristos isc_stdio_flush(FILE *f) {
101897be3a4Schristos 	int r;
102897be3a4Schristos 
103897be3a4Schristos 	r = fflush(f);
104897be3a4Schristos 	if (r == 0)
105897be3a4Schristos 		return (ISC_R_SUCCESS);
106897be3a4Schristos 	else
107897be3a4Schristos 		return (isc__errno2result(errno));
108897be3a4Schristos }
109897be3a4Schristos 
110897be3a4Schristos /*
111897be3a4Schristos  * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
112897be3a4Schristos  */
113897be3a4Schristos #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
114897be3a4Schristos #define ENOTSUP EOPNOTSUPP
115897be3a4Schristos #endif
116897be3a4Schristos 
117897be3a4Schristos isc_result_t
118897be3a4Schristos isc_stdio_sync(FILE *f) {
119897be3a4Schristos 	int r;
120897be3a4Schristos 
121897be3a4Schristos 	r = fsync(fileno(f));
122897be3a4Schristos 	/*
123897be3a4Schristos 	 * fsync is not supported on sockets and pipes which
124897be3a4Schristos 	 * result in EINVAL / ENOTSUP.
125897be3a4Schristos 	 */
126897be3a4Schristos 	if (r == 0 || errno == EINVAL || errno == ENOTSUP)
127897be3a4Schristos 		return (ISC_R_SUCCESS);
128897be3a4Schristos 	else
129897be3a4Schristos 		return (isc__errno2result(errno));
130897be3a4Schristos }
131897be3a4Schristos 
132