Blame lib/closedir.c

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