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-2011 Free Software Foundation, Inc.
5 Written by Bruno Haible <bruno@clisp.org>, 2009.
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.
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 General Public License for more details.
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/>. */
23 #include "close-hook.h"
31 /* Currently, this entire code is only needed for the handling of sockets
32 on native Windows platforms. */
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 };
40 execute_close_hooks (int fd, const struct close_hook *remaining_list)
42 if (remaining_list == &anchor)
43 /* End of list reached. */
46 return remaining_list->private_fn (fd, remaining_list->private_next);
50 execute_all_close_hooks (int fd)
52 return execute_close_hooks (fd, anchor.private_next);
56 register_close_hook (close_hook_fn hook, struct close_hook *link)
58 if (link->private_next == NULL && link->private_prev == NULL)
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;
69 /* The link is already in use. */
70 if (link->private_fn != hook)
76 unregister_close_hook (struct close_hook *link)
78 struct close_hook *next = link->private_next;
79 struct close_hook *prev = link->private_prev;
81 if (next != NULL && prev != NULL)
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;