[cs631apue] Using fts sort functions

Paul-Anthony Dudzinski pdudzins at stevens.edu
Thu Oct 10 18:48:15 EDT 2013


Hello all,
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:

So the fts man page say to use it like this:

FTS *fts_open(char * const *path_argv, int options,
                                             int (*compar)(const FTSENT **,
const FTSENT **));

and has this to say about it:
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.

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.

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:

The first thing I noticed is that there are two levels to the sort
functions: (these are the definitions at the top)

static int mastercmp(const FTSENT * const *, const FTSENT * const *);
static int (*sortfcn)(const FTSENT *, const FTSENT *);

After some logic it picks a sort type (which is *sortfcn ) and it is used
in fts like this:
fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)


So formost:
1. Why the aliasing of the sort function and why do the types not match the
man page for what fts is expecting?
2. Logically what exactly is this type : const FTSENT * const * and why was
it set up like this?
3. Why are the actual sort functions prototyped like (*sortfcn) and named
differently? Is this some sort of wild card function name replacement?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.stevens.edu/pipermail/cs631apue/attachments/20131010/dffe1023/attachment-0001.html>


More information about the cs631apue mailing list