uchar C++ tests: Fix build error on FreeBSD 12.
[gnulib.git] / lib / spawn_faction_addopen.c
bloba3fc22e4b14b5a26d5f8294d97e0730d1a46db81
1 /* Copyright (C) 2000, 2009-2020 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 <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #if !_LIBC
28 # define __sysconf(open_max) getdtablesize ()
29 #endif
31 #if REPLACE_POSIX_SPAWN
32 # include "spawn_int.h"
33 #endif
35 /* Add an action to FILE-ACTIONS which tells the implementation to call
36 'open' for the given file during the 'spawn' call. */
37 int
38 posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *file_actions,
39 int fd, const char *path, int oflag,
40 mode_t mode)
41 #undef posix_spawn_file_actions_addopen
43 int maxfd = __sysconf (_SC_OPEN_MAX);
45 /* Test for the validity of the file descriptor. */
46 if (fd < 0 || fd >= maxfd)
47 return EBADF;
49 #if !REPLACE_POSIX_SPAWN
50 return posix_spawn_file_actions_addopen (file_actions, fd, path, oflag, mode);
51 #else
53 /* Copy PATH, because the caller may free it before calling posix_spawn()
54 or posix_spawnp(). */
55 char *path_copy = strdup (path);
56 if (path_copy == NULL)
57 return ENOMEM;
59 /* Allocate more memory if needed. */
60 if (file_actions->_used == file_actions->_allocated
61 && __posix_spawn_file_actions_realloc (file_actions) != 0)
63 /* This can only mean we ran out of memory. */
64 free (path_copy);
65 return ENOMEM;
69 struct __spawn_action *rec;
71 /* Add the new value. */
72 rec = &file_actions->_actions[file_actions->_used];
73 rec->tag = spawn_do_open;
74 rec->action.open_action.fd = fd;
75 rec->action.open_action.path = path_copy;
76 rec->action.open_action.oflag = oflag;
77 rec->action.open_action.mode = mode;
79 /* Account for the new entry. */
80 ++file_actions->_used;
82 return 0;
85 #endif