1 /* gstdio.h - GFilename wrappers for C library functions
3 * Copyright 2004 Tor Lillqvist
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
24 #include <glib/gprintf.h>
31 #if (defined (__MINGW64_VERSION_MAJOR) || defined (_MSC_VER)) && !defined(_WIN64)
33 /* Make it clear that we mean the struct with 32-bit st_size and
34 * 32-bit st_*time fields as that is how the 32-bit GLib DLL normally
35 * has been compiled. If you get a compiler warning when calling
36 * g_stat(), do take it seriously and make sure that the type of
37 * struct stat the code in GLib fills in matches the struct the type
38 * of struct stat you pass to g_stat(). To avoid hassle, to get file
39 * attributes just use the GIO API instead which doesn't use struct
42 * Sure, it would be nicer to use a struct with 64-bit st_size and
43 * 64-bit st_*time fields, but changing that now would break ABI. And
44 * in MinGW, a plain "struct stat" is the one with 32-bit st_size and
48 typedef struct _stat32 GStatBuf;
50 #elif defined(__MINGW64_VERSION_MAJOR) && defined(_WIN64)
52 typedef struct _stat64 GStatBuf;
56 typedef struct stat GStatBuf;
60 #if defined(G_OS_UNIX) && !defined(G_STDIO_WRAP_ON_UNIX)
62 /* Just pass on to the system functions, so there's no potential for data
63 * format mismatches, especially with large file interfaces.
64 * A few functions can't be handled in this way, since they are not defined
65 * in a portable system header that we could include here.
67 * G_STDIO_WRAP_ON_UNIX is not public API and its behaviour is not guaranteed
71 #ifndef __GTK_DOC_IGNORE__
75 #define g_rename rename
79 #define g_remove remove
81 #define g_freopen freopen
87 int g_access (const gchar *filename,
91 int g_chdir (const gchar *path);
94 int g_unlink (const gchar *filename);
97 int g_rmdir (const gchar *filename);
99 #else /* ! G_OS_UNIX */
101 /* Wrappers for C library functions that take pathname arguments. On
102 * Unix, the pathname is a file name as it literally is in the file
103 * system. On well-maintained systems with consistent users who know
104 * what they are doing and no exchange of files with others this would
105 * be a well-defined encoding, preferably UTF-8. On Windows, the
106 * pathname is always in UTF-8, even if that is not the on-disk
107 * encoding, and not the encoding accepted by the C library or Win32
111 GLIB_AVAILABLE_IN_ALL
112 int g_access (const gchar *filename,
115 GLIB_AVAILABLE_IN_ALL
116 int g_chmod (const gchar *filename,
119 GLIB_AVAILABLE_IN_ALL
120 int g_open (const gchar *filename,
124 GLIB_AVAILABLE_IN_ALL
125 int g_creat (const gchar *filename,
128 GLIB_AVAILABLE_IN_ALL
129 int g_rename (const gchar *oldfilename,
130 const gchar *newfilename);
132 GLIB_AVAILABLE_IN_ALL
133 int g_mkdir (const gchar *filename,
136 GLIB_AVAILABLE_IN_ALL
137 int g_chdir (const gchar *path);
139 GLIB_AVAILABLE_IN_ALL
140 int g_stat (const gchar *filename,
143 GLIB_AVAILABLE_IN_ALL
144 int g_lstat (const gchar *filename,
147 GLIB_AVAILABLE_IN_ALL
148 int g_unlink (const gchar *filename);
150 GLIB_AVAILABLE_IN_ALL
151 int g_remove (const gchar *filename);
153 GLIB_AVAILABLE_IN_ALL
154 int g_rmdir (const gchar *filename);
156 GLIB_AVAILABLE_IN_ALL
157 FILE *g_fopen (const gchar *filename,
160 GLIB_AVAILABLE_IN_ALL
161 FILE *g_freopen (const gchar *filename,
165 GLIB_AVAILABLE_IN_2_64
166 gint g_fsync (gint fd);
168 struct utimbuf; /* Don't need the real definition of struct utimbuf when just
169 * including this header.
172 GLIB_AVAILABLE_IN_ALL
173 int g_utime (const gchar *filename,
174 struct utimbuf *utb);
176 #endif /* G_OS_UNIX */
178 GLIB_AVAILABLE_IN_2_36
179 gboolean g_close (gint fd,
182 GLIB_AVAILABLE_STATIC_INLINE_IN_2_76
183 static inline gboolean
184 g_clear_fd (int *fd_ptr,
194 /* Suppress "Not available before" warning */
195 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
196 return g_close (fd, error);
197 G_GNUC_END_IGNORE_DEPRECATIONS
200 /* g_autofd should be defined on the same compilers where g_autofree is
201 * This avoids duplicating the feature-detection here. */
203 #ifndef __GTK_DOC_IGNORE__
206 _g_clear_fd_ignore_error (int *fd_ptr)
208 /* Don't overwrite thread-local errno if closing the fd fails */
211 /* Suppress "Not available before" warning */
212 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
214 if (!g_clear_fd (fd_ptr, NULL))
216 /* Do nothing: we ignore all errors, except for EBADF which
217 * is a programming error, checked for by g_close(). */
220 G_GNUC_END_IGNORE_DEPRECATIONS
226 #define g_autofd _GLIB_CLEANUP(_g_clear_fd_ignore_error) GLIB_AVAILABLE_MACRO_IN_2_76
231 #endif /* __G_STDIO_H__ */