2001-12-11 07:59:02 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ftw.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-12-29 00:43:13 +08:00
|
|
|
#include <string.h>
|
2001-12-11 07:59:02 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-12-18 07:10:14 +08:00
|
|
|
static int cb_called;
|
|
|
|
|
2001-12-11 07:59:02 +08:00
|
|
|
static int
|
|
|
|
cb (const char *fname, const struct stat *st, int flag)
|
|
|
|
{
|
|
|
|
printf ("%s %d\n", fname, flag);
|
2001-12-18 07:10:14 +08:00
|
|
|
cb_called = 1;
|
2001-12-11 07:59:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
|
|
|
char tmp[] = "/tmp/ftwXXXXXX";
|
2001-12-18 07:10:14 +08:00
|
|
|
char tmp2[] = "/tmp/ftwXXXXXX/ftwXXXXXX";
|
2001-12-11 07:59:02 +08:00
|
|
|
char *dname;
|
2001-12-18 07:10:14 +08:00
|
|
|
char *dname2;
|
2001-12-11 07:59:02 +08:00
|
|
|
int r;
|
|
|
|
int e;
|
|
|
|
|
|
|
|
dname = mkdtemp (tmp);
|
|
|
|
if (dname == NULL)
|
|
|
|
{
|
|
|
|
printf ("mkdtemp: %m\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2001-12-18 07:10:14 +08:00
|
|
|
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)
|
2001-12-11 07:59:02 +08:00
|
|
|
{
|
|
|
|
printf ("chmod: %m\n");
|
2001-12-18 07:10:14 +08:00
|
|
|
rmdir (dname);
|
2001-12-11 07:59:02 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2001-12-18 07:10:14 +08:00
|
|
|
r = ftw (dname2, cb, 10);
|
2001-12-11 07:59:02 +08:00
|
|
|
e = errno;
|
|
|
|
printf ("r = %d", r);
|
|
|
|
if (r != 0)
|
|
|
|
printf (", errno = %d", errno);
|
|
|
|
puts ("");
|
|
|
|
|
|
|
|
chmod (dname, S_IRWXU|S_IRWXG|S_IRWXO);
|
2001-12-18 07:10:14 +08:00
|
|
|
rmdir (dname2);
|
2001-12-11 07:59:02 +08:00
|
|
|
rmdir (dname);
|
|
|
|
|
2001-12-18 07:10:14 +08:00
|
|
|
return (r != -1 && e == EACCES) || cb_called;
|
2001-12-11 07:59:02 +08:00
|
|
|
}
|