#include #include #include #include #include /* Arrange to define PATH_MAX, like "pathmax.h" does. */ #include #include #include #if !defined PATH_MAX && defined MAXPATHLEN # define PATH_MAX MAXPATHLEN #endif #ifndef S_IRWXU # define S_IRWXU 0700 #endif /* This size is chosen to be larger than PATH_MAX (4k), yet smaller than the 16kB pagesize on ia64 linux. Those conditions make the code below trigger a bug in glibc's getcwd implementation before 2.4.90-10. */ #define TARGET_LEN (5 * 1024) int main (void) { char *cwd; size_t initial_cwd_len; int fail = 0; cwd = getcwd (NULL, 0); if (cwd == NULL) return 2; initial_cwd_len = strlen (cwd); free (cwd); if (1) { static char const dir_name[] = "confdir-14B---"; size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len) / sizeof dir_name); size_t d; for (d = 0; d < desired_depth; d++) { if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0) { if (! (errno == ERANGE || errno == ENAMETOOLONG || errno == ENOENT)) fail = 3; /* Unable to construct deep hierarchy. */ break; } } /* If libc has the bug in question, this invocation of getcwd results in a failed assertion. */ cwd = getcwd (NULL, 0); if (cwd == NULL) fail = 4; /* getcwd didn't assert, but it failed for a long name where the answer could have been learned. */ free (cwd); /* Call rmdir first, in case the above chdir failed. */ rmdir (dir_name); while (0 < d--) { if (chdir ("..") < 0) { fail = 5; break; } rmdir (dir_name); } } return fail; }