aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/bison/lib/fd-safer-flag.c
diff options
context:
space:
mode:
authorthegeorg <thegeorg@yandex-team.com>2024-05-18 00:46:52 +0300
committerthegeorg <thegeorg@yandex-team.com>2024-05-18 00:57:03 +0300
commit144a9c52e58b6386dd6929c1d95a11d04bc8eaa5 (patch)
treeb633d50f211b16604dc3aed9ad4fc7f563ebc7b2 /contrib/tools/bison/lib/fd-safer-flag.c
parent6bb04f40e291c6fc3ac8e556d561d59eed0a49b9 (diff)
downloadydb-144a9c52e58b6386dd6929c1d95a11d04bc8eaa5.tar.gz
Flatten bison/gnulib/src to bison/lib to match upstream layout
2f47b5eab59c8031971bd995b703c854fd6f106d
Diffstat (limited to 'contrib/tools/bison/lib/fd-safer-flag.c')
-rw-r--r--contrib/tools/bison/lib/fd-safer-flag.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/contrib/tools/bison/lib/fd-safer-flag.c b/contrib/tools/bison/lib/fd-safer-flag.c
new file mode 100644
index 0000000000..8bc14bda2c
--- /dev/null
+++ b/contrib/tools/bison/lib/fd-safer-flag.c
@@ -0,0 +1,52 @@
+/* Adjust a file descriptor result so that it avoids clobbering
+ STD{IN,OUT,ERR}_FILENO, with specific flags.
+
+ Copyright (C) 2005-2006, 2009-2013 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert and Eric Blake. */
+
+#include <config.h>
+
+/* Specification. */
+#include "unistd-safer.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+/* Return FD, unless FD would be a copy of standard input, output, or
+ error; in that case, return a duplicate of FD, closing FD. If FLAG
+ contains O_CLOEXEC, the returned FD will have close-on-exec
+ semantics. On failure to duplicate, close FD, set errno, and
+ return -1. Preserve errno if FD is negative, so that the caller
+ can always inspect errno when the returned value is negative.
+
+ This function is usefully wrapped around functions that return file
+ descriptors, e.g., fd_safer_flag (open ("file", O_RDONLY | flag), flag). */
+
+int
+fd_safer_flag (int fd, int flag)
+{
+ if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
+ {
+ int f = dup_safer_flag (fd, flag);
+ int e = errno;
+ close (fd);
+ errno = e;
+ fd = f;
+ }
+
+ return fd;
+}