Use an all-permissive copyright notice, recommended by RMS.
[gnulib.git] / m4 / readdir.m4
1 #serial 8
2
3 # Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003 Free Software
4 # Foundation, Inc.
5 #
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9
10 dnl SunOS's readdir is broken in such a way that rm.c has to add extra code
11 dnl to test whether a NULL return value really means there are no more files
12 dnl in the directory.
13 dnl
14 dnl And the rm from coreutils-5.0 exposes a similar problem when there
15 dnl are 338 or more files in a directory on a Darwin-6.5 system
16 dnl
17 dnl Detect the problem by creating a directory containing 500 files (254 not
18 dnl counting . and .. is the minimum for SunOS, 338 for Darwin) and see
19 dnl if a loop doing `readdir; unlink' removes all of them.
20 dnl
21 dnl Define HAVE_WORKING_READDIR if readdir does *not* have this problem.
22
23 dnl Written by Jim Meyering.
24
25 AC_DEFUN([GL_FUNC_READDIR],
26 [dnl
27 AC_REQUIRE([AC_HEADER_DIRENT])
28 AC_CACHE_CHECK([for working readdir], gl_cv_func_working_readdir,
29   [dnl
30   # Arrange for deletion of the temporary directory this test creates, in
31   # case the test itself fails to delete everything -- as happens on Sunos.
32   ac_clean_files="$ac_clean_files conf-dir"
33
34   AC_TRY_RUN(
35 [#   include <stdio.h>
36 #   include <sys/types.h>
37 #   include <string.h>
38
39 #   ifdef HAVE_DIRENT_H
40 #    include <dirent.h>
41 #    define NLENGTH(direct) (strlen((direct)->d_name))
42 #   else /* not HAVE_DIRENT_H */
43 #    define dirent direct
44 #    define NLENGTH(direct) ((direct)->d_namlen)
45 #    ifdef HAVE_SYS_NDIR_H
46 #     include <sys/ndir.h>
47 #    endif /* HAVE_SYS_NDIR_H */
48 #    ifdef HAVE_SYS_DIR_H
49 #     include <sys/dir.h>
50 #    endif /* HAVE_SYS_DIR_H */
51 #    ifdef HAVE_NDIR_H
52 #     include <ndir.h>
53 #    endif /* HAVE_NDIR_H */
54 #   endif /* HAVE_DIRENT_H */
55
56 #   define DOT_OR_DOTDOT(Basename) \
57      (Basename[0] == '.' && (Basename[1] == '\0' \
58                              || (Basename[1] == '.' && Basename[2] == '\0')))
59
60 /* Don't try to use replacement mkdir; it wouldn't resolve at link time.  */
61 #   undef mkdir
62
63     static void
64     create_N_file_dir (const char *dir, size_t n_files)
65     {
66       unsigned int i;
67
68       if (mkdir (dir, 0700))
69         abort ();
70       if (chdir (dir))
71         abort ();
72
73       for (i = 0; i < n_files; i++)
74         {
75           char file_name[4];
76           FILE *out;
77
78           sprintf (file_name, "%03d", i);
79           out = fopen (file_name, "w");
80           if (!out)
81             abort ();
82           if (fclose (out) == EOF)
83             abort ();
84         }
85
86       if (chdir (".."))
87         abort ();
88     }
89
90     static void
91     remove_dir (const char *dir)
92     {
93       DIR *dirp;
94
95       if (chdir (dir))
96         abort ();
97
98       dirp = opendir (".");
99       if (dirp == NULL)
100         abort ();
101
102       while (1)
103         {
104           struct dirent *dp = readdir (dirp);
105           if (dp == NULL)
106             break;
107
108           if (DOT_OR_DOTDOT (dp->d_name))
109             continue;
110
111           if (unlink (dp->d_name))
112             abort ();
113         }
114       closedir (dirp);
115
116       if (chdir (".."))
117         abort ();
118
119       if (rmdir (dir))
120         exit (1);
121     }
122
123     int
124     main ()
125     {
126       const char *dir = "conf-dir";
127       create_N_file_dir (dir, 500);
128       remove_dir (dir);
129       exit (0);
130     }],
131   gl_cv_func_working_readdir=yes,
132   gl_cv_func_working_readdir=no,
133   gl_cv_func_working_readdir=no)])
134
135   if test $gl_cv_func_working_readdir = yes; then
136     AC_DEFINE(HAVE_WORKING_READDIR, 1,
137       [Define if readdir is found to work properly in some unusual cases. ])
138   fi
139 ])