about summary refs log tree commit diff
path: root/io/bug-ftw3.c
blob: fcc2ca7bba7a13ab8ed9dee7fad0aa26793111b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <errno.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static int cb_called;

static int
cb (const char *fname, const struct stat *st, int flag)
{
  printf ("%s %d\n", fname, flag);
  cb_called = 1;
  return 0;
}

int
main (void)
{
  char tmp[] = "/tmp/ftwXXXXXX";
  char tmp2[] = "/tmp/ftwXXXXXX/ftwXXXXXX";
  char *dname;
  char *dname2;
  int r;
  int e;

  dname = mkdtemp (tmp);
  if (dname == NULL)
    {
      printf ("mkdtemp: %m\n");
      exit (1);
    }

  memcpy (tmp2, tmp, strlen (tmp));
  dname2 = mkdtemp (tmp2);
  if (dname2 == NULL)
    {
      printf ("mkdtemp: %m\n");
      rmdir (dname);
      exit (1);
    }

  if (chmod (dname, S_IWUSR|S_IWGRP|S_IWOTH) != 0)
    {
      printf ("chmod: %m\n");
      rmdir (dname);
      exit (1);
    }

  r = ftw (dname2, cb, 10);
  e = errno;
  printf ("r = %d", r);
  if (r != 0)
    printf (", errno = %d", errno);
  puts ("");

  chmod (dname, S_IRWXU|S_IRWXG|S_IRWXO);
  rmdir (dname2);
  rmdir (dname);

  return (r != -1 && e == EACCES) || cb_called;
}