In my opinion, the only justification for a program is its usefulness to its users. That's the basic principle, everything else follows.
For this sorting issue, the display that's desirable is that which is useful to the user. I think that's means the order should be informative and predictable. Unspecified behaviour scores fairly badly, say I. Explaining _why_ it's unspecified doesn't justify it.
So, I suggest using a comparison wrapper such as compare() below rather than calling strcasecmp() directly. When qsort() is used with this compare function, the sort result should be rather good:
1) strcasecmp() is never called with an unspecified argument
2) there is a stable, easily predictable sort order
3) all zero-valued items are first
4) IPv4 addresses are sorted numerically correctly
5) IPv4 addresses are listed between the zero-valued items and the resolved addresses
Regrettably, it doesn't handle IPv6 addresses.
(The idea behind the " 4 " prefix is that since host names contain no spaces, the massaged IPv4 address is sorted before all host names. IPv6 addresses should have a " 6 " prefix and thus be sorted between IPv4 addresses and resolved host names.)
Please excuse my not submitting a patch. I prefer to avoid touching GPL'd source.
Arnt
static const char * fix( const char * s, const char * buf )
{
if ( !s || !*s ) {
s = "";
} else if ( isdigit( *s ) ) {
int a, b, c, d;
if ( sscanf( s, "%d.%d.%d.%d", &a, &b, &c, &d ) == 4 &&
a >= 0 && a <= 255 && b >= 0 && b <= 255 &&
c >= 0 && c <= 255 && d >= 0 && d <= 255 ) {
sprintf( buf, " 4 %03d.%03d.%03d.%03d", a, b, c, d );
s = buf;
}
}
return s;
}
int compare( const char * s1, const char * s2 ) { char buf1[20], buf2[20]; /* must be long enough for the sprintf above */
return strcasecmp( fix( s1, buf1 ), fix( s2, buf2 ) ); } _______________________________________________ Ntop mailing list [EMAIL PROTECTED] http://listgateway.unipi.it/mailman/listinfo/ntop
