Use Gnulib ‘dup2’ module

This simplifies code that would otherwise use dup and close.
* gnulib.modules: Add dup2.
* src/system.c: Add #pragma to pacify GCC 13.
(xdup2): Simplify by using dup2.
This commit is contained in:
Paul Eggert
2023-06-16 16:34:19 -07:00
parent 5d6736e394
commit 4c7a3798d8
2 changed files with 8 additions and 14 deletions

View File

@@ -28,6 +28,7 @@ backupfile
closeout
configmake
dirname
dup2
error
exclude
extern-inline

View File

@@ -299,6 +299,11 @@ sys_write_archive_buffer (void)
#define PREAD 0 /* read file descriptor from pipe() */
#define PWRITE 1 /* write file descriptor from pipe() */
/* Work around GCC bug 109839. */
#if 13 <= __GNUC__
# pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
#endif
/* Duplicate file descriptor FROM into becoming INTO.
INTO is closed first and has to be the next available slot. */
static void
@@ -306,22 +311,10 @@ xdup2 (int from, int into)
{
if (from != into)
{
int status = close (into);
if (status != 0 && errno != EBADF)
if (dup2 (from, into) < 0)
{
int e = errno;
FATAL_ERROR ((0, e, _("Cannot close")));
}
status = dup (from);
if (status != into)
{
if (status < 0)
{
int e = errno;
FATAL_ERROR ((0, e, _("Cannot dup")));
}
abort ();
FATAL_ERROR ((0, e, _("Cannot dup2")));
}
xclose (from);
}