1 #include <limits.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 
9 int
10 main (int argc, char **argv)
11 {
12   char template[] = "/tmp/wd_test_XXXXXX";
13   char *tmpdir = mkdtemp(template);
14   if (tmpdir == NULL)
15     {
16       perror ("Couldn't mkdtemp");
17       exit (-1);
18     }
19 
20   if (chdir (tmpdir) != 0)
21     {
22       perror ("Couldn't chdir into tmpdir");
23       exit (-1);
24     }
25 
26   /* Go deep. */
27   int dirslen = PATH_MAX;
28   while (dirslen > 0)
29     {
30       /* We don't do any error checking in case some OS fails. */
31       mkdir ("subdir", S_IRWXU);
32       chdir ("subdir");
33       dirslen -= strlen ("subdir");
34     }
35 
36   /* Make one component inaccessible. */
37   chmod(tmpdir, 0);
38 
39   /* Remove the current dir (don't check error, might fail). */
40   rmdir ("../subdir");
41 
42   execlp ("echo", "echo", "Hello", "World", (char *) NULL);
43   perror ("Couldn't execlp");
44   return -1;
45 }
46