how can I parameterize select function in scandir

famedoro

The scandir() function scans the directory dir, calling select() on each directory entry as "int(*filter)(const struct dirent *)" How can I pass pattern value as parameter to fnmatch(const char *pattern, const char *string, int flags) function used in filter ?

Here my sample code

int my_selectgrf(const struct dirent *namelist)
{
   int  r = 0;
   char     my_pattern[] = "*.grf";
   r = fnmatch(my_pattern, namelist->d_name, FNM_PERIOD);

   return (r==0)?1:0;
}
scandir("/pub/data/grf", &namelist, my_selectgrf, alphasort);

my goal is to be able to use my_pattern as input parameter.

R.. GitHub STOP HELPING ICE

The short answer: You can't. This is an atrociously bad API, and it's outright shameful that something like this was added to POSIX as recently as 2008 (based on a bad design in glibc). This kind of API without a way to parameterize it or pass it a context should have been abolished 20+ years ago.

With that said, there are some workarounds:

Approach 1: Use a global variable, and if your code needs to be thread-safe, ensure that only one thread can be using scandir with the given scan function at a time, by locking. This of course serializes usage, which is probably not acceptable if you actually want to be calling the function from multiple threads.

Approach 2: Use thread-local storage, either the GCC __thread keyword (or the C11 _Thread_local keyword, which GCC sadly still does not accept) or POSIX pthread_setspecific and family. This is fairly clean, but unfortunately it may not be correct; if the implementation of scandir internally used multiple threads, the parameter could fail to be available in some calls back to the scan function. At present, I don't believe there are multi-threaded implementations of scandir.

Now, the better solution:

Ditch scandir and write your own function to do the same thing, with the proper API. It's only a few lines anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In a custom R function that calls ezANOVA: How do I parameterize the dv?

From Dev

in jmeter how can I parameterize test plans on remote servers?

From Java

pytest - How can I pass pytest addoption value to pytest parameterize?

From Dev

How can I parameterize the tablename in a CREATE TABLE expression?

From Dev

How can I pass fixtures to pytest.mark.parameterize?

From Dev

in jmeter how can I parameterize test plans on remote servers?

From Dev

How can I parameterize a module so that it can configure the applications that require it on demand?

From Dev

How I can make a function to select a subset of a set of checkboxes?

From Dev

How, I can use select with max,min function in (linq and lambda)?

From Dev

How can I call a function everytime onChange on select is triggered on a userscript?

From Dev

How can I type parameterize a struct that doesn't use that type directly?

From Dev

Can I have a function within a SELECT query?

From Dev

How can I use noexcept when I select a specific hidden function from my inheritance list?

From Dev

Can I parameterize AWS lambda functions differently for staging and release resources?

From Dev

Can I parameterize labels and properties on CREATE or SET? (REST and transaction)

From Dev

Parameterize function name in Python

From Dev

Parameterize the Decode Function

From Dev

How can I style an HTML select element in function of the number of options it has?

From Dev

How can I send a SELECT box selected "text" to a function on a button for ng-click using angular?

From Dev

How can I select the currently clicked div? and why is my function not being executed?

From Dev

How can I return a function?

From Dev

How can I loop a function?

From Dev

How can I return a function?

From Dev

How can I call function?

From Dev

How can I import the function?

From Dev

How do I parameterize my Class parameter in this case?

From Dev

How do I parameterize the baseUrl property of the protractor config file

From Dev

How can I call a function inside of a function?

From Dev

How can I fire a function in a click(function)?

Related Related

  1. 1

    In a custom R function that calls ezANOVA: How do I parameterize the dv?

  2. 2

    in jmeter how can I parameterize test plans on remote servers?

  3. 3

    pytest - How can I pass pytest addoption value to pytest parameterize?

  4. 4

    How can I parameterize the tablename in a CREATE TABLE expression?

  5. 5

    How can I pass fixtures to pytest.mark.parameterize?

  6. 6

    in jmeter how can I parameterize test plans on remote servers?

  7. 7

    How can I parameterize a module so that it can configure the applications that require it on demand?

  8. 8

    How I can make a function to select a subset of a set of checkboxes?

  9. 9

    How, I can use select with max,min function in (linq and lambda)?

  10. 10

    How can I call a function everytime onChange on select is triggered on a userscript?

  11. 11

    How can I type parameterize a struct that doesn't use that type directly?

  12. 12

    Can I have a function within a SELECT query?

  13. 13

    How can I use noexcept when I select a specific hidden function from my inheritance list?

  14. 14

    Can I parameterize AWS lambda functions differently for staging and release resources?

  15. 15

    Can I parameterize labels and properties on CREATE or SET? (REST and transaction)

  16. 16

    Parameterize function name in Python

  17. 17

    Parameterize the Decode Function

  18. 18

    How can I style an HTML select element in function of the number of options it has?

  19. 19

    How can I send a SELECT box selected "text" to a function on a button for ng-click using angular?

  20. 20

    How can I select the currently clicked div? and why is my function not being executed?

  21. 21

    How can I return a function?

  22. 22

    How can I loop a function?

  23. 23

    How can I return a function?

  24. 24

    How can I call function?

  25. 25

    How can I import the function?

  26. 26

    How do I parameterize my Class parameter in this case?

  27. 27

    How do I parameterize the baseUrl property of the protractor config file

  28. 28

    How can I call a function inside of a function?

  29. 29

    How can I fire a function in a click(function)?

HotTag

Archive