exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / spawn_faction_adddup2.c
blob5660a6a7732dba1a17f1a5d0b7ab559ff3299fa3
1 /* Copyright (C) 2000, 2009-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <spawn.h>
22 #include <errno.h>
23 #include <unistd.h>
25 #if !_LIBC
26 # define __sysconf(open_max) getdtablesize ()
27 #endif
29 #if REPLACE_POSIX_SPAWN
30 # include "spawn_int.h"
31 #endif
33 /* Add an action to FILE-ACTIONS which tells the implementation to call
34 'dup2' for the given file descriptors during the 'spawn' call. */
35 int
36 posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *file_actions,
37 int fd, int newfd)
38 #undef posix_spawn_file_actions_adddup2
40 int maxfd = __sysconf (_SC_OPEN_MAX);
42 /* Test for the validity of the file descriptor. */
43 if (fd < 0 || newfd < 0 || fd >= maxfd || newfd >= maxfd)
44 return EBADF;
46 #if !REPLACE_POSIX_SPAWN
47 return posix_spawn_file_actions_adddup2 (file_actions, fd, newfd);
48 #else
49 /* Allocate more memory if needed. */
50 if (file_actions->_used == file_actions->_allocated
51 && __posix_spawn_file_actions_realloc (file_actions) != 0)
52 /* This can only mean we ran out of memory. */
53 return ENOMEM;
56 struct __spawn_action *rec;
58 /* Add the new value. */
59 rec = &file_actions->_actions[file_actions->_used];
60 rec->tag = spawn_do_dup2;
61 rec->action.dup2_action.fd = fd;
62 rec->action.dup2_action.newfd = newfd;
64 /* Account for the new entry. */
65 ++file_actions->_used;
67 return 0;
69 #endif