.
[gnulib.git] / m4 / readdir.m4
1 #serial 1
2
3 dnl FIXME: describe
4
5 AC_DEFUN(jm_FUNC_READDIR,
6 [dnl
7 AC_REQUIRE([AC_HEADER_DIRENT])
8 AC_CHECK_HEADERS(string.h)
9 AC_CACHE_CHECK([for working readdir], jm_cv_func_working_readdir,
10   [AC_TRY_RUN(
11   changequote(<<, >>)dnl
12   <<
13 #   include <stdio.h>
14 #   include <sys/types.h>
15 #   if HAVE_STRING_H
16 #    include <string.h>
17 #   endif
18
19 #   ifdef HAVE_DIRENT_H
20 #    include <dirent.h>
21 #    define NLENGTH(direct) (strlen((direct)->d_name))
22 #   else /* not HAVE_DIRENT_H */
23 #    define dirent direct
24 #    define NLENGTH(direct) ((direct)->d_namlen)
25 #    ifdef HAVE_SYS_NDIR_H
26 #     include <sys/ndir.h>
27 #    endif /* HAVE_SYS_NDIR_H */
28 #    ifdef HAVE_SYS_DIR_H
29 #     include <sys/dir.h>
30 #    endif /* HAVE_SYS_DIR_H */
31 #    ifdef HAVE_NDIR_H
32 #     include <ndir.h>
33 #    endif /* HAVE_NDIR_H */
34 #   endif /* HAVE_DIRENT_H */
35
36 #   define DOT_OR_DOTDOT(Basename) \
37      (Basename[0] == '.' && (Basename[1] == '\0' \
38                              || (Basename[1] == '.' && Basename[2] == '\0')))
39
40     static void
41     create_300_file_dir (const char *dir)
42     {
43       int i;
44
45       if (mkdir (dir, 0700))
46         exit (1);
47       if (chdir (dir))
48         exit (1);
49
50       for (i = 0; i < 300; i++)
51         {
52           char file_name[4];
53           FILE *out;
54
55           sprintf (file_name, "%03d", i);
56           out = fopen (file_name, "w");
57           if (!out)
58             exit (1);
59           if (fclose (out) == EOF)
60             exit (1);
61         }
62
63       if (chdir (".."))
64         exit (1);
65     }
66
67     static void
68     remove_dir (const char *dir)
69     {
70       DIR *dirp;
71
72       if (chdir (dir))
73         exit (1);
74
75       dirp = opendir (".");
76       if (dirp == NULL)
77         exit (1);
78
79       while (1)
80         {
81           struct dirent *dp = readdir (dirp);
82           if (dp == NULL)
83             break;
84
85           if (DOT_OR_DOTDOT (dp->d_name))
86             continue;
87
88           if (unlink (dp->d_name))
89             exit (1);
90         }
91       closedir (dirp);
92
93       if (chdir (".."))
94         exit (1);
95
96       if (rmdir (dir))
97         exit (1);
98     }
99
100     int
101     main ()
102     {
103       const char *dir = "conf-dir";
104       create_300_file_dir (dir);
105       remove_dir (dir);
106       exit (0);
107     }
108   >>,
109   changequote([, ])dnl
110   jm_cv_func_working_readdir=yes,
111   jm_cv_func_working_readdir=no,
112   jm_cv_func_working_readdir=no)])
113
114   if test x = y; then
115     dnl This code is deliberately never run via ./configure.
116     dnl FIXME: this is a gross hack to make autoheader put an entry
117     dnl for this HAVE_-prefixed symbol in config.h.in.
118     AC_CHECK_FUNCS(WORKING_READDIR)
119   fi
120
121   if test $jm_cv_func_working_readdir = yes; then
122     ac_kludge=HAVE_WORKING_READDIR
123     AC_DEFINE_UNQUOTED($ac_kludge)
124   fi
125 ])