Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending arguments to ftw()

Is there a way to send arguments to ftw() to be used in process each file/directory on the path? It's a bit difficult to have the argument concerned as a global variable due to multithreading issues, i.e having the value as global will be visible to all threads and that would be wrong.

like image 617
Lipika Deka Avatar asked Nov 07 '11 18:11

Lipika Deka


1 Answers

A properly designed C callback interface has a void* argument that you can use to pass arbitrary data from the surrounding code into the callback. [n]ftw does not have such an argument, so you're kinda up a creek.

If your compiler supports thread-local variables (the __thread storage specifier) you can use them instead of globals; this will work but is not really that much tidier than globals.

If your C library has the fts family of functions, use those instead. They are available on most modern Unixes (including Linux, OSX, and recent *BSD) and gnulib has a fallback implementation.

like image 174
zwol Avatar answered Oct 23 '22 00:10

zwol