Blame gettext-tools/gnulib-lib/closedir.c

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