Back to index

glibc  2.9
bug-ftw3.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <ftw.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 
00008 static int cb_called;
00009 
00010 static int
00011 cb (const char *fname, const struct stat *st, int flag)
00012 {
00013   printf ("%s %d\n", fname, flag);
00014   cb_called = 1;
00015   return 0;
00016 }
00017 
00018 int
00019 main (void)
00020 {
00021   char tmp[] = "/tmp/ftwXXXXXX";
00022   char tmp2[] = "/tmp/ftwXXXXXX/ftwXXXXXX";
00023   char *dname;
00024   char *dname2;
00025   int r;
00026   int e;
00027 
00028   if (getuid () == 0)
00029     {
00030       puts ("this test needs to be run by ordinary user");
00031       exit (0);
00032     }
00033 
00034   dname = mkdtemp (tmp);
00035   if (dname == NULL)
00036     {
00037       printf ("mkdtemp: %m\n");
00038       exit (1);
00039     }
00040 
00041   memcpy (tmp2, tmp, strlen (tmp));
00042   dname2 = mkdtemp (tmp2);
00043   if (dname2 == NULL)
00044     {
00045       printf ("mkdtemp: %m\n");
00046       rmdir (dname);
00047       exit (1);
00048     }
00049 
00050   if (chmod (dname, S_IWUSR|S_IWGRP|S_IWOTH) != 0)
00051     {
00052       printf ("chmod: %m\n");
00053       rmdir (dname);
00054       exit (1);
00055     }
00056 
00057   r = ftw (dname2, cb, 10);
00058   e = errno;
00059   printf ("r = %d", r);
00060   if (r != 0)
00061     printf (", errno = %d", errno);
00062   puts ("");
00063 
00064   chmod (dname, S_IRWXU|S_IRWXG|S_IRWXO);
00065   rmdir (dname2);
00066   rmdir (dname);
00067 
00068   return (r != -1 && e == EACCES) || cb_called;
00069 }