exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / spawn_faction_addfchdir.c
blobd63ae06b3f570b08dcda7ddee2521fc329e3c642
1 /* Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is free software: you can redistribute it and/or modify
4 it under the terms of the GNU Lesser General Public License as
5 published by the Free Software Foundation; either version 2.1 of the
6 License, or (at your option) any later version.
8 This file is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16 #include <config.h>
18 /* Specification. */
19 #include <spawn.h>
21 #include <errno.h>
22 #include <unistd.h>
24 #if !_LIBC
25 # define __sysconf(open_max) getdtablesize ()
26 #endif
28 #if REPLACE_POSIX_SPAWN
29 # include "spawn_int.h"
30 #endif
32 /* Add an action to FILE-ACTIONS which tells the implementation to call
33 'fchdir' to the given directory during the 'spawn' call. */
34 int
35 posix_spawn_file_actions_addfchdir (posix_spawn_file_actions_t *file_actions,
36 int fd)
37 #undef posix_spawn_file_actions_addfchdir
39 int maxfd = __sysconf (_SC_OPEN_MAX);
41 /* Test for the validity of the file descriptor. */
42 if (fd < 0 || fd >= maxfd)
43 return EBADF;
45 #if !REPLACE_POSIX_SPAWN
46 return posix_spawn_file_actions_addfchdir_np (file_actions, fd);
47 #else
48 /* Allocate more memory if needed. */
49 if (file_actions->_used == file_actions->_allocated
50 && __posix_spawn_file_actions_realloc (file_actions) != 0)
51 /* This can only mean we ran out of memory. */
52 return ENOMEM;
55 struct __spawn_action *rec;
57 /* Add the new value. */
58 rec = &file_actions->_actions[file_actions->_used];
59 rec->tag = spawn_do_fchdir;
60 rec->action.fchdir_action.fd = fd;
62 /* Account for the new entry. */
63 ++file_actions->_used;
65 return 0;
67 #endif