1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2000, 2001  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: stdio.c,v 1.6 2007/06/19 23:47:19 tbox Exp $ */
19258945Sroberto
20258945Sroberto#include <config.h>
21258945Sroberto
22258945Sroberto#include <io.h>
23258945Sroberto#include <errno.h>
24258945Sroberto
25258945Sroberto#include <isc/stdio.h>
26258945Sroberto
27258945Sroberto#include "errno2result.h"
28258945Sroberto
29258945Srobertoisc_result_t
30258945Srobertoisc_stdio_open(const char *filename, const char *mode, FILE **fp) {
31258945Sroberto	FILE *f;
32258945Sroberto
33258945Sroberto	f = fopen(filename, mode);
34258945Sroberto	if (f == NULL)
35258945Sroberto		return (isc__errno2result(errno));
36258945Sroberto	*fp = f;
37258945Sroberto	return (ISC_R_SUCCESS);
38258945Sroberto}
39258945Sroberto
40258945Srobertoisc_result_t
41258945Srobertoisc_stdio_close(FILE *f) {
42258945Sroberto	int r;
43258945Sroberto
44258945Sroberto	r = fclose(f);
45258945Sroberto	if (r == 0)
46258945Sroberto		return (ISC_R_SUCCESS);
47258945Sroberto	else
48258945Sroberto		return (isc__errno2result(errno));
49258945Sroberto}
50258945Sroberto
51258945Srobertoisc_result_t
52258945Srobertoisc_stdio_seek(FILE *f, long offset, int whence) {
53258945Sroberto	int r;
54258945Sroberto
55258945Sroberto	r = fseek(f, offset, whence);
56258945Sroberto	if (r == 0)
57258945Sroberto		return (ISC_R_SUCCESS);
58258945Sroberto	else
59258945Sroberto		return (isc__errno2result(errno));
60258945Sroberto}
61258945Sroberto
62258945Srobertoisc_result_t
63258945Srobertoisc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
64258945Sroberto	isc_result_t result = ISC_R_SUCCESS;
65258945Sroberto	size_t r;
66258945Sroberto
67258945Sroberto	clearerr(f);
68258945Sroberto	r = fread(ptr, size, nmemb, f);
69258945Sroberto	if (r != nmemb) {
70258945Sroberto		if (feof(f))
71258945Sroberto			result = ISC_R_EOF;
72258945Sroberto		else
73258945Sroberto			result = isc__errno2result(errno);
74258945Sroberto	}
75258945Sroberto	if (nret != NULL)
76258945Sroberto		*nret = r;
77258945Sroberto	return (result);
78258945Sroberto}
79258945Sroberto
80258945Srobertoisc_result_t
81258945Srobertoisc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
82258945Sroberto	       size_t *nret)
83258945Sroberto{
84258945Sroberto	isc_result_t result = ISC_R_SUCCESS;
85258945Sroberto	size_t r;
86258945Sroberto
87258945Sroberto	clearerr(f);
88258945Sroberto	r = fwrite(ptr, size, nmemb, f);
89258945Sroberto	if (r != nmemb)
90258945Sroberto		result = isc__errno2result(errno);
91258945Sroberto	if (nret != NULL)
92258945Sroberto		*nret = r;
93258945Sroberto	return (result);
94258945Sroberto}
95258945Sroberto
96258945Srobertoisc_result_t
97258945Srobertoisc_stdio_flush(FILE *f) {
98258945Sroberto	int r;
99258945Sroberto
100258945Sroberto	r = fflush(f);
101258945Sroberto	if (r == 0)
102258945Sroberto		return (ISC_R_SUCCESS);
103258945Sroberto	else
104258945Sroberto		return (isc__errno2result(errno));
105258945Sroberto}
106258945Sroberto
107258945Srobertoisc_result_t
108258945Srobertoisc_stdio_sync(FILE *f) {
109258945Sroberto	int r;
110258945Sroberto
111258945Sroberto	r = _commit(_fileno(f));
112258945Sroberto	if (r == 0)
113258945Sroberto		return (ISC_R_SUCCESS);
114258945Sroberto	else
115258945Sroberto		return (isc__errno2result(errno));
116258945Sroberto}
117258945Sroberto
118