Questions tagged [nftw]

18 questions
13
votes
3 answers

How avoid using global variable when using nftw

I want to use nftw to traverse a directory structure in C. However, given what I want to do, I don't see a way around using a global variable. The textbook examples of using (n)ftw all involve doing something like printing out a filename. I want,…
user1071847
  • 633
  • 9
  • 20
5
votes
1 answer

Recursive path of a directory with the nftw function in C

I'm trying to use the nftw() function to calculate the size of a directory by the sum of some of the files. But how I do handle the sum if I cannot use global variables to store the sum of the sizes, and I have to use the function nftw()?
J. Mendoza
  • 91
  • 7
4
votes
1 answer

Skip subdirectories when using ntfw in c

I am trying to get all the files and directories in the current or mentioned folder using nftw. But how do I instruct the function not to go further in any subdirectory? What is the purpose of the flag FTW_SKIP_SUBTREE? Also what is the header file…
bytestorm
  • 1,411
  • 3
  • 20
  • 36
4
votes
1 answer

nftw thread safe

Is there any thread safe implementation of nftw() in C/C++? In the documentation it says "The nftw() function need not be thread-safe." I'm going to use nftw for a recursive delete function to walk through the directory structure in a multi…
multiholle
  • 3,050
  • 8
  • 41
  • 60
3
votes
0 answers

Does Linux provide a mechanism for caching the file system in your program?

I'm working on a little hobby project to make my own file explorer. nothing fancy, just view the files in the selected folder and optionally add/edit/rename/delete them. I'd like for the view I present of the directory to update immediately as the…
J. Doe
  • 127
  • 9
3
votes
2 answers

how to specify nftw flags

This is my nftw function, it works correctly before specifying flags FTW_DEPTH and FTW_PHYS: if (nftw(argv[1], visit, 64, FTW_DEPTH | FTW_PHYS) != 0) { perror("nftw"); } Also I have defined visit as: int visit(const char *path, const struct…
2
votes
1 answer

Is it safe to fork() within a callback function in C?

I'm outlining a program. Basically, I want to use nftw to traverse the directory tree and perform a task(myexecutable). this function nftw takes for example (fn) as an argument and uses it as a callback function. Now I am planning to use a classic…
hirad davari
  • 103
  • 6
1
vote
1 answer

nftw different on BSD?

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) …
rwallace
  • 31,405
  • 40
  • 123
  • 242
1
vote
0 answers

C go in specified directory and find only simple files, on first file stop and do something

I want to use nftw() and be only on first lvl deep. But it still goes deeper, even if I specify FTW_MOUNT. Next I want to check if files is simple file, and do something with it. This is what i tried: #include #include #include…
Emilis
  • 152
  • 1
  • 4
  • 21
1
vote
2 answers

C: Specify maximum depth of search using nftw

In C, is there a way to specify the maximum depth from the base directory that nftw will search? For example, say the directory dir that I want to search has a sub-subdirectory, but I only want nftw to search through subdir and not sub-subdir, or…
petew
  • 671
  • 8
  • 13
1
vote
2 answers

Why do I get a Segmentation fault? I'm using stat, mmap, nftw, and memcmp, among other things

Here is my code. I'm assuming this has something to do with improper use of pointers or maybe I'm not mapping and unmapping my memory correctly. Could anyone please provide me with some insight into the issue? #define _XOPEN_SOURCE 500 #include…
Frank
  • 155
  • 1
  • 2
  • 9
0
votes
1 answer

using nftw to only traverse the folder specified

I am using nftw() to do directory traversal. Right now I am only wanting to list out all the files in the directory specified, however it seems to no matter what go down all the folders. It seems that nftw still traverses even if I specify…
xf900
  • 35
  • 4
0
votes
1 answer

Linked list of leaf files and directories in a dir tree using C

I'm using nftw(file tree walk) for traversing a dir(which has sub-directories and files). I've passed a directory using CLI function. Now I need to store the leaf files and dirs(empty dirs) into a linked list and print them out. I've create a…
0
votes
1 answer

How to make nftw() faster

I am working on a directory listing project and I need to capture all the files on the computer and then store them in a queue, which will then be sent off for worker threads to do work on. Right now I am using this example code of nftw(): #define…
Doritos
  • 403
  • 3
  • 16
0
votes
1 answer

Check for level change while traversing folder using nftw()

I am trying to recursively iterate a folder using the nftw() function of C for printing the complete Directory Structure along with that I can't find a way to check whether the level has changed ,i.e, it has moved inside a directory or is iterating…
Vishal Chugh
  • 337
  • 1
  • 6
  • 12
1
2