00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <errno.h>
00023 #include <unistd.h>
00024
00025 #include <isc/stdio.h>
00026 #include <isc/stat.h>
00027 #include <isc/util.h>
00028
00029 #include "errno2result.h"
00030
00031 isc_result_t
00032 isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
00033 FILE *f;
00034
00035 f = fopen(filename, mode);
00036 if (f == NULL)
00037 return (isc__errno2result(errno));
00038 *fp = f;
00039 return (ISC_R_SUCCESS);
00040 }
00041
00042 isc_result_t
00043 isc_stdio_close(FILE *f) {
00044 int r;
00045
00046 r = fclose(f);
00047 if (r == 0)
00048 return (ISC_R_SUCCESS);
00049 else
00050 return (isc__errno2result(errno));
00051 }
00052
00053 isc_result_t
00054 isc_stdio_seek(FILE *f, off_t offset, int whence) {
00055 int r;
00056
00057 #ifdef HAVE_FSEEKO
00058 r = fseeko(f, offset, whence);
00059 #else
00060 r = fseek(f, offset, whence);
00061 #endif
00062 if (r == 0)
00063 return (ISC_R_SUCCESS);
00064 else
00065 return (isc__errno2result(errno));
00066 }
00067
00068 isc_result_t
00069 isc_stdio_tell(FILE *f, off_t *offsetp) {
00070 off_t r;
00071
00072 REQUIRE(offsetp != NULL);
00073
00074 #ifdef HAVE_FTELLO
00075 r = ftello(f);
00076 #else
00077 r = ftell(f);
00078 #endif
00079 if (r >= 0) {
00080 *offsetp = r;
00081 return (ISC_R_SUCCESS);
00082 } else
00083 return (isc__errno2result(errno));
00084 }
00085
00086 isc_result_t
00087 isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
00088 isc_result_t result = ISC_R_SUCCESS;
00089 size_t r;
00090
00091 clearerr(f);
00092 r = fread(ptr, size, nmemb, f);
00093 if (r != nmemb) {
00094 if (feof(f))
00095 result = ISC_R_EOF;
00096 else
00097 result = isc__errno2result(errno);
00098 }
00099 if (nret != NULL)
00100 *nret = r;
00101 return (result);
00102 }
00103
00104 isc_result_t
00105 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
00106 size_t *nret)
00107 {
00108 isc_result_t result = ISC_R_SUCCESS;
00109 size_t r;
00110
00111 clearerr(f);
00112 r = fwrite(ptr, size, nmemb, f);
00113 if (r != nmemb)
00114 result = isc__errno2result(errno);
00115 if (nret != NULL)
00116 *nret = r;
00117 return (result);
00118 }
00119
00120 isc_result_t
00121 isc_stdio_flush(FILE *f) {
00122 int r;
00123
00124 r = fflush(f);
00125 if (r == 0)
00126 return (ISC_R_SUCCESS);
00127 else
00128 return (isc__errno2result(errno));
00129 }
00130
00131
00132
00133
00134 #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
00135 #define ENOTSUP EOPNOTSUPP
00136 #endif
00137
00138 isc_result_t
00139 isc_stdio_sync(FILE *f) {
00140 int r;
00141
00142 r = fsync(fileno(f));
00143
00144
00145
00146
00147 if (r == 0 || errno == EINVAL || errno == ENOTSUP)
00148 return (ISC_R_SUCCESS);
00149 else
00150 return (isc__errno2result(errno));
00151 }
00152