chiark / gitweb /
bres: Use mdup to duplicate file descriptors for the child.
[mLib] / mdup.h
1 /* -*-c-*-
2  *
3  * Duplicate multiple files
4  *
5  * (c) 2008 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24
25 #ifndef MDUP_H
26 #define MDUP_H
27
28 #ifdef __cplusplus
29   extern "C" {
30 #endif
31
32 /*----- Data structures ---------------------------------------------------*/
33
34 typedef struct mdup_fd {
35   int cur;                              /* Current file descriptor */
36   int want;                             /* File descriptor wanted */
37 } mdup_fd;
38
39 /*----- Functions provided ------------------------------------------------*/
40
41 /* --- @mdup@ --- *
42  *
43  * Arguments:   @mdup_fd *v@ = pointer to @mdup_fd@ vector
44  *              @size_t n@ = size of vector
45  *
46  * Returns:     Zero if successful, @-1@ on failure.
47  *
48  * Use:         Rearranges file descriptors.
49  *
50  *              The vector @v@ consists of a number of @mdup_fd@ structures.
51  *              Each `slot' in the table represents a file.  The slot's @cur@
52  *              member names the current file descriptor for this file; the
53  *              @want@ member is the file descriptor we want to use for it.
54  *              if you want to keep a file alive but don't care which
55  *              descriptor it ends up with, set @want = -1@.  Several slots
56  *              may specify the same @cur@ descriptor; but they all have to
57  *              declare different @want@s (except that several slots may have
58  *              @want = -1@.
59  *
60  *              On successful exit, the function will have rearranged the
61  *              file descriptors as requested.  To reflect this, the @cur@
62  *              members will all be set to match the (non-@-1@) @want@
63  *              members.
64  *
65  *              If there is a failure, then some rearrangement may have been
66  *              performed and some not; the @cur@ members are set to reflect
67  *              which file descriptors are to be used.  The old file
68  *              descriptors are closed.  (This is different from usual @dup@
69  *              behaviour, of course, but essential for reliable error
70  *              handling.)  If you want to keep a particular source file
71  *              descriptor open as well as make a new copy then specify two
72  *              slots with the same @cur@, one with @want = cur@ and one with
73  *              the desired output descriptor.
74  *
75  *              This function works correctly even if the desired remappings
76  *              contain cycles.
77  */
78
79 extern int mdup(mdup_fd */*v*/, size_t /*n*/);
80
81 /*----- That's all, folks -------------------------------------------------*/
82
83 #ifdef __cplusplus
84   }
85 #endif
86
87 #endif