]> mj.ucw.cz Git - libucw.git/blob - ucw/res-fd.c
xtypes: Added FIXME with possible segfault.
[libucw.git] / ucw / res-fd.c
1 /*
2  *      The UCW Library -- Resources for File Descriptors
3  *
4  *      (c) 2008 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/resource.h>
12
13 #include <stdio.h>
14 #include <unistd.h>
15
16 static void
17 fd_res_free(struct resource *r)
18 {
19   close((int)(intptr_t) r->priv);
20 }
21
22 static void
23 fd_res_dump(struct resource *r, uint indent UNUSED)
24 {
25   printf(" fd=%d\n", (int)(intptr_t) r->priv);
26 }
27
28 static const struct res_class fd_res_class = {
29   .name = "fd",
30   .dump = fd_res_dump,
31   .free = fd_res_free,
32 };
33
34 struct resource *
35 res_for_fd(int fd)
36 {
37   return res_new(&fd_res_class, (void*)(intptr_t) fd);
38 }
39
40 #ifdef TEST
41
42 int main(void)
43 {
44   struct respool *rp = rp_new("test", NULL);
45   rp_switch(rp);
46   res_for_fd(1);
47   rp_dump(rp, 0);
48   rp_delete(rp);
49   return 0;
50 }
51
52 #endif