/contrib/bind9/lib/isc/unix/stdio.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 129 lines · 84 code · 21 blank · 24 comment · 24 complexity · 85c31edab4ed3d830d939db9f3bf7ce5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000, 2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. #include <config.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <isc/stdio.h>
  22. #include <isc/stat.h>
  23. #include "errno2result.h"
  24. isc_result_t
  25. isc_stdio_open(const char *filename, const char *mode, FILE **fp) {
  26. FILE *f;
  27. f = fopen(filename, mode);
  28. if (f == NULL)
  29. return (isc__errno2result(errno));
  30. *fp = f;
  31. return (ISC_R_SUCCESS);
  32. }
  33. isc_result_t
  34. isc_stdio_close(FILE *f) {
  35. int r;
  36. r = fclose(f);
  37. if (r == 0)
  38. return (ISC_R_SUCCESS);
  39. else
  40. return (isc__errno2result(errno));
  41. }
  42. isc_result_t
  43. isc_stdio_seek(FILE *f, long offset, int whence) {
  44. int r;
  45. r = fseek(f, offset, whence);
  46. if (r == 0)
  47. return (ISC_R_SUCCESS);
  48. else
  49. return (isc__errno2result(errno));
  50. }
  51. isc_result_t
  52. isc_stdio_read(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
  53. isc_result_t result = ISC_R_SUCCESS;
  54. size_t r;
  55. clearerr(f);
  56. r = fread(ptr, size, nmemb, f);
  57. if (r != nmemb) {
  58. if (feof(f))
  59. result = ISC_R_EOF;
  60. else
  61. result = isc__errno2result(errno);
  62. }
  63. if (nret != NULL)
  64. *nret = r;
  65. return (result);
  66. }
  67. isc_result_t
  68. isc_stdio_write(const void *ptr, size_t size, size_t nmemb, FILE *f,
  69. size_t *nret)
  70. {
  71. isc_result_t result = ISC_R_SUCCESS;
  72. size_t r;
  73. clearerr(f);
  74. r = fwrite(ptr, size, nmemb, f);
  75. if (r != nmemb)
  76. result = isc__errno2result(errno);
  77. if (nret != NULL)
  78. *nret = r;
  79. return (result);
  80. }
  81. isc_result_t
  82. isc_stdio_flush(FILE *f) {
  83. int r;
  84. r = fflush(f);
  85. if (r == 0)
  86. return (ISC_R_SUCCESS);
  87. else
  88. return (isc__errno2result(errno));
  89. }
  90. /*
  91. * OpenBSD has deprecated ENOTSUP in favor of EOPNOTSUPP.
  92. */
  93. #if defined(EOPNOTSUPP) && !defined(ENOTSUP)
  94. #define ENOTSUP EOPNOTSUPP
  95. #endif
  96. isc_result_t
  97. isc_stdio_sync(FILE *f) {
  98. int r;
  99. r = fsync(fileno(f));
  100. /*
  101. * fsync is not supported on sockets and pipes which
  102. * result in EINVAL / ENOTSUP.
  103. */
  104. if (r == 0 || errno == EINVAL || errno == ENOTSUP)
  105. return (ISC_R_SUCCESS);
  106. else
  107. return (isc__errno2result(errno));
  108. }