--- wmwifi-0.4/src/wmwifi.c	Fri Feb 27 20:45:33 2004
+++ wmwifi-0.4-freebsd/src/wmwifi.c	Thu Mar  4 23:40:06 2004
@@ -67,7 +67,12 @@
     int ncolor = 0;
     struct wifi wfi;
 
+	/* find a valid wireless interface */
+#if __FreeBSD__
+	wfi.ifnum = get_wlaniface(0, 1);
+#else
     wfi.ifnum = 0;
+#endif
 
     /* Parse CommandLine */
     parse_arguments(argc, argv);
@@ -140,10 +145,18 @@
 		    switch_light(&wfi);
 		    break;
 		case Button2:
+#ifdef __FreeBSD__
+			wfi.ifnum = get_wlaniface(wfi.ifnum, 1);
+#else
 		    next_if(&wfi);
+#endif
 		    break;
 		case Button3:
-		    last_if(&wfi);
+#ifdef __FreeBSD__
+			wfi.ifnum = get_wlaniface(wfi.ifnum, -1);
+#else
+		    prev_if(&wfi);
+#endif
 		    break;
 		}
 
--- wmwifi-0.4/src/wmwifi.h	Fri Feb 27 20:32:49 2004
+++ wmwifi-0.4-freebsd/src/wmwifi.h	Thu Mar  4 23:39:39 2004
@@ -13,10 +13,21 @@
 #include <net/ethernet.h>	/* struct ether_addr */
 #include <sys/time.h>		/* struct timeval */
 
+/* max interface name length */
+#ifdef __FreeBSD__
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <net/if_var.h>
+#include <netinet/in.h>
+#define IFNAMELEN	(IFNAMSIZ)
+#else
+#define IFNAMELEN	255
+#endif
+
 int get_max_qual(char ifname[255]);
-//struct wifi wifi_info(void);
 struct wifi {
-    char ifname[255];
+    char ifname[IFNAMELEN];
     int ifnum; 
     int link;
     char level[255];
@@ -26,4 +37,7 @@
 int get_max_ifs(void);
 int wifi_info(struct wifi *wfi);
 void next_if(struct wifi *wfi);
-void last_if(struct wifi *wfi);
+void prev_if(struct wifi *wfi);
+#ifdef __FreeBSD__
+int get_wlaniface(int, int);
+#endif
--- wmwifi-0.4/src/wireless.c	Fri Feb 27 20:44:22 2004
+++ wmwifi-0.4-freebsd/src/wireless.c	Thu Mar  4 23:41:06 2004
@@ -6,11 +6,32 @@
 #include <X11/X.h>
 #include <X11/xpm.h>
 #include "wmwifi.h"
+#ifdef __linux__
 #include <net/ethernet.h>
 #include <linux/if.h>
 #include <linux/socket.h>
 #include <linux/wireless.h>
+#elif __FreeBSD__
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/sysctl.h>
+#include <netinet/in.h>
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <net/if_media.h>
+#include <net/if_mib.h>
+#include <net/if_var.h>
+#include <netinet/in.h>
+#include <dev/wi/if_wavelan_ieee.h>
+#else
+#error "sorry, your OS is not supported yet"
+#endif
+
 
+#ifdef __linux__
 /*
  * This is my quick and dirty way of accessing the Linux Wireless Extensions
  * from the /proc/net/wireless interface.
@@ -18,7 +39,7 @@
  * - Jess
  */
 
-int get_max_qual(char ifname[255])
+int get_max_qual(char ifname[IFNAMELEN])
 {
     struct iwreq wrq;
     struct iw_range range;
@@ -44,6 +65,19 @@
 
     return ret;
 }
+int get_max_ifs(void)
+{
+    FILE *fp;
+    char buff[255];
+    int i = 0;
+
+    if (fp = fopen("/proc/net/wireless", "r")) {
+	for (i = -3; fgets(buff, sizeof(buff), fp); i++) {
+	}
+	fclose(fp);
+    }
+    return i;
+}
 void next_if(struct wifi *wfi)
 {
     int max;
@@ -56,7 +90,7 @@
 	wfi->ifnum = 0;
     }
 }
-void last_if(struct wifi *wfi)
+void prev_if(struct wifi *wfi)
 {
     int max;
 
@@ -68,19 +102,6 @@
 	wfi->ifnum = max;
     }
 }
-int get_max_ifs(void)
-{
-    FILE *fp;
-    char buff[255];
-    int i = 0;
-
-    if (fp = fopen("/proc/net/wireless", "r")) {
-	for (i = -3; fgets(buff, sizeof(buff), fp); i++) {
-	}
-	fclose(fp);
-    }
-    return i;
-}
 int wifi_info(struct wifi *wfi)
 {
     FILE *fp;
@@ -101,9 +122,158 @@
 
 	fclose(fp);
     } else {
-	fprintf(stdout,
+	fprintf(stderr,
 		"ERROR: Cannot Open Linux Wireless Extensions!\n\b");
 	exit(1);
     }
     return 1;
 }
