test: add nftw test case
[uclibc-ng.git] / test / misc / tst-nftw.c
blob76d11eb41c418642e9e0e027482859744fb6ab70
1 #define _XOPEN_SOURCE 500
2 #define _GNU_SOURCE
3 #include <ftw.h>
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
10 int result = 0;
12 static int process_one_entry(const char *fpath, const struct stat *sb,
13 int typeflag, struct FTW *ftwbuf)
16 struct stat buf;
17 const char *rel_path = fpath+ftwbuf->base;
19 printf("Processing %s in working dir %s\n",
20 rel_path, get_current_dir_name());
21 if (stat(rel_path, &buf) < 0) {
22 perror("Oops...relative path does not exist in current directory");
23 result = 1;
27 static int
28 do_test(void)
30 char *path = "/tmp/stest_dir";
31 char *subpath = "/tmp/stest_dir/d1";
32 char *filepath = "/tmp/stest_dir/f1";
33 char *filesubpath = "/tmp/stest_dir/d1/f2";
35 if ((mkdir(path, 0700)) < 0)
36 perror("Creating path");
37 if ((mkdir(subpath, 0700)) < 0)
38 perror("Creating subpath");
39 if ((open(filepath, O_CREAT)) < 0)
40 perror("Opening filepath");
41 if ((open(filesubpath, O_CREAT)) < 0)
42 perror("Opening filesubpath");
44 if (nftw(path, process_one_entry, 100, (FTW_CHDIR|FTW_DEPTH|FTW_PHYS)) < 0)
45 perror("ntfw");
47 unlink(filesubpath);
48 unlink(filepath);
49 rmdir(subpath);
50 rmdir(path);
52 return result;
55 #define TIMEOUT 5
56 #define TEST_FUNCTION do_test ()
57 #include "../test-skeleton.c"