Index: newlib/libc/posix/posix_spawn_file_actions.c |
diff --git a/newlib/libc/posix/posix_spawn_file_actions.c b/newlib/libc/posix/posix_spawn_file_actions.c |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bfa7ce02cef6a79b741605e3acbd36709ea77d33 |
--- /dev/null |
+++ b/newlib/libc/posix/posix_spawn_file_actions.c |
@@ -0,0 +1,158 @@ |
+/*- |
+ * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> |
+ * All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions |
+ * are met: |
+ * 1. Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * 2. Redistributions in binary form must reproduce the above copyright |
+ * notice, this list of conditions and the following disclaimer in the |
+ * documentation and/or other materials provided with the distribution. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
+ * SUCH DAMAGE. |
+ */ |
+ |
+#ifndef _NO_POSIX_SPAWN |
+ |
+#include <sys/cdefs.h> |
+ |
+#include <errno.h> |
+#include <spawn.h> |
+#include <stdlib.h> |
+#include <string.h> |
+ |
+/* |
+ * File descriptor actions |
+ */ |
+ |
+int |
+_DEFUN(posix_spawn_file_actions_init, (ret), |
+ posix_spawn_file_actions_t *ret) |
+{ |
+ posix_spawn_file_actions_t* fa; |
+ |
+ fa = malloc(sizeof(posix_spawn_file_actions_t)); |
+ if (fa == NULL) |
+ return (-1); |
+ |
+ STAILQ_INIT(&fa->fa_list); |
+ ret = fa; |
+ return (0); |
+} |
+ |
+int |
+_DEFUN(posix_spawn_file_actions_destroy, (fa), |
+ posix_spawn_file_actions_t *fa) |
+{ |
+ posix_spawn_file_actions_entry_t *fae; |
+ |
+ while ((fae = STAILQ_FIRST(&fa->fa_list)) != NULL) { |
+ /* Remove file action entry from the queue */ |
+ STAILQ_REMOVE_HEAD(&fa->fa_list, fae_list); |
+ |
+ /* Deallocate file action entry */ |
+ if (fae->fae_action == FAE_OPEN) |
+ free(fae->fae_path); |
+ free(fae); |
+ } |
+ |
+ free(fa); |
+ return (0); |
+} |
+ |
+int |
+_DEFUN(posix_spawn_file_actions_addopen, (fa, fildes, path, oflag, mode), |
+ posix_spawn_file_actions_t * __restrict fa _AND |
+ int fildes _AND |
+ _CONST char * __restrict path _AND |
+ int oflag _AND |
+ mode_t mode) |
+{ |
+ posix_spawn_file_actions_entry_t *fae; |
+ int error; |
+ |
+ if (fildes < 0) |
+ return (EBADF); |
+ |
+ /* Allocate object */ |
+ fae = malloc(sizeof(posix_spawn_file_actions_entry_t)); |
+ if (fae == NULL) |
+ return (errno); |
+ |
+ /* Set values and store in queue */ |
+ fae->fae_action = FAE_OPEN; |
+ fae->fae_path = strdup(path); |
+ if (fae->fae_path == NULL) { |
+ error = errno; |
+ free(fae); |
+ return (error); |
+ } |
+ fae->fae_fildes = fildes; |
+ fae->fae_oflag = oflag; |
+ fae->fae_mode = mode; |
+ |
+ STAILQ_INSERT_TAIL(&fa->fa_list, fae, fae_list); |
+ return (0); |
+} |
+ |
+int |
+_DEFUN(posix_spawn_file_actions_adddup2, (fa, fildes, newfildes), |
+ posix_spawn_file_actions_t *fa _AND |
+ int fildes _AND |
+ int newfildes) |
+{ |
+ posix_spawn_file_actions_entry_t *fae; |
+ |
+ if (fildes < 0 || newfildes < 0) |
+ return (EBADF); |
+ |
+ /* Allocate object */ |
+ fae = malloc(sizeof(posix_spawn_file_actions_entry_t)); |
+ if (fae == NULL) |
+ return (errno); |
+ |
+ /* Set values and store in queue */ |
+ fae->fae_action = FAE_DUP2; |
+ fae->fae_fildes = fildes; |
+ fae->fae_newfildes = newfildes; |
+ |
+ STAILQ_INSERT_TAIL(&fa->fa_list, fae, fae_list); |
+ return (0); |
+} |
+ |
+int |
+_DEFUN(posix_spawn_file_actions_addclose, (fa, fildes), |
+ posix_spawn_file_actions_t *fa _AND |
+ int fildes) |
+{ |
+ posix_spawn_file_actions_entry_t *fae; |
+ |
+ if (fildes < 0) |
+ return (EBADF); |
+ |
+ /* Allocate object */ |
+ fae = malloc(sizeof(posix_spawn_file_actions_entry_t)); |
+ if (fae == NULL) |
+ return (errno); |
+ |
+ /* Set values and store in queue */ |
+ fae->fae_action = FAE_CLOSE; |
+ fae->fae_fildes = fildes; |
+ |
+ STAILQ_INSERT_TAIL(&fa->fa_list, fae, fae_list); |
+ return (0); |
+} |
+ |
+#endif /* !_NO_POSIX_SPAWN */ |