1 /* $NetBSD: stdio.c,v 1.2 2024/08/18 20:47:16 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2007, 2011, 2012 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 */ 21 22 #include <config.h> 23 24 #include <errno.h> 25 #include <unistd.h> 26 27 #include <isc/stdio.h> 28 #include <isc/stat.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, long offset, int whence) { 56 int r; 57 58 r = fseek(f, offset, whence); 59 if (r == 0) 60 return (ISC_R_SUCCESS); 61 else 62 return (isc__errno2result(errno)); 63 } 64 65 isc_result_t 66 isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) { 67 isc_result_t result = ISC_R_SUCCESS; 68 size_t r; 69 70 clearerr(f); 71 r = fread(ptr, size, nmemb, f); 72 if (r != nmemb) { 73 if (feof(f)) 74 result = ISC_R_EOF; 75 else 76 result = isc__errno2result(errno); 77 } 78 if (nret != NULL) 79 *nret = r; 80 return (result); 81 } 82 83 isc_result_t 84 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f, 85 size_t *nret) 86 { 87 isc_result_t result = ISC_R_SUCCESS; 88 size_t r; 89 90 clearerr(f); 91 r = fwrite(ptr, size, nmemb, f); 92 if (r != nmemb) 93 result = isc__errno2result(errno); 94 if (nret != NULL) 95 *nret = r; 96 return (result); 97 } 98 99 isc_result_t 100 isc_stdio_flush(FILE *f) { 101 int r; 102 103 r = fflush(f); 104 if (r == 0) 105 return (ISC_R_SUCCESS); 106 else 107 return (isc__errno2result(errno)); 108 } 109 110 /* 111 * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP. 112 */ 113 #if defined(EOPNOTSUPP) && !defined(ENOTSUP) 114 #define ENOTSUP EOPNOTSUPP 115 #endif 116 117 isc_result_t 118 isc_stdio_sync(FILE *f) { 119 int r; 120 121 r = fsync(fileno(f)); 122 /* 123 * fsync is not supported on sockets and pipes which 124 * result in EINVAL / ENOTSUP. 125 */ 126 if (r == 0 || errno == EINVAL || errno == ENOTSUP) 127 return (ISC_R_SUCCESS); 128 else 129 return (isc__errno2result(errno)); 130 } 131 132