<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Mine may not be as elegant but I just setup separate sort functions for each type of sort and an if/else to call one depending on flags. I didn't look at any existing ls code because wanted to learn it as I go and built it my way.<div><br></div><div>It works! :)</div><div><br></div><div><div><div>On Oct 10, 2013, at 6:48 PM, Paul-Anthony Dudzinski &lt;<a href="mailto:pdudzins@stevens.edu">pdudzins@stevens.edu</a>&gt; wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><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,&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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="font-family: monospace; font-size: medium; white-space: pre; ">static int       mastercmp(const FTSENT * const *, const FTSENT * const *);</span><br>
</div><div><span style="font-family: monospace; font-size: medium; white-space: pre; ">static int (*sortfcn)(const FTSENT *, const FTSENT *);</span><span style="font-family: monospace; font-size: medium; white-space: pre; "><br>
</span></div><div><span style="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="font-family: monospace; font-size: medium; white-space: pre; ">fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)</span><span style="font-family: monospace; font-size: medium; white-space: pre; "><br>
</span></div><div><br></div><div><br></div>So formost:&nbsp;<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 :&nbsp;<span style="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>
_______________________________________________<br>cs631apue mailing list<br><a href="mailto:cs631apue@lists.stevens.edu">cs631apue@lists.stevens.edu</a><br>https://lists.stevens.edu/mailman/listinfo/cs631apue<br></blockquote></div><br></div></body></html>