Blame lib/closedir.c

Packit 1ac44c
/* Stop reading the entries of a directory.
Packit 1ac44c
   Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 1ac44c
Packit 1ac44c
   This program is free software: you can redistribute it and/or modify
Packit 1ac44c
   it under the terms of the GNU General Public License as published by
Packit 1ac44c
   the Free Software Foundation; either version 3 of the License, or
Packit 1ac44c
   (at your option) any later version.
Packit 1ac44c
Packit 1ac44c
   This program is distributed in the hope that it will be useful,
Packit 1ac44c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1ac44c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1ac44c
   GNU General Public License for more details.
Packit 1ac44c
Packit 1ac44c
   You should have received a copy of the GNU General Public License
Packit 1ac44c
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 1ac44c
Packit 1ac44c
#include <config.h>
Packit 1ac44c
Packit 1ac44c
/* Specification.  */
Packit 1ac44c
#include <dirent.h>
Packit 1ac44c
Packit 1ac44c
#if REPLACE_FCHDIR
Packit 1ac44c
# include <unistd.h>
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
#if HAVE_CLOSEDIR
Packit 1ac44c
Packit 1ac44c
/* Override closedir(), to keep track of the open file descriptors.
Packit 1ac44c
   Needed because there is a function dirfd().  */
Packit 1ac44c
Packit 1ac44c
#else
Packit 1ac44c
Packit 1ac44c
# include <stdlib.h>
Packit 1ac44c
Packit 1ac44c
# include "dirent-private.h"
Packit 1ac44c
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
int
Packit 1ac44c
closedir (DIR *dirp)
Packit 1ac44c
{
Packit 1ac44c
# if REPLACE_FCHDIR || REPLACE_DIRFD
Packit 1ac44c
  int fd = dirfd (dirp);
Packit 1ac44c
# endif
Packit 1ac44c
  int retval;
Packit 1ac44c
Packit 1ac44c
#if HAVE_CLOSEDIR
Packit 1ac44c
# undef closedir
Packit 1ac44c
Packit 1ac44c
  retval = closedir (dirp);
Packit 1ac44c
Packit 1ac44c
# ifdef __KLIBC__
Packit 1ac44c
  if (!retval)
Packit 1ac44c
    _gl_unregister_dirp_fd (fd);
Packit 1ac44c
# endif
Packit 1ac44c
#else
Packit 1ac44c
Packit 1ac44c
  if (dirp->current != INVALID_HANDLE_VALUE)
Packit 1ac44c
    FindClose (dirp->current);
Packit 1ac44c
  free (dirp);
Packit 1ac44c
Packit 1ac44c
  retval = 0;
Packit 1ac44c
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
#if REPLACE_FCHDIR
Packit 1ac44c
  if (retval >= 0)
Packit 1ac44c
    _gl_unregister_fd (fd);
Packit 1ac44c
#endif
Packit 1ac44c
  return retval;
Packit 1ac44c
}