Debian Codegraph @

src/dex-fd.c

raw symbols SCIP
1 /* dex-fd.c
2 *
3 * Copyright 2024 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #include "dex-fd-private.h"
28
29 #ifdef G_OS_WIN32
30 #include <io.h>
31 #endif
32
33 int
34 dex_fd_peek (const DexFD *fd)
35 {
36 if (fd == NULL)
37 return -1;
38
39 return *(int *)fd;
40 }
41
42 int
43 dex_fd_steal (DexFD *fd)
44 {
45 if (fd == NULL)
46 return -1;
47
48 return g_steal_fd ((int *)fd);
49 }
50
51 void
52 dex_fd_free (DexFD *fd)
53 {
54 int real = dex_fd_steal (fd);
55 if (real != -1)
56 close (real);
57 g_free (fd);
58 }
59
60 DexFD *
61 dex_fd_dup (const DexFD *fd)
62 {
63 int real = dex_fd_peek (fd);
64
65 if (real == -1)
66 return NULL;
67
68 real = dup (real);
69
70 return g_memdup2 (&real, sizeof real);
71 }
72
73 G_DEFINE_BOXED_TYPE (DexFD, dex_fd, dex_fd_dup, dex_fd_free)