3 * Duplicate multiple files
5 * (c) 2008 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Data structures ---------------------------------------------------*/
37 typedef struct mdup_fd {
38 int cur; /* Current file descriptor */
39 int want; /* File descriptor wanted */
42 /*----- Functions provided ------------------------------------------------*/
46 * Arguments: @mdup_fd *v@ = pointer to @mdup_fd@ vector
47 * @size_t n@ = size of vector
49 * Returns: Zero if successful, @-1@ on failure.
51 * Use: Rearranges file descriptors.
53 * The vector @v@ consists of a number of @mdup_fd@ structures.
54 * Each `slot' in the table represents a file. The slot's @cur@
55 * member names the current file descriptor for this file; the
56 * @want@ member is the file descriptor we want to use for it.
57 * if you want to keep a file alive but don't care which
58 * descriptor it ends up with, set @want = -1@. Several slots
59 * may specify the same @cur@ descriptor; but they all have to
60 * declare different @want@s (except that several slots may have
63 * On successful exit, the function will have rearranged the
64 * file descriptors as requested. To reflect this, the @cur@
65 * members will all be set to match the (non-@-1@) @want@
68 * If there is a failure, then some rearrangement may have been
69 * performed and some not; the @cur@ members are set to reflect
70 * which file descriptors are to be used. The old file
71 * descriptors are closed. (This is different from usual @dup@
72 * behaviour, of course, but essential for reliable error
73 * handling.) If you want to keep a particular source file
74 * descriptor open as well as make a new copy then specify two
75 * slots with the same @cur@, one with @want = cur@ and one with
76 * the desired output descriptor.
78 * This function works correctly even if the desired remappings
82 extern int mdup(mdup_fd */*v*/, size_t /*n*/);
84 /*----- That's all, folks -------------------------------------------------*/