stdio.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2007, 2011-2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
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  * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
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          * fsync is not supported on sockets and pipes which
00145          * result in EINVAL / ENOTSUP.
00146          */
00147         if (r == 0 || errno == EINVAL || errno == ENOTSUP)
00148                 return (ISC_R_SUCCESS);
00149         else
00150                 return (isc__errno2result(errno));
00151 }
00152 

Generated on Tue Apr 28 17:41:06 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha