1

I'm trying to get all .c files in a directory tree using nftw with the following code:

static int gf(const char *path, const struct stat *st, int t, struct FTW *ftw) {
    if (t != FTW_F)
        return 0;
    if (strcmp(ext(path), ".c") == 0)
        addl(&files, dup(abspath(path)));
    return 0;
}

void getfiles(char *path) {
    nftw(path, gf, 255, FTW_PHYS);
}

It works on Linux and Solaris, but on PC-BSD it fails by simply not picking up any files. What am I missing?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • Have you really used the name "dup" for strdup? That seems like a disaster waiting to happen! Does dup2 copy two strings? – William Pursell Nov 11 '11 at 13:22
  • I'm doing my own memory management for strings; dup is a function that basically does the same job as strdup except without the overhead of the general memory manager. But even if there was a bug in that function, it wouldn't account for the observed behavior, because the test I'm running basically only checks whether addl was ever called. – rwallace Nov 11 '11 at 13:32
  • 3
    But dup is a terrible name, since that is the standard library function that duplicates a file descriptor. – William Pursell Nov 11 '11 at 13:54
  • 1
    To clarify, if nftw is trying to duplicate a file descriptor and it calls your dup instead of the normal dup, it will likely fail and cause problems. (I don't think it is likely that this is the problem, but it is definitely a potential source of future problems.) – William Pursell Nov 11 '11 at 14:15
  • Doh! I'd entirely forgotten about that function. You're right of course, I'll change the name. – rwallace Nov 11 '11 at 14:39
  • Are the functions `addl()`, `abspath()`, and `ext()` also yours (as well as `dup()`)? What's the type of `files`? Have you tested them independently? Did you get it to work when you changed the name of your `dup()` function? – Jonathan Leffler Nov 13 '11 at 21:32

1 Answers1

1

What is the return value of nftw? If it's -1 and the errno is set to EINVAL it's quite likely that you're exceeding the value of OPEN_MAX. Try passing a smaller value as third parameter to nftw and ensure it's smaller than OPEN_MAX.

chill
  • 16,470
  • 2
  • 40
  • 44