Import upstream version 1.27
[debian/tar] / gnu / closedir.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Stop reading the entries of a directory.
4    Copyright (C) 2006-2013 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <dirent.h>
23
24 #if REPLACE_FCHDIR
25 # include <unistd.h>
26 #endif
27
28 #if HAVE_CLOSEDIR
29
30 /* Override closedir(), to keep track of the open file descriptors.
31    Needed because there is a function dirfd().  */
32
33 #else
34
35 # include <stdlib.h>
36
37 # include "dirent-private.h"
38
39 #endif
40
41 int
42 closedir (DIR *dirp)
43 {
44 # if REPLACE_FCHDIR
45   int fd = dirfd (dirp);
46 # endif
47   int retval;
48
49 #if HAVE_CLOSEDIR
50 # undef closedir
51
52   retval = closedir (dirp);
53
54 #else
55
56   if (dirp->current != INVALID_HANDLE_VALUE)
57     FindClose (dirp->current);
58   free (dirp);
59
60   retval = 0;
61
62 #endif
63
64 #if REPLACE_FCHDIR
65   if (retval >= 0)
66     _gl_unregister_fd (fd);
67 #endif
68   return retval;
69 }