1 /* $NetBSD: stdio.c,v 1.1.1.1 2009/12/13 16:54:38 kardel Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2007 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.8 2007/06/19 23:47:18 tbox Exp */ 21 22 #include <config.h> 23 24 #include <errno.h> 25 #include <unistd.h> 26 27 #include <isc/stdio.h> 28 29 #include "errno2result.h" 30 31 isc_result_t 32 isc_stdio_open(const char *filename, const char *mode, FILE **fp) { 33 FILE *f; 34 35 f = fopen(filename, mode); 36 if (f == NULL) 37 return (isc__errno2result(errno)); 38 *fp = f; 39 return (ISC_R_SUCCESS); 40 } 41 42 isc_result_t 43 isc_stdio_close(FILE *f) { 44 int r; 45 46 r = fclose(f); 47 if (r == 0) 48 return (ISC_R_SUCCESS); 49 else 50 return (isc__errno2result(errno)); 51 } 52 53 isc_result_t 54 isc_stdio_seek(FILE *f, long offset, int whence) { 55 int r; 56 57 r = fseek(f, offset, whence); 58 if (r == 0) 59 return (ISC_R_SUCCESS); 60 else 61 return (isc__errno2result(errno)); 62 } 63 64 isc_result_t 65 isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) { 66 isc_result_t result = ISC_R_SUCCESS; 67 size_t r; 68 69 clearerr(f); 70 r = fread(ptr, size, nmemb, f); 71 if (r != nmemb) { 72 if (feof(f)) 73 result = ISC_R_EOF; 74 else 75 result = isc__errno2result(errno); 76 } 77 if (nret != NULL) 78 *nret = r; 79 return (result); 80 } 81 82 isc_result_t 83 isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f, 84 size_t *nret) 85 { 86 isc_result_t result = ISC_R_SUCCESS; 87 size_t r; 88 89 clearerr(f); 90 r = fwrite(ptr, size, nmemb, f); 91 if (r != nmemb) 92 result = isc__errno2result(errno); 93 if (nret != NULL) 94 *nret = r; 95 return (result); 96 } 97 98 isc_result_t 99 isc_stdio_flush(FILE *f) { 100 int r; 101 102 r = fflush(f); 103 if (r == 0) 104 return (ISC_R_SUCCESS); 105 else 106 return (isc__errno2result(errno)); 107 } 108 109 isc_result_t 110 isc_stdio_sync(FILE *f) { 111 int r; 112 113 r = fsync(fileno(f)); 114 if (r == 0) 115 return (ISC_R_SUCCESS); 116 else 117 return (isc__errno2result(errno)); 118 } 119 120