gstdio.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* gstdio.h - GFilename wrappers for C library functions
  2. *
  3. * Copyright 2004 Tor Lillqvist
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __G_STDIO_H__
  21. #define __G_STDIO_H__
  22. #include <glib/gprintf.h>
  23. #include <errno.h>
  24. #include <sys/stat.h>
  25. G_BEGIN_DECLS
  26. #if (defined (__MINGW64_VERSION_MAJOR) || defined (_MSC_VER)) && !defined(_WIN64)
  27. /* Make it clear that we mean the struct with 32-bit st_size and
  28. * 32-bit st_*time fields as that is how the 32-bit GLib DLL normally
  29. * has been compiled. If you get a compiler warning when calling
  30. * g_stat(), do take it seriously and make sure that the type of
  31. * struct stat the code in GLib fills in matches the struct the type
  32. * of struct stat you pass to g_stat(). To avoid hassle, to get file
  33. * attributes just use the GIO API instead which doesn't use struct
  34. * stat.
  35. *
  36. * Sure, it would be nicer to use a struct with 64-bit st_size and
  37. * 64-bit st_*time fields, but changing that now would break ABI. And
  38. * in MinGW, a plain "struct stat" is the one with 32-bit st_size and
  39. * st_*time fields.
  40. */
  41. typedef struct _stat32 GStatBuf;
  42. #elif defined(__MINGW64_VERSION_MAJOR) && defined(_WIN64)
  43. typedef struct _stat64 GStatBuf;
  44. #else
  45. typedef struct stat GStatBuf;
  46. #endif
  47. #if defined(G_OS_UNIX) && !defined(G_STDIO_WRAP_ON_UNIX) && !defined(__GI_SCANNER__)
  48. /* Just pass on to the system functions, so there's no potential for data
  49. * format mismatches, especially with large file interfaces.
  50. * A few functions can't be handled in this way, since they are not defined
  51. * in a portable system header that we could include here.
  52. *
  53. * G_STDIO_WRAP_ON_UNIX is not public API and its behaviour is not guaranteed
  54. * in future.
  55. */
  56. #ifndef __GTK_DOC_IGNORE__
  57. #define g_chmod chmod
  58. #define g_open open
  59. #define g_creat creat
  60. #define g_rename rename
  61. #define g_mkdir mkdir
  62. #define g_stat stat
  63. #define g_lstat lstat
  64. #define g_remove remove
  65. #define g_fopen fopen
  66. #define g_freopen freopen
  67. #define g_fsync fsync
  68. #define g_utime utime
  69. #endif
  70. GLIB_AVAILABLE_IN_ALL
  71. int g_access (const gchar *filename,
  72. int mode);
  73. GLIB_AVAILABLE_IN_ALL
  74. int g_chdir (const gchar *path);
  75. GLIB_AVAILABLE_IN_ALL
  76. int g_unlink (const gchar *filename);
  77. GLIB_AVAILABLE_IN_ALL
  78. int g_rmdir (const gchar *filename);
  79. #else /* ! G_OS_UNIX */
  80. /* Wrappers for C library functions that take pathname arguments. On
  81. * Unix, the pathname is a file name as it literally is in the file
  82. * system. On well-maintained systems with consistent users who know
  83. * what they are doing and no exchange of files with others this would
  84. * be a well-defined encoding, preferably UTF-8. On Windows, the
  85. * pathname is always in UTF-8, even if that is not the on-disk
  86. * encoding, and not the encoding accepted by the C library or Win32
  87. * API.
  88. */
  89. GLIB_AVAILABLE_IN_ALL
  90. int g_access (const gchar *filename,
  91. int mode);
  92. GLIB_AVAILABLE_IN_ALL
  93. int g_chmod (const gchar *filename,
  94. int mode);
  95. GLIB_AVAILABLE_IN_ALL
  96. int g_open (const gchar *filename,
  97. int flags,
  98. int mode);
  99. GLIB_AVAILABLE_IN_ALL
  100. int g_creat (const gchar *filename,
  101. int mode);
  102. GLIB_AVAILABLE_IN_ALL
  103. int g_rename (const gchar *oldfilename,
  104. const gchar *newfilename);
  105. GLIB_AVAILABLE_IN_ALL
  106. int g_mkdir (const gchar *filename,
  107. int mode);
  108. GLIB_AVAILABLE_IN_ALL
  109. int g_chdir (const gchar *path);
  110. GLIB_AVAILABLE_IN_ALL
  111. int g_stat (const gchar *filename,
  112. GStatBuf *buf);
  113. GLIB_AVAILABLE_IN_ALL
  114. int g_lstat (const gchar *filename,
  115. GStatBuf *buf);
  116. GLIB_AVAILABLE_IN_ALL
  117. int g_unlink (const gchar *filename);
  118. GLIB_AVAILABLE_IN_ALL
  119. int g_remove (const gchar *filename);
  120. GLIB_AVAILABLE_IN_ALL
  121. int g_rmdir (const gchar *filename);
  122. GLIB_AVAILABLE_IN_ALL
  123. FILE *g_fopen (const gchar *filename,
  124. const gchar *mode);
  125. GLIB_AVAILABLE_IN_ALL
  126. FILE *g_freopen (const gchar *filename,
  127. const gchar *mode,
  128. FILE *stream);
  129. GLIB_AVAILABLE_IN_2_64
  130. gint g_fsync (gint fd);
  131. struct utimbuf; /* Don't need the real definition of struct utimbuf when just
  132. * including this header.
  133. */
  134. GLIB_AVAILABLE_IN_ALL
  135. int g_utime (const gchar *filename,
  136. struct utimbuf *utb);
  137. #endif /* G_OS_UNIX */
  138. GLIB_AVAILABLE_IN_2_36
  139. gboolean g_close (gint fd,
  140. GError **error);
  141. GLIB_AVAILABLE_STATIC_INLINE_IN_2_76
  142. static inline gboolean
  143. g_clear_fd (int *fd_ptr,
  144. GError **error)
  145. {
  146. int fd = *fd_ptr;
  147. *fd_ptr = -1;
  148. if (fd < 0)
  149. return TRUE;
  150. /* Suppress "Not available before" warning */
  151. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  152. return g_close (fd, error);
  153. G_GNUC_END_IGNORE_DEPRECATIONS
  154. }
  155. /* g_autofd should be defined on the same compilers where g_autofree is
  156. * This avoids duplicating the feature-detection here. */
  157. #ifdef g_autofree
  158. #ifndef __GTK_DOC_IGNORE__
  159. /* Not public API */
  160. static inline void
  161. _g_clear_fd_ignore_error (int *fd_ptr)
  162. {
  163. /* Don't overwrite thread-local errno if closing the fd fails */
  164. int errsv = errno;
  165. /* Suppress "Not available before" warning */
  166. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  167. if (!g_clear_fd (fd_ptr, NULL))
  168. {
  169. /* Do nothing: we ignore all errors, except for EBADF which
  170. * is a programming error, checked for by g_close(). */
  171. }
  172. G_GNUC_END_IGNORE_DEPRECATIONS
  173. errno = errsv;
  174. }
  175. #endif
  176. #define g_autofd _GLIB_CLEANUP(_g_clear_fd_ignore_error) GLIB_AVAILABLE_MACRO_IN_2_76
  177. #endif
  178. G_END_DECLS
  179. #endif /* __G_STDIO_H__ */