gv_fopen.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /// @file
  2. /// @brief wrapper around `fopen` for internal library usage
  3. #pragma once
  4. /// hide the symbols this header declares by default
  5. ///
  6. /// The expectation is that users of this header (applications, shared
  7. /// libraries, or static libraries) want to call `gv_fopen` but not re-export it
  8. /// to their users. This annotation is only correct while the containing library
  9. /// is built statically. If it were built as a shared library, `gv_fopen` would
  10. /// need to have `default` visibility (and thus be unavoidably re-exported) in
  11. /// order to be callable.
  12. #ifndef UTIL_API
  13. #if !defined(__CYGWIN__) && defined(__GNUC__) && !defined(__MINGW32__)
  14. #define UTIL_API __attribute__((visibility("hidden")))
  15. #else
  16. #define UTIL_API /* nothing */
  17. #endif
  18. #endif
  19. #include <stdio.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /// open a file, setting close-on-exec
  24. ///
  25. /// Generally, library code should set close-on-exec (`O_CLOEXEC`) on file
  26. /// descriptors it creates to avoid child processes of concurrent `fork`+`exec`
  27. /// operations accidentally inheriting copies of the descriptors. It is tricky
  28. /// to achieve this without races. This function attempts to avoid the common
  29. /// problems when trying to do this with `fopen`.
  30. ///
  31. /// @param filename A filename, as you would pass to `fopen`
  32. /// @param mode A mode, as you would pass to `fopen`
  33. /// @return A file handle with close-on-exit set on success or `NULL` on failure
  34. UTIL_API FILE *gv_fopen(const char *filename, const char *mode);
  35. #ifdef __cplusplus
  36. }
  37. #endif