1 /* $NetBSD: stdio.c,v 1.5 2014/12/10 04:38:01 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2007, 2013 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2000, 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: stdio.c,v 1.6 2007/06/19 23:47:19 tbox Exp */ 21 22 #include <config.h> 23 24 #include <io.h> 25 #include <errno.h> 26 27 #include <isc/stdio.h> 28 #include <isc/util.h> 29 30 #include "errno2result.h" 31 32 isc_result_t 33 isc_stdio_open(const char *filename, const char *mode, FILE **fp) { 34 FILE *f; 35 36 f = fopen(filename, mode); 37 if (f == NULL) 38 return (isc__errno2result(errno)); 39 *fp = f; 40 return (ISC_R_SUCCESS); 41 } 42 43 isc_result_t 44 isc_stdio_close(FILE *f) { 45 int r; 46 47 r = fclose(f); 48 if (r == 0) 49 return (ISC_R_SUCCESS); 50 else 51 return (isc__errno2result(errno)); 52 } 53 54 isc_result_t 55 isc_stdio_seek(FILE *f, off_t offset, int whence) { 56 int r; 57 58 #ifndef _WIN64 59 r = fseek(f, offset, whence); 60 #else 61 r = _fseeki64(f, offset, whence); 62 #endif 63 if (r == 0) 64 return (ISC_R_SUCCESS); 65 else 66 return (isc__errno2result(errno)); 67 } 68 69 isc_result_t 70 isc_stdio_tell(FILE *f, off_t *offsetp) { 71 #ifndef _WIN64 72 long r; 73 #else 74 __int64 r; 75 #endif 76 77 REQUIRE(offsetp != NULL); 78 79 #ifndef _WIN64 80 r = ftell(f); 81 #else 82 r = _ftelli64(f); 83 #endif 84 if (r >= 0) { 85 *offsetp = r; 86 return (ISC_R_SUCCESS); 87 } else 88 return (isc__errno2result(errno)); 89 } 90 91 isc_result_t 92 isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) { 93 isc_result_t result = ISC_R_SUCCESS; 94 size_t r; 95 96 clearerr(f); 97 r = fread(ptr, size, nmemb, f); 98 if (r != nmemb) { 99 if (feof(f)) 100 result = ISC_R_EOF; 101 else 102 result = isc__errno2result(errno); 103 } 104 if (nret != NULL) 105 *nret = r; 106 return (result); 107 } 108 109 isc_result_t 110 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f, 111 size_t *nret) 112 { 113 isc_result_t result = ISC_R_SUCCESS; 114 size_t r; 115 116 clearerr(f); 117 r = fwrite(ptr, size, nmemb, f); 118 if (r != nmemb) 119 result = isc__errno2result(errno); 120 if (nret != NULL) 121 *nret = r; 122 return (result); 123 } 124 125 isc_result_t 126 isc_stdio_flush(FILE *f) { 127 int r; 128 129 r = fflush(f); 130 if (r == 0) 131 return (ISC_R_SUCCESS); 132 else 133 return (isc__errno2result(errno)); 134 } 135 136 isc_result_t 137 isc_stdio_sync(FILE *f) { 138 int r; 139 140 r = _commit(_fileno(f)); 141 if (r == 0) 142 return (ISC_R_SUCCESS); 143 else 144 return (isc__errno2result(errno)); 145 } 146 147