| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Duplicate multiple files |
| 4 | * |
| 5 | * (c) 2008 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef MDUP_H |
| 29 | #define MDUP_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Data structures ---------------------------------------------------*/ |
| 36 | |
| 37 | typedef struct mdup_fd { |
| 38 | int cur; /* Current file descriptor */ |
| 39 | int want; /* File descriptor wanted */ |
| 40 | } mdup_fd; |
| 41 | |
| 42 | /*----- Functions provided ------------------------------------------------*/ |
| 43 | |
| 44 | /* --- @mdup@ --- * |
| 45 | * |
| 46 | * Arguments: @mdup_fd *v@ = pointer to @mdup_fd@ vector |
| 47 | * @size_t n@ = size of vector |
| 48 | * |
| 49 | * Returns: Zero if successful, @-1@ on failure. |
| 50 | * |
| 51 | * Use: Rearranges file descriptors. |
| 52 | * |
| 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 |
| 61 | * @want = -1@. |
| 62 | * |
| 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@ |
| 66 | * members. |
| 67 | * |
| 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. |
| 77 | * |
| 78 | * This function works correctly even if the desired remappings |
| 79 | * contain cycles. |
| 80 | */ |
| 81 | |
| 82 | extern int mdup(mdup_fd */*v*/, size_t /*n*/); |
| 83 | |
| 84 | /*----- That's all, folks -------------------------------------------------*/ |
| 85 | |
| 86 | #ifdef __cplusplus |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | #endif |