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, instead, to take the pathname and file checksum and place those in a data structure. But I don't see a good way to do that, given the limits on what can be passed to nftw.
The solution I'm using involves a global variable. The function called by nftw can then access that variable and add the required data.
Is there any reasonable way to do this without using a global variable?
No.
nftwdoesn't offer any user parameter that could be passed to the function, so you have to use global (or static) variables in C.GCC offers an extension "nested function" which should capture the variables of their enclosing scopes, so they could be used like this: