stdio.c revision 280849
1234370Sjasone/*
2234370Sjasone * Copyright (C) 2004, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3234370Sjasone * Copyright (C) 2000, 2001  Internet Software Consortium.
4234370Sjasone *
5234370Sjasone * Permission to use, copy, modify, and/or distribute this software for any
6234370Sjasone * purpose with or without fee is hereby granted, provided that the above
7234370Sjasone * copyright notice and this permission notice appear in all copies.
8234370Sjasone *
9234370Sjasone * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10235238Sjasone * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11234370Sjasone * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12234370Sjasone * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13234370Sjasone * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14234370Sjasone * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15234370Sjasone * PERFORMANCE OF THIS SOFTWARE.
16234370Sjasone */
17234370Sjasone
18234370Sjasone/* $Id$ */
19234370Sjasone
20234370Sjasone#include <config.h>
21234370Sjasone
22234370Sjasone#include <errno.h>
23234370Sjasone#include <unistd.h>
24234370Sjasone
25234370Sjasone#include <isc/stdio.h>
26234370Sjasone#include <isc/stat.h>
27234370Sjasone
28234370Sjasone#include "errno2result.h"
29234370Sjasone
30234370Sjasoneisc_result_t
31234370Sjasoneisc_stdio_open(const char *filename, const char *mode, FILE **fp) {
32234370Sjasone	FILE *f;
33234370Sjasone
34234370Sjasone	f = fopen(filename, mode);
35234370Sjasone	if (f == NULL)
36234370Sjasone		return (isc__errno2result(errno));
37234370Sjasone	*fp = f;
38234370Sjasone	return (ISC_R_SUCCESS);
39234370Sjasone}
40234370Sjasone
41234370Sjasoneisc_result_t
42234370Sjasoneisc_stdio_close(FILE *f) {
43242844Sjasone	int r;
44242844Sjasone
45242844Sjasone	r = fclose(f);
46242844Sjasone	if (r == 0)
47242844Sjasone		return (ISC_R_SUCCESS);
48242844Sjasone	else
49234370Sjasone		return (isc__errno2result(errno));
50235238Sjasone}
51234370Sjasone
52234370Sjasoneisc_result_t
53235322Sjasoneisc_stdio_seek(FILE *f, long offset, int whence) {
54235322Sjasone	int r;
55234370Sjasone
56235238Sjasone	r = fseek(f, offset, whence);
57242844Sjasone	if (r == 0)
58242844Sjasone		return (ISC_R_SUCCESS);
59234370Sjasone	else
60242844Sjasone		return (isc__errno2result(errno));
61242844Sjasone}
62234370Sjasone
63234370Sjasoneisc_result_t
64234370Sjasoneisc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
65234370Sjasone	isc_result_t result = ISC_R_SUCCESS;
66234370Sjasone	size_t r;
67234370Sjasone
68234370Sjasone	clearerr(f);
69234370Sjasone	r = fread(ptr, size, nmemb, f);
70234370Sjasone	if (r != nmemb) {
71234370Sjasone		if (feof(f))
72234370Sjasone			result = ISC_R_EOF;
73234370Sjasone		else
74234370Sjasone			result = isc__errno2result(errno);
75234370Sjasone	}
76234370Sjasone	if (nret != NULL)
77234370Sjasone		*nret = r;
78234370Sjasone	return (result);
79234370Sjasone}
80234370Sjasone
81234370Sjasoneisc_result_t
82234370Sjasoneisc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
83234370Sjasone	       size_t *nret)
84234370Sjasone{
85234370Sjasone	isc_result_t result = ISC_R_SUCCESS;
86234370Sjasone	size_t r;
87234370Sjasone
88234370Sjasone	clearerr(f);
89234370Sjasone	r = fwrite(ptr, size, nmemb, f);
90234370Sjasone	if (r != nmemb)
91234370Sjasone		result = isc__errno2result(errno);
92234370Sjasone	if (nret != NULL)
93234370Sjasone		*nret = r;
94234370Sjasone	return (result);
95234370Sjasone}
96234370Sjasone
97234370Sjasoneisc_result_t
98234370Sjasoneisc_stdio_flush(FILE *f) {
99234370Sjasone	int r;
100234370Sjasone
101234370Sjasone	r = fflush(f);
102234370Sjasone	if (r == 0)
103234370Sjasone		return (ISC_R_SUCCESS);
104234370Sjasone	else
105234370Sjasone		return (isc__errno2result(errno));
106234370Sjasone}
107234370Sjasone
108234370Sjasone/*
109234370Sjasone * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
110234370Sjasone */
111234370Sjasone#if defined(EOPNOTSUPP) && !defined(ENOTSUP)
112234370Sjasone#define ENOTSUP EOPNOTSUPP
113234370Sjasone#endif
114234370Sjasone
115234370Sjasoneisc_result_t
116234370Sjasoneisc_stdio_sync(FILE *f) {
117234370Sjasone	int r;
118234370Sjasone
119234370Sjasone	r = fsync(fileno(f));
120234370Sjasone	/*
121234370Sjasone	 * fsync is not supported on sockets and pipes which
122234370Sjasone	 * result in EINVAL / ENOTSUP.
123234370Sjasone	 */
124234370Sjasone	if (r == 0 || errno == EINVAL || errno == ENOTSUP)
125234370Sjasone		return (ISC_R_SUCCESS);
126234370Sjasone	else
127234370Sjasone		return (isc__errno2result(errno));
128234370Sjasone}
129234370Sjasone
130234370Sjasone