Coverage Report

Created: 2026-10-13 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/usr/local/include/glib-2.0/glib/gstdio.h
Line
Count
Source
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
21
#ifndef __G_STDIO_H__
22
#define __G_STDIO_H__
23
24
#include <glib/gprintf.h>
25
26
#include <errno.h>
27
#include <sys/stat.h>
28
29
G_BEGIN_DECLS
30
31
#if (defined (__MINGW64_VERSION_MAJOR) || defined (_MSC_VER)) && !defined(_WIN64)
32
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
40
 * stat.
41
 *
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
45
 * st_*time fields.
46
 */
47
48
typedef struct _stat32 GStatBuf;
49
50
#elif defined(__MINGW64_VERSION_MAJOR) && defined(_WIN64)
51
52
typedef struct _stat64 GStatBuf;
53
54
#else
55
56
typedef struct stat GStatBuf;
57
58
#endif
59
60
#ifndef __GI_SCANNER__
61
# if defined (G_OS_UNIX) && !defined (G_STDIO_WRAP_ON_UNIX)
62
#   define G_STDIO_LIBC_DIRECT
63
# endif
64
#endif
65
66
#ifdef G_STDIO_LIBC_DIRECT
67
68
/* Just pass on to the system functions, so there's no potential for data
69
 * format mismatches, especially with large file interfaces. 
70
 * A few functions can't be handled in this way, since they are not defined
71
 * in a portable system header that we could include here.
72
 *
73
 * G_STDIO_WRAP_ON_UNIX is not public API and its behaviour is not guaranteed
74
 * in future.
75
 */
76
77
#ifndef __GTK_DOC_IGNORE__
78
#define g_chmod   chmod
79
0
#define g_open    open
80
#define g_creat   creat
81
0
#define g_rename  rename
82
0
#define g_mkdir   mkdir
83
0
#define g_stat    stat
84
#define g_lstat   lstat
85
#define g_remove  remove
86
0
#define g_fopen   fopen
87
#define g_freopen freopen
88
#define g_fsync   fsync
89
#define g_utime   utime
90
#endif
91
92
GLIB_AVAILABLE_IN_ALL
93
int g_access (const gchar *filename,
94
        int          mode);
95
96
GLIB_AVAILABLE_IN_ALL
97
int g_chdir  (const gchar *path);
98
99
GLIB_AVAILABLE_IN_ALL
100
int g_unlink (const gchar *filename);
101
102
GLIB_AVAILABLE_IN_ALL
103
int g_rmdir  (const gchar *filename);
104
105
#else /* ! G_STDIO_LIBC_DIRECT */
106
107
/* Wrappers for C library functions that take pathname arguments. On
108
 * Unix, the pathname is a file name as it literally is in the file
109
 * system. On well-maintained systems with consistent users who know
110
 * what they are doing and no exchange of files with others this would
111
 * be a well-defined encoding, preferably UTF-8. On Windows, the
112
 * pathname is always in UTF-8, even if that is not the on-disk
113
 * encoding, and not the encoding accepted by the C library or Win32
114
 * API.
115
 */
116
117
GLIB_AVAILABLE_IN_ALL
118
int g_access    (const gchar *filename,
119
     int          mode);
120
121
GLIB_AVAILABLE_IN_ALL
122
int g_chmod     (const gchar *filename,
123
     int          mode);
124
125
GLIB_AVAILABLE_IN_ALL
126
int g_open      (const gchar *filename,
127
                 int          flags,
128
                 int          mode);
129
130
GLIB_AVAILABLE_IN_ALL
131
int g_creat     (const gchar *filename,
132
                 int          mode);
133
134
GLIB_AVAILABLE_IN_ALL
135
int g_rename    (const gchar *oldfilename,
136
                 const gchar *newfilename);
137
138
GLIB_AVAILABLE_IN_ALL
139
int g_mkdir     (const gchar *filename,
140
                 int          mode);
141
142
GLIB_AVAILABLE_IN_ALL
143
int g_chdir     (const gchar *path);
144
145
GLIB_AVAILABLE_IN_ALL
146
int g_stat      (const gchar *filename,
147
                 GStatBuf    *buf);
148
149
GLIB_AVAILABLE_IN_ALL
150
int g_lstat     (const gchar *filename,
151
                 GStatBuf    *buf);