+#elif __FreeBSD__
+/* get index of another wireless interface
+ * dir  = 1 -> incrementing direction
+ * dir != 1 -> decrementing
+ */
+int get_wlaniface(int old, int dir)
+{
+	int i, max, step, found, index;
+	struct ifmibdata ifmd;
+	struct ifmediareq ifmr;
+	int name[6], len, s;
+	char *iface[IFNAMSIZ];
+
+	max = get_max_ifs();
+	step = 0;
+
+	if (old > max)
+		old = max; /* just be sure to not be out of bounds */
+
+	index = old;
+
+	while ((step < max) && !found)
+	{
+		step++;	/* prevent endless looping */
+		if (dir == 1)
+		{
+			if (index < max)
+				index++;
+			else
+				index = 1;
+		}
+		else
+		{
+			if (index > 1)
+				index--;
+			else
+				index = max;
+		}
+
+		/* build new request */
+		name[0] = CTL_NET;
+		name[1] = PF_LINK;
+		name[2] = NETLINK_GENERIC;
+		name[3] = IFMIB_IFDATA;
+		name[4] = index;
+		name[5] = IFDATA_GENERAL;
+
+		len = sizeof(ifmd);
+		sysctl(name, 6, &ifmd, &len, NULL, 0);
+
+		/* skip loopback interface */
+		if (!strcmp(ifmd.ifmd_name, "lo0"))
+			continue;
+
+		s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+		if (s == -1)
+		{
+			perror("socket");
+			exit(1);
+		}
+		bzero(&ifmr, sizeof(ifmr));
+		strncpy(ifmr.ifm_name, ifmd.ifmd_name, sizeof(ifmd.ifmd_name));
+
+		if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
+		{
+			perror("ioctl");
+			close(s);
+			exit(1);
+		}
+		close(s);
+		/* we cannot monitor interfaces in hostap mode, so just
+		 * make sure it's a wireless interface
+		 */
+		if ((ifmr.ifm_active & IFM_IEEE80211) &&
+		   !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP))
+		{
+			found++;
+		}
+	}
+	return index;
+}
+/* how many interfaces do we have? this includes non-wireless! */
+int get_max_ifs(void)
+{
+	int count, len;
+
+	len = sizeof(count);
+	sysctlbyname("net.link.generic.system.ifcount", &count, &len, NULL, 0);
+	return count;
+}
+int wifi_info(struct wifi *wfi)
+{
+	int name[6], len;		/* interface name */
+	struct ifmibdata ifmd;
+
+	struct ifreq ifr;		/* interface stats */
+	struct wi_req wireq;
+	int s;
+
+	/* lets find the current interface name */
+	name[0] = CTL_NET;
+	name[1] = PF_LINK;
+	name[2] = NETLINK_GENERIC;
+	name[3] = IFMIB_IFDATA;
+	name[4] = wfi->ifnum;
+	name[5] = IFDATA_GENERAL;
+
+	len = sizeof(ifmd);
+	sysctl(name, 6, &ifmd, &len, NULL, 0);
+	strncpy(wfi->ifname, ifmd.ifmd_name,
+		(strlen(ifmd.ifmd_name) > IFNAMELEN) ?
+		IFNAMELEN : strlen(ifmd.ifmd_name));
+
+	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+	if (s == -1)
+	{
+		perror("socket");
+		exit(1);
+	}
+	bzero(&ifr, sizeof(ifr));
+	/* struct ifreq interface name can only be 16 Bytes long as of now */
+	if (strlen(wfi->ifname) >= IFNAMSIZ)
+	{
+		fprintf(stderr, "ERROR: interface name too long\n");
+		exit(1);
+	}
+	strncpy(ifr.ifr_name, wfi->ifname, strlen(wfi->ifname));
+	wireq.wi_type	= WI_RID_COMMS_QUALITY;
+	wireq.wi_len	= WI_MAX_DATALEN;
+	ifr.ifr_data	= (void *) &wireq;
+
+	if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0)
+	{
+		perror("ioctl");
+		exit(1);
+	}
+	close (s);
+
+	/*
+	 * wi_val[0] = quality
+	 * wi_val[1] = signal
+	 * wi_val[2] = noise
+	 */
+	wfi->link = (int) wireq.wi_val[1];
+	wfi->max_qual = 128;
+
+	return 1;	
+}
+#endif


syntax highlighted by Code2HTML, v. 0.9.1