xref: /freebsd/contrib/ntp/libntp/lib/isc/unix/stdio.c (revision a466cc55)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 2000, 2001  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert #include <config.h>
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <errno.h>
23*a466cc55SCy Schubert #include <unistd.h>
24*a466cc55SCy Schubert 
25*a466cc55SCy Schubert #include <isc/stdio.h>
26*a466cc55SCy Schubert #include <isc/stat.h>
27*a466cc55SCy Schubert 
28*a466cc55SCy Schubert #include "errno2result.h"
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert isc_result_t
isc_stdio_open(const char * filename,const char * mode,FILE ** fp)31*a466cc55SCy Schubert isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
32*a466cc55SCy Schubert 	FILE *f;
33*a466cc55SCy Schubert 
34*a466cc55SCy Schubert 	f = fopen(filename, mode);
35*a466cc55SCy Schubert 	if (f == NULL)
36*a466cc55SCy Schubert 		return (isc__errno2result(errno));
37*a466cc55SCy Schubert 	*fp = f;
38*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
39*a466cc55SCy Schubert }
40*a466cc55SCy Schubert 
41*a466cc55SCy Schubert isc_result_t
isc_stdio_close(FILE * f)42*a466cc55SCy Schubert isc_stdio_close(FILE *f) {
43*a466cc55SCy Schubert 	int r;
44*a466cc55SCy Schubert 
45*a466cc55SCy Schubert 	r = fclose(f);
46*a466cc55SCy Schubert 	if (r == 0)
47*a466cc55SCy Schubert 		return (ISC_R_SUCCESS);
48*a466cc55SCy Schubert 	else
49*a466cc55SCy Schubert 		return (isc__errno2result(errno));
50*a466cc55SCy Schubert }
51*a466cc55SCy Schubert 
52*a466cc55SCy Schubert isc_result_t
isc_stdio_seek(FILE * f,long offset,int whence)53*a466cc55SCy Schubert isc_stdio_seek(FILE *f, long offset, int whence) {
54*a466cc55SCy Schubert 	int r;
55*a466cc55SCy Schubert 
56*a466cc55SCy Schubert 	r = fseek(f, offset, whence);
57*a466cc55SCy Schubert 	if (r == 0)
58*a466cc55SCy Schubert 		return (ISC_R_SUCCESS);
59*a466cc55SCy Schubert 	else
60*a466cc55SCy Schubert 		return (isc__errno2result(errno));
61*a466cc55SCy Schubert }
62*a466cc55SCy Schubert 
63*a466cc55SCy Schubert isc_result_t
isc_stdio_read(void * ptr,size_t size,size_t nmemb,FILE * f,size_t * nret)64*a466cc55SCy Schubert isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
65*a466cc55SCy Schubert 	isc_result_t result = ISC_R_SUCCESS;
66*a466cc55SCy Schubert 	size_t r;
67*a466cc55SCy Schubert 
68*a466cc55SCy Schubert 	clearerr(f);
69*a466cc55SCy Schubert 	r = fread(ptr, size, nmemb, f);
70*a466cc55SCy Schubert 	if (r != nmemb) {
71*a466cc55SCy Schubert 		if (feof(f))
72*a466cc55SCy Schubert 			result = ISC_R_EOF;
73*a466cc55SCy Schubert 		else
74*a466cc55SCy Schubert 			result = isc__errno2result(errno);
75*a466cc55SCy Schubert 	}
76*a466cc55SCy Schubert 	if (nret != NULL)
77*a466cc55SCy Schubert 		*nret = r;
78*a466cc55SCy Schubert 	return (result);
79*a466cc55SCy Schubert }
80*a466cc55SCy Schubert 
81*a466cc55SCy Schubert isc_result_t
isc_stdio_write(const void * ptr,size_t size,size_t nmemb,FILE * f,size_t * nret)82*a466cc55SCy Schubert isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
83*a466cc55SCy Schubert 	       size_t *nret)
84*a466cc55SCy Schubert {
85*a466cc55SCy Schubert 	isc_result_t result = ISC_R_SUCCESS;
86*a466cc55SCy Schubert 	size_t r;
87*a466cc55SCy Schubert 
88*a466cc55SCy Schubert 	clearerr(f);
89*a466cc55SCy Schubert 	r = fwrite(ptr, size, nmemb, f);
90*a466cc55SCy Schubert 	if (r != nmemb)
91*a466cc55SCy Schubert 		result = isc__errno2result(errno);
92*a466cc55SCy Schubert 	if (nret != NULL)
93*a466cc55SCy Schubert 		*nret = r;
94*a466cc55SCy Schubert 	return (result);
95*a466cc55SCy Schubert }
96*a466cc55SCy Schubert 
97*a466cc55SCy Schubert isc_result_t
isc_stdio_flush(FILE * f)98*a466cc55SCy Schubert isc_stdio_flush(FILE *f) {
99*a466cc55SCy Schubert 	int r;
100*a466cc55SCy Schubert 
101*a466cc55SCy Schubert 	r = fflush(f);
102*a466cc55SCy Schubert 	if (r == 0)
103*a466cc55SCy Schubert 		return (ISC_R_SUCCESS);
104*a466cc55SCy Schubert 	else
105*a466cc55SCy Schubert 		return (isc__errno2result(errno));
106*a466cc55SCy Schubert }
107*a466cc55SCy Schubert 
108*a466cc55SCy Schubert /*
109*a466cc55SCy Schubert  * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
110*a466cc55SCy Schubert  */
111*a466cc55SCy Schubert #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
112*a466cc55SCy Schubert #define ENOTSUP EOPNOTSUPP
113*a466cc55SCy Schubert #endif
114*a466cc55SCy Schubert 
115*a466cc55SCy Schubert isc_result_t
isc_stdio_sync(FILE * f)116*a466cc55SCy Schubert isc_stdio_sync(FILE *f) {
117*a466cc55SCy Schubert 	int r;
118*a466cc55SCy Schubert 
119*a466cc55SCy Schubert 	r = fsync(fileno(f));
120*a466cc55SCy Schubert 	/*
121*a466cc55SCy Schubert 	 * fsync is not supported on sockets and pipes which
122*a466cc55SCy Schubert 	 * result in EINVAL / ENOTSUP.
123*a466cc55SCy Schubert 	 */
124*a466cc55SCy Schubert 	if (r == 0 || errno == EINVAL || errno == ENOTSUP)
125*a466cc55SCy Schubert 		return (ISC_R_SUCCESS);
126*a466cc55SCy Schubert 	else
127*a466cc55SCy Schubert 		return (isc__errno2result(errno));
128*a466cc55SCy Schubert }
129*a466cc55SCy Schubert 
130