]> sourceware.org Git - glibc.git/blobdiff - io/ftwtest.c
support: Use macros for *stat wrappers
[glibc.git] / io / ftwtest.c
index 607926517595149cca20cc83335a3b5fe2cd3c2e..77debd28050c92c467fbcc5388ad36fa9c462130 100644 (file)
@@ -3,6 +3,7 @@
 #include <mcheck.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
 int do_depth;
 int do_chdir;
 int do_phys;
+int do_exit;
+char *skip_subtree;
+char *skip_siblings;
 
 struct option options[] =
 {
   { "depth", no_argument, &do_depth, 1 },
   { "chdir", no_argument, &do_chdir, 1 },
   { "phys", no_argument, &do_phys, 1 },
+  { "skip-subtree", required_argument, NULL, 't' },
+  { "skip-siblings", required_argument, NULL, 's' },
+  { "early-exit", no_argument, &do_exit, 1 },
   { NULL, 0, NULL, 0 }
 };
 
@@ -31,9 +38,12 @@ const char *flag2name[] =
 };
 
 
-int
+static int
 cb (const char *name, const struct stat *st, int flag, struct FTW *f)
 {
+  if (do_exit && strcmp (name + f->base, "file@2"))
+    return FTW_CONTINUE;
+
   printf ("base = \"%.*s\", file = \"%s\", flag = %s",
          f->base, name, name + f->base, flag2name[flag]);
   if (do_chdir)
@@ -43,7 +53,14 @@ cb (const char *name, const struct stat *st, int flag, struct FTW *f)
       free (cwd);
     }
   printf (", level = %d\n", f->level);
-  return 0;
+
+  if (skip_siblings && strcmp (name + f->base, skip_siblings) == 0)
+    return FTW_SKIP_SIBLINGS;
+
+  if (skip_subtree && strcmp (name + f->base, skip_subtree) == 0)
+    return FTW_SKIP_SUBTREE;
+
+  return do_exit ? 26 : FTW_CONTINUE;
 }
 
 int
@@ -55,7 +72,12 @@ main (int argc, char *argv[])
   mtrace ();
 
   while ((opt = getopt_long_only (argc, argv, "", options, NULL)) != -1)
-    ;
+    {
+      if (opt == 't')
+        skip_subtree = optarg;
+      else if (opt == 's')
+        skip_siblings = optarg;
+    }
 
   if (do_chdir)
     flag |= FTW_CHDIR;
@@ -63,9 +85,36 @@ main (int argc, char *argv[])
     flag |= FTW_DEPTH;
   if (do_phys)
     flag |= FTW_PHYS;
+  if (skip_subtree || skip_siblings)
+    {
+      flag |= FTW_ACTIONRETVAL;
+      if (do_exit)
+       {
+         printf ("--early-exit cannot be used together with --skip-{siblings,subtree}");
+         exit (1);
+       }
+    }
 
-  r = nftw (optind < argc ? argv[optind] : ".", cb, 3, flag);
-  if (r)
+  char *cw1 = getcwd (NULL, 0);
+
+  r = nftw (optind < argc ? argv[optind] : ".", cb, do_exit ? 1 : 3, flag);
+  if (r < 0)
     perror ("nftw");
+
+  char *cw2 = getcwd (NULL, 0);
+
+  if (strcmp (cw1, cw2) != 0)
+    {
+      printf ("current working directory before and after nftw call differ:\n"
+             "before: %s\n"
+             "after:  %s\n", cw1, cw2);
+      exit (1);
+    }
+
+  if (do_exit)
+    {
+      puts (r == 26 ? "succeeded" : "failed");
+      return r == 26 ? 0 : 1;
+    }
   return r;
 }
This page took 0.031124 seconds and 5 git commands to generate.