<div dir="ltr">Hello all,<div>I am currently trying to implement fts sorting for all the sort options we need to do for our project. As c is not my first or even third language I was hoping someone could help me detangle the pointers going on in this:</div>
<div><br></div><div>So the fts man page say to use it like this:</div><div><br></div>FTS *fts_open(char * const *path_argv, int options, <div>                                             int (*compar)(const FTSENT **, const FTSENT **));</div>
<div><br></div><div>and has this to say about it:</div>The argument compar() specifies a user-defined function which may be used to order the traversal of the hierarchy. It takes two pointers to pointers to FTSENT structures as arguments and should return a negative value, zero, or a positive value to indicate if the file referenced by its first argument comes before, in any order with respect to, or after, the file referenced by its second argument.<div>
<br></div><div>So from what I can tell from the documentation I am then to write some sort of function that returns -1,0,1 as any qsort should.</div><div><br></div><div>Being particularly puzzled and not able to get it to pass anything correctly I took a look at ls from bsd and saw some interesting things:</div>
<div><br></div><div>The first thing I noticed is that there are two levels to the sort functions: (these are the definitions at the top)</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre">static int       mastercmp(const FTSENT * const *, const FTSENT * const *);</span><br>
</div><div><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre">static int (*sortfcn)(const FTSENT *, const FTSENT *);</span><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre"><br>
</span></div><div><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre"><br></span></div><div>After some logic it picks a sort type (which is *sortfcn ) and it is used in fts like this:</div>
<div><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre">fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)</span><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre"><br>
</span></div><div><br></div><div><br></div>So formost: <div>1. Why the aliasing of the sort function and why do the types not match the man page for what fts is expecting?</div><div>2. Logically what exactly is this type : <span style="color:rgb(0,0,0);font-family:monospace;font-size:medium;white-space:pre">const FTSENT * const * </span>and why was it set up like this?</div>
<div>3. Why are the actual sort functions prototyped like (*sortfcn) and named differently? Is this some sort of wild card function name replacement?</div><div><br></div><div><br></div><div><br></div></div>