vma-iter: Add support for Android.
[gnulib.git] / lib / spawn_faction_addopen.c
blob6b4422fd127554afc8ac876fc8e59cf448b2eae4
1 /* Copyright (C) 2000, 2009-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU 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 'open' for the given file during the 'spawn' call. */
35 int
36 posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *file_actions,
37 int fd, const char *path, int oflag,
38 mode_t mode)
39 #undef posix_spawn_file_actions_addopen
41 int maxfd = __sysconf (_SC_OPEN_MAX);
43 /* Test for the validity of the file descriptor. */
44 if (fd < 0 || fd >= maxfd)
45 return EBADF;
47 #if !REPLACE_POSIX_SPAWN
48 return posix_spawn_file_actions_addopen (file_actions, fd, path, oflag, mode);
49 #else
50 /* Allocate more memory if needed. */
51 if (file_actions->_used == file_actions->_allocated
52 && __posix_spawn_file_actions_realloc (file_actions) != 0)
53 /* This can only mean we ran out of memory. */
54 return ENOMEM;
57 struct __spawn_action *rec;
59 /* Add the new value. */
60 rec = &file_actions->_actions[file_actions->_used];
61 rec->tag = spawn_do_open;
62 rec->action.open_action.fd = fd;
63 rec->action.open_action.path = path;
64 rec->action.open_action.oflag = oflag;
65 rec->action.open_action.mode = mode;
67 /* Account for the new entry. */
68 ++file_actions->_used;
70 return 0;
72 #endif