]> git.cworth.org Git - tar/blob - gnu/close-hook.c
upstream: Fix extraction of device nodes.
[tar] / gnu / close-hook.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Hook for making the close() function extensible.
4    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2009.
6
7    This program is free software: you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "close-hook.h"
24
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #undef close
29
30
31 /* Currently, this entire code is only needed for the handling of sockets
32    on native Windows platforms.  */
33 #if WINDOWS_SOCKETS
34
35 /* The first and last link in the doubly linked list.
36    Initially the list is empty.  */
37 static struct close_hook anchor = { &anchor, &anchor, NULL };
38
39 int
40 execute_close_hooks (int fd, const struct close_hook *remaining_list)
41 {
42   if (remaining_list == &anchor)
43     /* End of list reached.  */
44     return close (fd);
45   else
46     return remaining_list->private_fn (fd, remaining_list->private_next);
47 }
48
49 int
50 execute_all_close_hooks (int fd)
51 {
52   return execute_close_hooks (fd, anchor.private_next);
53 }
54
55 void
56 register_close_hook (close_hook_fn hook, struct close_hook *link)
57 {
58   if (link->private_next == NULL && link->private_prev == NULL)
59     {
60       /* Add the link to the doubly linked list.  */
61       link->private_next = anchor.private_next;
62       link->private_prev = &anchor;
63       link->private_fn = hook;
64       anchor.private_next->private_prev = link;
65       anchor.private_next = link;
66     }
67   else
68     {
69       /* The link is already in use.  */
70       if (link->private_fn != hook)
71         abort ();
72     }
73 }
74
75 void
76 unregister_close_hook (struct close_hook *link)
77 {
78   struct close_hook *next = link->private_next;
79   struct close_hook *prev = link->private_prev;
80
81   if (next != NULL && prev != NULL)
82     {
83       /* The link is in use.  Remove it from the doubly linked list.  */
84       prev->private_next = next;
85       next->private_prev = prev;
86       /* Clear the link, to mark it unused.  */
87       link->private_next = NULL;
88       link->private_prev = NULL;
89       link->private_fn = NULL;
90     }
91 }
92
93 #endif