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