152
153
GLIB_AVAILABLE_IN_ALL
154
int g_unlink    (const gchar *filename);
155
156
GLIB_AVAILABLE_IN_ALL
157
int g_remove    (const gchar *filename);
158
159
GLIB_AVAILABLE_IN_ALL
160
int g_rmdir     (const gchar *filename);
161
162
GLIB_AVAILABLE_IN_ALL
163
FILE *g_fopen   (const gchar *filename,
164
                 const gchar *mode);
165
166
GLIB_AVAILABLE_IN_ALL
167
FILE *g_freopen (const gchar *filename,
168
                 const gchar *mode,
169
                 FILE        *stream);
170
171
GLIB_AVAILABLE_IN_2_64
172
gint g_fsync    (gint fd);
173
174
struct utimbuf;     /* Don't need the real definition of struct utimbuf when just
175
         * including this header.
176
         */
177
178
GLIB_AVAILABLE_IN_ALL
179
int g_utime     (const gchar    *filename,
180
     struct utimbuf *utb);
181
182
#endif /* G_STDIO_LIBC_DIRECT */
183
184
GLIB_AVAILABLE_IN_2_36
185
gboolean g_close (gint       fd,
186
                  GError   **error);
187
188
/**
189
 * g_clear_fd: (skip)
190
 * @fd_ptr: (not optional) (inout) (transfer full): a pointer to a file descriptor
191
 * @error: Used to return an error on failure
192
 *
193
 * If @fd_ptr points to a file descriptor, close it and return
194
 * whether closing it was successful, like g_close().
195
 * If @fd_ptr points to a negative number, return %TRUE without closing
196
 * anything.
197
 * In both cases, set @fd_ptr to `-1` before returning.
198
 *
199
 * Like g_close(), if closing the file descriptor fails, the error is
200
 * stored in both %errno and @error. If this function succeeds,
201
 * %errno is undefined.
202
 *
203
 * On POSIX platforms, this function is async-signal safe
204
 * if @error is %NULL and @fd_ptr points to either a negative number or a
205
 * valid open file descriptor.
206
 * This makes it safe to call from a signal handler or a #GSpawnChildSetupFunc
207
 * under those conditions.
208
 * See [`signal(7)`](man:signal(7)) and
209
 * [`signal-safety(7)`](man:signal-safety(7)) for more details.
210
 *
211
 * It is a programming error for @fd_ptr to point to a non-negative
212
 * number that is not a valid file descriptor.
213
 *
214
 * A typical use of this function is to clean up a file descriptor at
215
 * the end of its scope, whether it has been set successfully or not:
216
 *
217
 * |[
218
 * gboolean
219
 * operate_on_fd (GError **error)
220
 * {
221
 *   gboolean ret = FALSE;
222
 *   int fd = -1;
223
 *
224
 *   fd = open_a_fd (error);
225
 *
226
 *   if (fd < 0)
227
 *     goto out;
228
 *
229
 *   if (!do_something (fd, error))
230
 *     goto out;
231
 *
232
 *   if (!g_clear_fd (&fd, error))
233
 *     goto out;
234
 *
235
 *   ret = TRUE;
236
 *
237
 * out:
238
 *   // OK to call even if fd was never opened or was already closed
239
 *   g_clear_fd (&fd, NULL);
240
 *   return ret;
241
 * }
242
 * ]|
243
 *
244
 * This function is also useful in conjunction with #g_autofd.
245
 *
246
 * Returns: %TRUE on success
247
 * Since: 2.76
248
 */
249
GLIB_AVAILABLE_STATIC_INLINE_IN_2_76
250
static inline gboolean g_clear_fd (int     *fd_ptr,
251
                                   GError **error);
252
253
GLIB_AVAILABLE_STATIC_INLINE_IN_2_76
254
static inline gboolean
255
g_clear_fd (int     *fd_ptr,
256
            GError **error)
