(root)/
coreutils-9.4/
gnulib-tests/
test-dirfd.c
       1  /* Test of dirfd() function.
       2     Copyright (C) 2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Bruno Haible <bruno@clisp.org>, 2023.  */
      18  
      19  #include <config.h>
      20  
      21  #include <dirent.h>
      22  
      23  #include <stdio.h>
      24  
      25  #include "macros.h"
      26  
      27  int
      28  main ()
      29  {
      30  #if defined _WIN32 && !defined __CYGWIN__
      31    fprintf (stderr, "Skipping test: The DIR type does not contain a file descriptor.\n");
      32    return 77;
      33  #else
      34    /* On all other platforms, we expect to have either
      35         - a dirfd() function, or
      36         - a dirfd macro, or
      37         - a DIR struct with a d_fd member, or
      38         - a DIR struct with a dd_fd member.
      39       If we don't have this, dirfd.c produces a function that always returns -1.
      40       Check here that this does not happen.  */
      41    DIR *d = opendir (".");
      42    int fd = dirfd (d);
      43    ASSERT (fd >= 0);
      44  
      45    return 0;
      46  #endif
      47  }