wpa_supplicant/scan.c
static int wpa_scan_result_compar(const void *a, const void *b)
{
#define MIN(a,b) a < b ? a : b
	struct wpa_scan_res **_wa = (void *) a;
	struct wpa_scan_res **_wb = (void *) b;
	struct wpa_scan_res *wa = *_wa;
	struct wpa_scan_res *wb = *_wb;
	int wpa_a, wpa_b;
	int snr_a, snr_b, snr_a_full, snr_b_full;

	/* WPA/WPA2 support preferred */
	wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
		wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
	wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
		wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;

	if (wpa_b && !wpa_a)
		return 1;
	if (!wpa_b && wpa_a)
		return -1;

	/* privacy support preferred */
	if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
	    (wb->caps & IEEE80211_CAP_PRIVACY))
		return 1;
	if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
	    (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
		return -1;

	if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
		snr_a_full = wa->snr;
		snr_a = MIN(wa->snr, GREAT_SNR);
		snr_b_full = wb->snr;
		snr_b = MIN(wb->snr, GREAT_SNR);
	} else {
		/* Level is not in dBm, so we can't calculate
		 * SNR. Just use raw level (units unknown). */
		snr_a = snr_a_full = wa->level;
		snr_b = snr_b_full = wb->level;
	}

	/* if SNR is close, decide by max rate or frequency band */
	if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
	    (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
		if (wa->est_throughput != wb->est_throughput)
			return wb->est_throughput - wa->est_throughput;
		if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
			return IS_5GHZ(wa->freq) ? -1 : 1;
	}

	/* all things being equal, use SNR; if SNRs are
	 * identical, use quality values since some drivers may only report
	 * that value and leave the signal level zero */
	if (snr_b_full == snr_a_full)
		return wb->qual - wa->qual;
	return snr_b_full - snr_a_full;
#undef MIN
}
/* Compare function for sorting scan results. Return >0 if @b is considered
 * better. */
Compare function for sorting scan results. Return >0 if @b is considered
better.
Compare function for sorting scan results . Return > 0 if @b is considered better .
static int wpa_scan_result_compar(const void *a, const void *b)
{
#define MIN(a,b) a < b ? a : b
	struct wpa_scan_res **_wa = (void *) a;
	struct wpa_scan_res **_wb = (void *) b;
	struct wpa_scan_res *wa = *_wa;
	struct wpa_scan_res *wb = *_wb;
	int wpa_a, wpa_b;
	int snr_a, snr_b, snr_a_full, snr_b_full;
	wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
		wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
	wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
		wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
	if (wpa_b && !wpa_a)
		return 1;
	if (!wpa_b && wpa_a)
		return -1;
	if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
	    (wb->caps & IEEE80211_CAP_PRIVACY))
		return 1;
	if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
	    (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
		return -1;
	if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
		snr_a_full = wa->snr;
		snr_a = MIN(wa->snr, GREAT_SNR);
		snr_b_full = wb->snr;
		snr_b = MIN(wb->snr, GREAT_SNR);
	} else {
		snr_a = snr_a_full = wa->level;
		snr_b = snr_b_full = wb->level;
	}
	if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
	    (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
		if (wa->est_throughput != wb->est_throughput)
			return wb->est_throughput - wa->est_throughput;
		if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
			return IS_5GHZ(wa->freq) ? -1 : 1;
	}
	if (snr_b_full == snr_a_full)
		return wb->qual - wa->qual;
	return snr_b_full - snr_a_full;
#undef MIN
}
static int wpa_scan_result_compar ( const void * a , const void * b ) { #define MIN ( a , b )  a < b ? a : b struct wpa_scan_res * * _wa = ( void * ) a ; struct wpa_scan_res * * _wb = ( void * ) b ; struct wpa_scan_res * wa = * _wa ; struct wpa_scan_res * wb = * _wb ; int wpa_a , wpa_b ; int snr_a , snr_b , snr_a_full , snr_b_full ; wpa_a = wpa_scan_get_vendor_ie ( wa , WPA_IE_VENDOR_TYPE ) != NULL || wpa_scan_get_ie ( wa , WLAN_EID_RSN ) != NULL ; wpa_b = wpa_scan_get_vendor_ie ( wb , WPA_IE_VENDOR_TYPE ) != NULL || wpa_scan_get_ie ( wb , WLAN_EID_RSN ) != NULL ; if ( wpa_b && ! wpa_a ) return 1 ; if ( ! wpa_b && wpa_a ) return -1 ; if ( ( wa -> caps & IEEE80211_CAP_PRIVACY ) == 0 && ( wb -> caps & IEEE80211_CAP_PRIVACY ) ) return 1 ; if ( ( wa -> caps & IEEE80211_CAP_PRIVACY ) && ( wb -> caps & IEEE80211_CAP_PRIVACY ) == 0 ) return -1 ; if ( wa -> flags & wb -> flags & WPA_SCAN_LEVEL_DBM ) { snr_a_full = wa -> snr ; snr_a = MIN ( wa -> snr , GREAT_SNR ) ; snr_b_full = wb -> snr ; snr_b = MIN ( wb -> snr , GREAT_SNR ) ; } else { snr_a = snr_a_full = wa -> level ; snr_b = snr_b_full = wb -> level ; } if ( ( snr_a && snr_b && abs ( snr_b - snr_a ) < 5 ) || ( wa -> qual && wb -> qual && abs ( wb -> qual - wa -> qual ) < 10 ) ) { if ( wa -> est_throughput != wb -> est_throughput ) return wb -> est_throughput - wa -> est_throughput ; if ( IS_5GHZ ( wa -> freq ) ^ IS_5GHZ ( wb -> freq ) ) return IS_5GHZ ( wa -> freq ) ? -1 : 1 ; } if ( snr_b_full == snr_a_full ) return wb -> qual - wa -> qual ; return snr_b_full - snr_a_full ; #undef  MIN }
Compare function for sorting scan results.
Compare function for sorting scan results .
/* WPA/WPA2 support preferred */ /* privacy support preferred */ /* Level is not in dBm, so we can't calculate
		 * SNR. Just use raw level (units unknown). */ /* if SNR is close, decide by max rate or frequency band */ /* all things being equal, use SNR; if SNRs are
	 * identical, use quality values since some drivers may only report
	 * that value and leave the signal level zero */