Provide a `--pidfile' option in `fw'.
[fwd] / reffd.c
1 /* -*-c-*-
2 *
3 * $Id: reffd.c,v 1.2 2004/04/08 01:36:25 mdw Exp $
4 *
5 * Reference counted file descriptors
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Header files ------------------------------------------------------*/
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <sys/types.h>
36 #include <unistd.h>
37
38 #include <mLib/sub.h>
39
40 #include "reffd.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 /* --- @reffd_init@ --- *
45 *
46 * Arguments: @int fd@ = file descriptor
47 *
48 * Returns: Reference-counted file descriptor object.
49 *
50 * Use: Creates a refcounted file descriptor.
51 */
52
53 reffd *reffd_init(int fd)
54 {
55 reffd *r = CREATE(reffd);
56 r->fd = fd;
57 r->ref = 1;
58 r->proc = 0;
59 r->p = 0;
60 return (r);
61 }
62
63 /* --- @reffd_handler@ --- *
64 *
65 * Arguments: @reffd *r@ = pointer to reference counted filehandle
66 * @void (*proc)(void *p)@ = procedure to call
67 * @void *p@
68 *
69 * Returns: ---
70 *
71 * Use: Sets the reference counted file descriptor to call @proc@
72 * when it is no longer required.
73 */
74
75 void reffd_handler(reffd *r, void (*proc)(void *p), void *p)
76 {
77 r->proc = proc;
78 r->p = p;
79 }
80
81 /* --- @reffd_inc@ --- *
82 *
83 * Arguments: @reffd *r@ = pointer to reference counted filehandle
84 *
85 * Returns: ---
86 *
87 * Use: Increments the reference count for a file descriptor.
88 */
89
90 void reffd_inc(reffd *r) { REFFD_INC(r); }
91
92 /* --- @reffd_dec@ --- *
93 *
94 * Arguments: @reffd *r@ = pointer to reference counted filehandle
95 *
96 * Returns: ---
97 *
98 * Use: Decrements the reference count for a file descriptor.
99 */
100
101 void reffd_dec(reffd *r) { REFFD_DEC(r); }
102
103 /*----- That's all, folks -------------------------------------------------*/