257
0
{
258
0
  int fd = *fd_ptr;
259
0
260
0
  *fd_ptr = -1;
261
0
262
0
  if (fd < 0)
263
0
    return TRUE;
264
0
265
0
  /* Suppress "Not available before" warning */
266
0
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
267
0
  return g_close (fd, error);
268
0
  G_GNUC_END_IGNORE_DEPRECATIONS
269
0
}
Unexecuted instantiation: caps.c:g_clear_fd
Unexecuted instantiation: iq.c:g_clear_fd
Unexecuted instantiation: jabber.c:g_clear_fd
Unexecuted instantiation: jingle.c:g_clear_fd
Unexecuted instantiation: content.c:g_clear_fd
Unexecuted instantiation: iceudp.c:g_clear_fd
Unexecuted instantiation: rawudp.c:g_clear_fd
Unexecuted instantiation: session.c:g_clear_fd
Unexecuted instantiation: transport.c:g_clear_fd
Unexecuted instantiation: jutil.c:g_clear_fd
Unexecuted instantiation: message.c:g_clear_fd
Unexecuted instantiation: oob.c:g_clear_fd
Unexecuted instantiation: parser.c:g_clear_fd
Unexecuted instantiation: pep.c:g_clear_fd
Unexecuted instantiation: ping.c:g_clear_fd
Unexecuted instantiation: presence.c:g_clear_fd
Unexecuted instantiation: roster.c:g_clear_fd
Unexecuted instantiation: si.c:g_clear_fd
Unexecuted instantiation: stream_management.c:g_clear_fd
Unexecuted instantiation: useravatar.c:g_clear_fd
Unexecuted instantiation: usermood.c:g_clear_fd
Unexecuted instantiation: usernick.c:g_clear_fd
Unexecuted instantiation: usertune.c:g_clear_fd
Unexecuted instantiation: xdata.c:g_clear_fd
Unexecuted instantiation: adhoccommands.c:g_clear_fd
Unexecuted instantiation: auth.c:g_clear_fd
Unexecuted instantiation: auth_digest_md5.c:g_clear_fd
Unexecuted instantiation: auth_plain.c:g_clear_fd
Unexecuted instantiation: auth_scram.c:g_clear_fd
Unexecuted instantiation: buddy.c:g_clear_fd
Unexecuted instantiation: bosh.c:g_clear_fd
Unexecuted instantiation: chat.c:g_clear_fd
Unexecuted instantiation: data.c:g_clear_fd
Unexecuted instantiation: disco.c:g_clear_fd
Unexecuted instantiation: gmail.c:g_clear_fd
Unexecuted instantiation: google.c:g_clear_fd
Unexecuted instantiation: google_presence.c:g_clear_fd
Unexecuted instantiation: google_roster.c:g_clear_fd
Unexecuted instantiation: jingleinfo.c:g_clear_fd
Unexecuted instantiation: ibb.c:g_clear_fd
Unexecuted instantiation: account.c:g_clear_fd
Unexecuted instantiation: blist.c:g_clear_fd
Unexecuted instantiation: buddyicon.c:g_clear_fd
Unexecuted instantiation: cipher.c:g_clear_fd
Unexecuted instantiation: circbuffer.c:g_clear_fd
Unexecuted instantiation: cmds.c:g_clear_fd
Unexecuted instantiation: connection.c:g_clear_fd
Unexecuted instantiation: conversation.c:g_clear_fd
Unexecuted instantiation: core.c:g_clear_fd
Unexecuted instantiation: debug.c:g_clear_fd
Unexecuted instantiation: eventloop.c:g_clear_fd
Unexecuted instantiation: ft.c:g_clear_fd
Unexecuted instantiation: idle.c:g_clear_fd
Unexecuted instantiation: imgstore.c:g_clear_fd
Unexecuted instantiation: log.c:g_clear_fd
Unexecuted instantiation: network.c:g_clear_fd
Unexecuted instantiation: notify.c:g_clear_fd
Unexecuted instantiation: plugin.c:g_clear_fd
Unexecuted instantiation: pounce.c:g_clear_fd
Unexecuted instantiation: prefs.c:g_clear_fd
Unexecuted instantiation: privacy.c:g_clear_fd
Unexecuted instantiation: proxy.c:g_clear_fd
Unexecuted instantiation: prpl.c:g_clear_fd
Unexecuted instantiation: request.c:g_clear_fd
Unexecuted instantiation: roomlist.c:g_clear_fd
Unexecuted instantiation: savedstatuses.c:g_clear_fd
Unexecuted instantiation: server.c:g_clear_fd
Unexecuted instantiation: signals.c:g_clear_fd
Unexecuted instantiation: smiley.c:g_clear_fd
Unexecuted instantiation: dnsquery.c:g_clear_fd
Unexecuted instantiation: dnssrv.c:g_clear_fd
Unexecuted instantiation: status.c:g_clear_fd
Unexecuted instantiation: stringref.c:g_clear_fd
Unexecuted instantiation: stun.c:g_clear_fd
Unexecuted instantiation: sound.c:g_clear_fd
Unexecuted instantiation: sound-theme-loader.c:g_clear_fd
Unexecuted instantiation: sslconn.c:g_clear_fd
Unexecuted instantiation: theme-loader.c:g_clear_fd
Unexecuted instantiation: theme-manager.c:g_clear_fd
Unexecuted instantiation: upnp.c:g_clear_fd
Unexecuted instantiation: util.c:g_clear_fd
Unexecuted instantiation: value.c:g_clear_fd
Unexecuted instantiation: xmlnode.c:g_clear_fd
Unexecuted instantiation: accountopt.c:g_clear_fd
Unexecuted instantiation: certificate.c:g_clear_fd
Unexecuted instantiation: nat-pmp.c:g_clear_fd
Unexecuted instantiation: ntlm.c:g_clear_fd
Unexecuted instantiation: sound-theme.c:g_clear_fd
Unexecuted instantiation: theme.c:g_clear_fd
270
271
/* g_autofd should be defined on the same compilers where g_autofree is
272
 * This avoids duplicating the feature-detection here. */
273
#ifdef g_autofree
274
#ifndef __GTK_DOC_IGNORE__
275
/* Not public API */
276
static inline void
277
_g_clear_fd_ignore_error (int *fd_ptr)
278
0
{
279
0
  /* Don't overwrite thread-local errno if closing the fd fails */
280
0
  int errsv = errno;
281
0
282
0
  /* Suppress "Not available before" warning */
283
0
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
284
0
285
0
  if (!g_clear_fd (fd_ptr, NULL))
286
0
    {
287
0
      /* Do nothing: we ignore all errors, except for EBADF which
288
0
       * is a programming error, checked for by g_close(). */
289
0
    }
290
0
291
0
  G_GNUC_END_IGNORE_DEPRECATIONS
292
0
293
0
  errno = errsv;
294
0
}
Unexecuted instantiation: caps.c:_g_clear_fd_ignore_error
Unexecuted instantiation: iq.c:_g_clear_fd_ignore_error
Unexecuted instantiation: jabber.c:_g_clear_fd_ignore_error
Unexecuted instantiation: jingle.c:_g_clear_fd_ignore_error
Unexecuted instantiation: content.c:_g_clear_fd_ignore_error
Unexecuted instantiation: iceudp.c:_g_clear_fd_ignore_error
Unexecuted instantiation: rawudp.c:_g_clear_fd_ignore_error
Unexecuted instantiation: session.c:_g_clear_fd_ignore_error
Unexecuted instantiation: transport.c:_g_clear_fd_ignore_error
Unexecuted instantiation: jutil.c:_g_clear_fd_ignore_error
Unexecuted instantiation: message.c:_g_clear_fd_ignore_error
Unexecuted instantiation: oob.c:_g_clear_fd_ignore_error
Unexecuted instantiation: parser.c:_g_clear_fd_ignore_error
Unexecuted instantiation: pep.c:_g_clear_fd_ignore_error
Unexecuted instantiation: ping.c:_g_clear_fd_ignore_error
Unexecuted instantiation: presence.c:_g_clear_fd_ignore_error
Unexecuted instantiation: roster.c:_g_clear_fd_ignore_error
Unexecuted instantiation: si.c:_g_clear_fd_ignore_error
Unexecuted instantiation: stream_management.c:_g_clear_fd_ignore_error
Unexecuted instantiation: useravatar.c:_g_clear_fd_ignore_error
Unexecuted instantiation: usermood.c:_g_clear_fd_ignore_error
Unexecuted instantiation: usernick.c:_g_clear_fd_ignore_error
Unexecuted instantiation: usertune.c:_g_clear_fd_ignore_error
Unexecuted instantiation: xdata.c:_g_clear_fd_ignore_error
Unexecuted instantiation: adhoccommands.c:_g_clear_fd_ignore_error
Unexecuted instantiation: auth.c:_g_clear_fd_ignore_error
Unexecuted instantiation: auth_digest_md5.c:_g_clear_fd_ignore_error
Unexecuted instantiation: auth_plain.c:_g_clear_fd_ignore_error
Unexecuted instantiation: auth_scram.c:_g_clear_fd_ignore_error
Unexecuted instantiation: buddy.c:_g_clear_fd_ignore_error
Unexecuted instantiation: bosh.c:_g_clear_fd_ignore_error
Unexecuted instantiation: chat.c:_g_clear_fd_ignore_error
Unexecuted instantiation: data.c:_g_clear_fd_ignore_error
Unexecuted instantiation: disco.c:_g_clear_fd_ignore_error
Unexecuted instantiation: gmail.c:_g_clear_fd_ignore_error
Unexecuted instantiation: google.c:_g_clear_fd_ignore_error
Unexecuted instantiation: google_presence.c:_g_clear_fd_ignore_error
Unexecuted instantiation: google_roster.c:_g_clear_fd_ignore_error
Unexecuted instantiation: jingleinfo.c:_g_clear_fd_ignore_error
Unexecuted instantiation: ibb.c:_g_clear_fd_ignore_error
Unexecuted instantiation: account.c:_g_clear_fd_ignore_error
Unexecuted instantiation: blist.c:_g_clear_fd_ignore_error
Unexecuted instantiation: buddyicon.c:_g_clear_fd_ignore_error
Unexecuted instantiation: cipher.c:_g_clear_fd_ignore_error
Unexecuted instantiation: circbuffer.c:_g_clear_fd_ignore_error
Unexecuted instantiation: cmds.c:_g_clear_fd_ignore_error
Unexecuted instantiation: connection.c:_g_clear_fd_ignore_error
Unexecuted instantiation: conversation.c:_g_clear_fd_ignore_error
Unexecuted instantiation: core.c:_g_clear_fd_ignore_error
Unexecuted instantiation: debug.c:_g_clear_fd_ignore_error
Unexecuted instantiation: eventloop.c:_g_clear_fd_ignore_error
Unexecuted instantiation: ft.c:_g_clear_fd_ignore_error
Unexecuted instantiation: idle.c:_g_clear_fd_ignore_error
Unexecuted instantiation: imgstore.c:_g_clear_fd_ignore_error
Unexecuted instantiation: log.c:_g_clear_fd_ignore_error
Unexecuted instantiation: network.c:_g_clear_fd_ignore_error
Unexecuted instantiation: notify.c:_g_clear_fd_ignore_error
Unexecuted instantiation: plugin.c:_g_clear_fd_ignore_error
Unexecuted instantiation: pounce.c:_g_clear_fd_ignore_error
Unexecuted instantiation: prefs.c:_g_clear_fd_ignore_error
Unexecuted instantiation: privacy.c:_g_clear_fd_ignore_error
Unexecuted instantiation: proxy.c:_g_clear_fd_ignore_error
Unexecuted instantiation: prpl.c:_g_clear_fd_ignore_error
Unexecuted instantiation: request.c:_g_clear_fd_ignore_error
Unexecuted instantiation: roomlist.c:_g_clear_fd_ignore_error
Unexecuted instantiation: savedstatuses.c:_g_clear_fd_ignore_error
Unexecuted instantiation: server.c:_g_clear_fd_ignore_error
Unexecuted instantiation: signals.c:_g_clear_fd_ignore_error
Unexecuted instantiation: smiley.c:_g_clear_fd_ignore_error
Unexecuted instantiation: dnsquery.c:_g_clear_fd_ignore_error
Unexecuted instantiation: dnssrv.c:_g_clear_fd_ignore_error
Unexecuted instantiation: status.c:_g_clear_fd_ignore_error
Unexecuted instantiation: stringref.c:_g_clear_fd_ignore_error
Unexecuted instantiation: stun.c:_g_clear_fd_ignore_error
Unexecuted instantiation: sound.c:_g_clear_fd_ignore_error
Unexecuted instantiation: sound-theme-loader.c:_g_clear_fd_ignore_error
Unexecuted instantiation: sslconn.c:_g_clear_fd_ignore_error
Unexecuted instantiation: theme-loader.c:_g_clear_fd_ignore_error
Unexecuted instantiation: theme-manager.c:_g_clear_fd_ignore_error
Unexecuted instantiation: upnp.c:_g_clear_fd_ignore_error
Unexecuted instantiation: util.c:_g_clear_fd_ignore_error
Unexecuted instantiation: value.c:_g_clear_fd_ignore_error
Unexecuted instantiation: xmlnode.c:_g_clear_fd_ignore_error
Unexecuted instantiation: accountopt.c:_g_clear_fd_ignore_error
Unexecuted instantiation: certificate.c:_g_clear_fd_ignore_error
Unexecuted instantiation: nat-pmp.c:_g_clear_fd_ignore_error
Unexecuted instantiation: ntlm.c:_g_clear_fd_ignore_error
Unexecuted instantiation: sound-theme.c:_g_clear_fd_ignore_error
Unexecuted instantiation: theme.c:_g_clear_fd_ignore_error
295
#endif
296
297
#define g_autofd _GLIB_CLEANUP(_g_clear_fd_ignore_error) GLIB_AVAILABLE_MACRO_IN_2_76
298
#endif
299
300
G_END_DECLS
301
302
#endif /* __G_STDIO_H__ */