diff --git a/Makefile b/Makefile
index 6b8c4a6867da1a52609963965af2391274b21bf7..9b90917c4381ffbb83d331ee76e4372417c819ee 100755
--- a/Makefile
+++ b/Makefile
@@ -13,13 +13,17 @@
 
 .EXPORT_ALL_VARIABLES:
 
+OSARCH=$(shell uname -s)
+
 # Pentium Pro Optimize
-#PROC=i686
+PROC=i686
 # Pentium Optimize
 #PROC=i586
 #PROC=k6
 #PROC=ppc
+ifeq (${OSARCH},Linux)
 PROC=$(shell uname -m)
+endif
 
 ######### More GSM codec optimization
 ######### Uncomment to enable MMXTM optimizations for x86 architecture CPU's
@@ -63,6 +67,9 @@ CFLAGS=-pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarati
 CFLAGS+=$(OPTIMIZE)
 CFLAGS+=$(shell if $(CC) -march=$(PROC) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-march=$(PROC)"; fi)
 CFLAGS+=$(shell if uname -m | grep -q ppc; then echo "-fsigned-char"; fi)
+ifeq (${OSARCH},OpenBSD)
+CFLAGS+=-pthread
+endif
 
 LIBEDIT=editline/libedit.a
 
@@ -85,7 +92,10 @@ CFLAGS+= $(DEBUG_THREADS)
 CFLAGS+= $(TRACE_FRAMES)
 CFLAGS+=# -fomit-frame-pointer 
 SUBDIRS=res channels pbx apps codecs formats agi cdr astman
-LIBS=-ldl -lpthread -lncurses -lm  #-lnjamd
+ifeq (${OSARCH},Linux)
+LIBS=-ldl
+endif
+LIBS+=-lpthread -lncurses -lm  #-lnjamd
 OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
 	translate.o file.o say.o pbx.o cli.o md5.o term.o \
 	ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
@@ -147,7 +157,7 @@ build.h:
 endif
 
 asterisk: .version build.h editline/libedit.a db1-ast/libdb1.a $(OBJS)
-	gcc -o asterisk -rdynamic $(OBJS) $(LIBS) $(LIBEDIT) db1-ast/libdb1.a
+	gcc $(DEBUG) -o asterisk -rdynamic $(OBJS) $(LIBS) $(LIBEDIT) db1-ast/libdb1.a
 
 subdirs: 
 	for x in $(SUBDIRS); do $(MAKE) -C $$x || exit 1 ; done
diff --git a/apps/Makefile b/apps/Makefile
index 30e1a9f273bb733ff7b2d10317d417438357d54c..ba74bebe6658105420bd1e01217293581928eb4f 100755
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -27,7 +27,7 @@ APPS=app_dial.so app_playback.so app_voicemail.so app_directory.so app_intercom.
 APPS+=$(shell if [ -f /usr/include/linux/zaptel.h ]; then echo "app_zapras.so app_meetme.so app_flash.so app_zapbarge.so" ; fi)
 #APPS+=$(shell if [ -f /usr/include/zap.h ]; then echo "app_rpt.so" ; fi)
 
-CFLAGS+=
+CFLAGS+=-fPIC
 
 all: $(APPS)
 
diff --git a/apps/app_mp3.c b/apps/app_mp3.c
index 791840106d4c8a3d9ecb75c04815eb05e8a8ad01..52f1225c65df163a85b545d4b49dc8a47d3710f3 100755
--- a/apps/app_mp3.c
+++ b/apps/app_mp3.c
@@ -75,7 +75,7 @@ static int timed_read(int fd, void *data, int datalen)
 	struct timeval tv = { 2, 0 };		/* Wait no more than 2 seconds */
 	FD_ZERO(&fds);
 	FD_SET(fd, &fds);
-	res = select(fd + 1, &fds, NULL, NULL, &tv);
+	res = ast_select(fd + 1, &fds, NULL, NULL, &tv);
 	if (res < 1) {
 		ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
 		return -1;
diff --git a/asterisk.c b/asterisk.c
index 9e551a55c6510c8b54d2c8846c0930bdaa35327d..5ee2a4f6721d26c31d6bef4d1b901e40f0739331 100755
--- a/asterisk.c
+++ b/asterisk.c
@@ -130,7 +130,7 @@ static void *netconsole(void *vconsole)
 		max = con->fd;
 		if (con->p[0] > max)
 			max = con->p[0];
-		res = select(max + 1, &rfds, NULL, NULL, NULL);
+		res = ast_select(max + 1, &rfds, NULL, NULL, NULL);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "select returned < 0: %s\n", strerror(errno));
 			continue;
@@ -672,7 +672,7 @@ static int ast_el_read_char(EditLine *el, char *cp)
 		max = ast_consock;
 		if (STDIN_FILENO > max)
 			max = STDIN_FILENO;
-		res = select(max+1, &rfds, NULL, NULL, NULL);
+		res = ast_select(max+1, &rfds, NULL, NULL, NULL);
 		if (res < 0) {
 			if (errno == EINTR)
 				continue;
@@ -1369,7 +1369,7 @@ int main(int argc, char *argv[])
 
 	} else {
  		/* Do nothing */
-		select(0,NULL,NULL,NULL,NULL);
+		ast_select(0,NULL,NULL,NULL,NULL);
 	}
 	return 0;
 }
diff --git a/cdr/Makefile b/cdr/Makefile
index a1aad8cf7516968a6944136ca515dba6fd1ac933..8f05678dd6900d672a8a32b952ae0a00dfbbfef8 100755
--- a/cdr/Makefile
+++ b/cdr/Makefile
@@ -13,7 +13,7 @@
 
 MODS=cdr_csv.so
 
-CFLAGS+=
+CFLAGS+=-fPIC
 
 #
 # MySQL stuff...  Autoconf anyone??
diff --git a/channel.c b/channel.c
index 52130a8793d299dd91e78041258a6c92cff04249..40f73b843ae67602b014ff78c4bbfd8bed81fbf6 100755
--- a/channel.c
+++ b/channel.c
@@ -756,10 +756,10 @@ int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
 				max = fds[x];
 		}
 	}
-	if (*ms >= 0) 
-		res = select(max + 1, &rfds, NULL, &efds, &tv);
+	if (*ms >= 0)
+		res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
 	else
-		res = select(max + 1, &rfds, NULL, &efds, NULL);
+		res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
 
 	if (res < 0) {
 		/* Simulate a timeout if we were interrupted */
@@ -828,10 +828,10 @@ struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds,
 		if (fds[x] > max)
 			max = fds[x];
 	}
-	if (*ms >= 0) 
-		res = select(max + 1, &rfds, NULL, &efds, &tv);
+	if (*ms >= 0)
+		res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
 	else
-		res = select(max + 1, &rfds, NULL, &efds, NULL);
+		res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
 
 	if (res < 0) {
 		for (x=0;x<n;x++) 
diff --git a/channels/Makefile b/channels/Makefile
index 179a90072fd0a7437c16e0f944008bb40cd2ac2d..5b055703b457d149c813f9a579c86cafb8abd077 100755
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -11,6 +11,8 @@
 # the GNU General Public License
 #
 
+OSARCH=$(shell uname -s)
+
 CHANNEL_LIBS=chan_modem.so chan_iax.so chan_sip.so \
 	     chan_modem_aopen.so chan_oss.so \
              chan_modem_bestdata.so chan_modem_i4l.so \
@@ -41,6 +43,7 @@ ALSA_SRC=chan_alsa.c
 ALSA_SRC+=$(shell [ -f alsa-monitor.h ] && echo "alsa-monitor.h")
 
 CFLAGS+=-DCRYPTO
+CFLAGS+=-fPIC
 
 CFLAGS+=#-DVOFRDUMPER
 
@@ -76,7 +79,11 @@ ringtone.h: gentone
 chan_oss.o: chan_oss.c  busy.h ringtone.h
 
 chan_iax2.so: chan_iax2.o iax2-parser.o
+ifeq (${OSARCH},Linux)
 	$(CC) -shared -Xlinker -x -o $@ chan_iax2.o iax2-parser.o
+else
+	$(CC) -shared -Xlinker -x -o $@ chan_iax2.o iax2-parser.o -lossaudio
+endif
 
 chan_zap.o: $(CHANZAP)
 	$(CC) -c $(CFLAGS) -o chan_zap.o $(CHANZAP)
diff --git a/channels/chan_alsa.c b/channels/chan_alsa.c
index 1032811d11ca1bddddf9c70ea2897fcae79a1c9a..dfbce5722e57e6b9cbc94c811b2ff4de1e0abd3c 100755
--- a/channels/chan_alsa.c
+++ b/channels/chan_alsa.c
@@ -262,7 +262,7 @@ static void *sound_thread(void *unused)
 				max = readdev;
 		}
 #endif
-		res = select(max + 1, &rfds, &wfds, NULL, NULL);
+		res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
 		if (res < 1) {
 			ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
 			continue;
diff --git a/channels/chan_modem.c b/channels/chan_modem.c
index caa79dbb63fafbceb827314f309d41aae94b6672..90ac7c3ae13da98b47d42c0bb19483088518157d 100755
--- a/channels/chan_modem.c
+++ b/channels/chan_modem.c
@@ -609,7 +609,7 @@ static void *do_monitor(void *data)
 #endif
 		/* Wait indefinitely for something to happen */
 		pthread_testcancel();
-		res = select(n + 1, &rfds, NULL, &efds, NULL);
+		res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
 		pthread_testcancel();
 		/* Okay, select has finished.  Let's see what happened.  */
 		if (res < 1) {
diff --git a/channels/chan_oss.c b/channels/chan_oss.c
index ffbbbdef8c499da6539012b965fe9f5015e95cf5..7199d33b93c5004e07fb33e6e9bd09578c665fb1 100755
--- a/channels/chan_oss.c
+++ b/channels/chan_oss.c
@@ -245,7 +245,7 @@ static void *sound_thread(void *unused)
 			if (sounddev > max)
 				max = sounddev;
 		}
-		res = select(max + 1, &rfds, &wfds, NULL, NULL);
+		res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
 		if (res < 1) {
 			ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
 			continue;
diff --git a/channels/chan_phone.c b/channels/chan_phone.c
index 83660cdd0e2ebb9488b8479f4606206478bc06b9..80232c3c883719e634476b6d250f341751ff444d 100755
--- a/channels/chan_phone.c
+++ b/channels/chan_phone.c
@@ -848,9 +848,9 @@ static void *do_monitor(void *data)
 				tv.tv_usec = 30000;
 				tv.tv_sec = 0;
 			}
-			res = select(n + 1, &rfds, NULL, &efds, &tv);
+			res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
 		} else {
-			res = select(n + 1, &rfds, NULL, &efds, NULL);
+			res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
 			tv.tv_usec = 0;
 			tv.tv_sec = 0;
 			tonepos = 0;
diff --git a/channels/chan_vofr.c b/channels/chan_vofr.c
index f1bb5e5394bba07864231957112fe5cadeaf9fb3..4cbc71f2b901de3a2e8cb70f8c301ad590e5dc6b 100755
--- a/channels/chan_vofr.c
+++ b/channels/chan_vofr.c
@@ -564,7 +564,7 @@ retry:
 			fd_set fds;
 			FD_ZERO(&fds);
 			FD_SET(p->s, &fds);
-			select(p->s + 1, &fds, NULL, NULL, NULL);
+			ast_select(p->s + 1, &fds, NULL, NULL, NULL);
 			goto retry;
 		}
 		ast->blocking = 0;
@@ -953,7 +953,7 @@ static void *do_monitor(void *data)
 		ast_pthread_mutex_unlock(&monlock);
 		pthread_testcancel();
 		/* Wait indefinitely for something to happen */
-		res = select(n + 1, &rfds, NULL, NULL, NULL);
+		res = ast_select(n + 1, &rfds, NULL, NULL, NULL);
 		pthread_testcancel();
 		/* Okay, select has finished.  Let's see what happened.  */
 		if (res < 0) {
diff --git a/channels/chan_zap.c b/channels/chan_zap.c
index 973a086bfe0f9c8c1a7fd620ff8d38834319438b..e15ebe3a93377bf1b014e8a4cc7175a5d7418938 100755
--- a/channels/chan_zap.c
+++ b/channels/chan_zap.c
@@ -1863,7 +1863,7 @@ int	x;
 				FD_ZERO(&efds);
 				FD_SET(fd,&wfds);
 				FD_SET(fd,&efds);			
-				res = select(fd + 1,NULL,&wfds,&efds,NULL);
+				res = ast_select(fd + 1,NULL,&wfds,&efds,NULL);
 				if (!res) {
 					ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
 					continue;
@@ -4417,7 +4417,7 @@ static void *do_monitor(void *data)
 		/* Wait at least a second for something to happen */
 		tv.tv_sec = 1;
 		tv.tv_usec = 0;
-		res = select(n + 1, &rfds, NULL, &efds, &tv);
+		res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
 		pthread_testcancel();
 		/* Okay, select has finished.  Let's see what happened.  */
 		if (res < 0) {
@@ -5447,7 +5447,7 @@ static void *pri_dchannel(void *vpri)
 		pthread_mutex_unlock(&pri->lock);
 
 		e = NULL;
-		res = select(pri->fd + 1, &rfds, NULL, &efds, &tv);
+		res = ast_select(pri->fd + 1, &rfds, NULL, &efds, &tv);
 
 		ast_pthread_mutex_lock(&pri->lock);
 		if (!res) {
@@ -6982,7 +6982,7 @@ static int zt_sendtext(struct ast_channel *c, char *text)
 		FD_ZERO(&efds);
 		FD_SET(fd,&wfds);
 		FD_SET(fd,&efds);			
-		res = select(fd + 1,NULL,&wfds,&efds,NULL);
+		res = ast_select(fd + 1,NULL,&wfds,&efds,NULL);
 		if (!res) {
 			ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
 			continue;
diff --git a/channels/chan_zap_old.c b/channels/chan_zap_old.c
index 7e9b2df8e7dc1c3d2b61058130b2183a955cd169..79324cadfc75465db83baad782ebafc913e1c570 100755
--- a/channels/chan_zap_old.c
+++ b/channels/chan_zap_old.c
@@ -1673,7 +1673,7 @@ int	x;
 				FD_ZERO(&efds);
 				FD_SET(fd,&wfds);
 				FD_SET(fd,&efds);			
-				res = select(fd + 1,NULL,&wfds,&efds,NULL);
+				res = ast_select(fd + 1,NULL,&wfds,&efds,NULL);
 				if (!res) {
 					ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
 					continue;
@@ -4055,7 +4055,7 @@ static void *do_monitor(void *data)
 		/* Wait at least a second for something to happen */
 		tv.tv_sec = 1;
 		tv.tv_usec = 0;
-		res = select(n + 1, &rfds, NULL, &efds, &tv);
+		res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
 		pthread_testcancel();
 		/* Okay, select has finished.  Let's see what happened.  */
 		if (res < 0) {
@@ -5114,7 +5114,7 @@ static void *pri_dchannel(void *vpri)
 		pthread_mutex_unlock(&pri->lock);
 
 		e = NULL;
-		res = select(pri->fd + 1, &rfds, NULL, &efds, &tv);
+		res = ast_select(pri->fd + 1, &rfds, NULL, &efds, &tv);
 
 		ast_pthread_mutex_lock(&pri->lock);
 		if (!res) {
@@ -6550,7 +6550,7 @@ static int zt_sendtext(struct ast_channel *c, char *text)
 		FD_ZERO(&efds);
 		FD_SET(fd,&wfds);
 		FD_SET(fd,&efds);			
-		res = select(fd + 1,NULL,&wfds,&efds,NULL);
+		res = ast_select(fd + 1,NULL,&wfds,&efds,NULL);
 		if (!res) {
 			ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
 			continue;
diff --git a/codecs/Makefile b/codecs/Makefile
index 1ce2d333f2f79e9be3d936c7d6d4dd63fd294357..6341395d3a9d9a62841f5ed432539bcf290c8ad3 100755
--- a/codecs/Makefile
+++ b/codecs/Makefile
@@ -21,7 +21,7 @@ MODG723=$(shell [ -f g723.1/coder.c ] && echo "codec_g723_1.so")
 MODG723+=$(shell [ -f g723.1b/coder2.c ] && echo "codec_g723_1b.so")
 MODSPEEX=$(shell [ -f /usr/include/speex.h ] || [ -f /usr/local/include/speex.h ] && echo "codec_speex.so")
 MODILBC=$(shell [ -f ilbc/iLBC_decode.h ] && echo "codec_ilbc.so")
-CFLAGS+=
+CFLAGS+=-fPIC
 
 LIBG723=g723.1/libg723.a
 LIBG723B=g723.1b/libg723b.a
diff --git a/codecs/gsm/Makefile b/codecs/gsm/Makefile
index 01ce9c9214420ed18f073e2bc0354663bce96c2e..2304169ca7fe1161495ea39f1af54699ed3b19f6 100755
--- a/codecs/gsm/Makefile
+++ b/codecs/gsm/Makefile
@@ -55,7 +55,7 @@ PG =
 # CCFLAGS 	= -c -O
 
 CC		= gcc -ansi -pedantic $(OPTIMIZE) -march=$(PROC) -fschedule-insns2  -fomit-frame-pointer 
-CCFLAGS 	+= -c -DNeedFunctionPrototypes=1 -finline-functions -funroll-loops
+CCFLAGS 	+= -c -DNeedFunctionPrototypes=1 -finline-functions -funroll-loops -fPIC
 
 LD 		= $(CC)
 
@@ -175,7 +175,6 @@ GSM_SOURCES =	$(SRC)/add.c		\
 		$(SRC)/code.c		\
 		$(SRC)/debug.c		\
 		$(SRC)/decode.c		\
-		$(SRC)/k6opt.s	\
 		$(SRC)/long_term.c	\
 		$(SRC)/lpc.c		\
 		$(SRC)/preprocess.c	\
@@ -190,6 +189,9 @@ GSM_SOURCES =	$(SRC)/add.c		\
 		$(SRC)/gsm_option.c	\
 		$(SRC)/short_term.c	\
 		$(SRC)/table.c
+ifneq (${OSARCH},OpenBSD)
+GSM_SOURCES+=	$(SRC)/k6opt.s
+endif
 
 TOAST_SOURCES = $(SRC)/toast.c 		\
 		$(SRC)/toast_lin.c	\
@@ -220,7 +222,6 @@ GSM_OBJECTS =	$(SRC)/add.o		\
 		$(SRC)/code.o		\
 		$(SRC)/debug.o		\
 		$(SRC)/decode.o		\
-		$(SRC)/k6opt.o	\
 		$(SRC)/long_term.o	\
 		$(SRC)/lpc.o		\
 		$(SRC)/preprocess.o	\
@@ -235,6 +236,9 @@ GSM_OBJECTS =	$(SRC)/add.o		\
 		$(SRC)/gsm_option.o	\
 		$(SRC)/short_term.o	\
 		$(SRC)/table.o
+ifneq (${OSARCH},OpenBSD)
+GSM_OBJECTS+=	$(SRC)/k6opt.o
+endif
 
 TOAST_OBJECTS =	$(SRC)/toast.o 		\
 		$(SRC)/toast_lin.o	\
diff --git a/codecs/ilbc/Makefile b/codecs/ilbc/Makefile
index 58d35798896c785969d750e0d3c97534db05bd67..16379771e875f60dbb599a000fef9acf8deb8e49 100755
--- a/codecs/ilbc/Makefile
+++ b/codecs/ilbc/Makefile
@@ -1,3 +1,4 @@
+CFLAGS+= -fPIC -O3
 LIB=libilbc.a
 
 OBJS= anaFilter.o iCBSearch.o packing.o \
diff --git a/codecs/lpc10/Makefile b/codecs/lpc10/Makefile
index bf7f11b3f708de66d6ea709d92b1c1dc42e02496..83ffc225b96a100d9b01c0cd0f41351ac3fd0d21 100755
--- a/codecs/lpc10/Makefile
+++ b/codecs/lpc10/Makefile
@@ -22,7 +22,7 @@ LIB_TARGET_DIR = .
 # 
 
 WARNINGS = -Wall -Wno-comment -Wno-error
-CFLAGS = $(OPTIMIZE) -I$(LIB_TARGET_DIR) $(WARNINGS)
+CFLAGS = $(OPTIMIZE) -I$(LIB_TARGET_DIR) $(WARNINGS) -fPIC
 #CFLAGS+= $(shell if uname -m | grep -q 86; then echo "-mpentium" ; fi)
 
 #fix for PPC processors
diff --git a/codecs/mp3/Makefile b/codecs/mp3/Makefile
index 44ebc39d1a86e4923ec6d24cf9fd113f7a3e94c5..e8a771fca988513c31c43eb47d5a7b7c48cf40e4 100755
--- a/codecs/mp3/Makefile
+++ b/codecs/mp3/Makefile
@@ -1,7 +1,7 @@
 #
 # LMC section
 
-CFLAGS+= -I../include -Iinclude -O6 -funroll-loops -finline-functions -Wall -Wno-missing-prototypes -Wno-missing-declarations -g
+CFLAGS+= -I../include -Iinclude -O6 -funroll-loops -finline-functions -Wall -Wno-missing-prototypes -Wno-missing-declarations -g -fPIC
 RANLIB=ranlib
 
 # the XING decoder objs and dependencies:
diff --git a/db1-ast/include/db.h b/db1-ast/include/db.h
index 8b7921f975a89de1e4149f907dd3eef5cf3e18ad..f00c4df9f9acf5f23e47b0b89fe37036f58d2b42 100755
--- a/db1-ast/include/db.h
+++ b/db1-ast/include/db.h
@@ -117,14 +117,14 @@ typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
 /* Access method description structure. */
 typedef struct __db {
 	DBTYPE type;			/* Underlying db type. */
-	int (*close)	__PMT((struct __db *));
-	int (*del)	__PMT((const struct __db *, const DBT *, u_int));
-	int (*get)	__PMT((const struct __db *, const DBT *, DBT *, u_int));
-	int (*put)	__PMT((const struct __db *, DBT *, const DBT *, u_int));
-	int (*seq)	__PMT((const struct __db *, DBT *, DBT *, u_int));
-	int (*sync)	__PMT((const struct __db *, u_int));
+	int (*close)	__P((struct __db *));
+	int (*del)	__P((const struct __db *, const DBT *, u_int));
+	int (*get)	__P((const struct __db *, const DBT *, DBT *, u_int));
+	int (*put)	__P((const struct __db *, DBT *, const DBT *, u_int));
+	int (*seq)	__P((const struct __db *, DBT *, DBT *, u_int));
+	int (*sync)	__P((const struct __db *, u_int));
 	void *internal;			/* Access method private. */
-	int (*fd)	__PMT((const struct __db *));
+	int (*fd)	__P((const struct __db *));
 } DB;
 
 #define	BTREEMAGIC	0x053162
@@ -139,9 +139,9 @@ typedef struct {
 	int	minkeypage;	/* minimum keys per page */
 	u_int	psize;		/* page size */
 	int	(*compare)	/* comparison function */
-	    __PMT((const DBT *, const DBT *));
+	    __P((const DBT *, const DBT *));
 	size_t	(*prefix)	/* prefix function */
-	    __PMT((const DBT *, const DBT *));
+	    __P((const DBT *, const DBT *));
 	int	lorder;		/* byte order */
 } BTREEINFO;
 
@@ -155,7 +155,7 @@ typedef struct {
 	u_int	nelem;		/* number of elements */
 	u_int	cachesize;	/* bytes to cache */
 	u_int32_t		/* hash function */
-		(*hash) __PMT((const void *, size_t));
+		(*hash) __P((const void *, size_t));
 	int	lorder;		/* byte order */
 } HASHINFO;
 
diff --git a/db1-ast/include/mpool.h b/db1-ast/include/mpool.h
index 22c9feabd3586e2faee6380bb116884c3b0eba7c..56f17f90dbdd0da8e1f6d4596be45599f169dbfe 100755
--- a/db1-ast/include/mpool.h
+++ b/db1-ast/include/mpool.h
@@ -70,9 +70,9 @@ typedef struct MPOOL {
 	u_long	pagesize;		/* file page size */
 	int	fd;			/* file descriptor */
 					/* page in conversion routine */
-	void    (*pgin) __PMT((void *, pgno_t, void *));
+	void    (*pgin) __P((void *, pgno_t, void *));
 					/* page out conversion routine */
-	void    (*pgout) __PMT((void *, pgno_t, void *));
+	void    (*pgout) __P((void *, pgno_t, void *));
 	void	*pgcookie;		/* cookie for page in/out routines */
 #ifdef STATISTICS
 	u_long	cachehit;
diff --git a/editline/configure b/editline/configure
index f6a1c428588fb9e8d2dffbb579c3ba287763668c..ce38a97a54f94b61a90b0a37e52ac4f9d14a1c5c 100755
--- a/editline/configure
+++ b/editline/configure
@@ -841,7 +841,7 @@ echo "$ac_t""$CPP" 1>&6
 
 
 ac_aux_dir=
-for ac_dir in $srcdir $srcdir/.. $srcdir/../..; do
+for ac_dir in ${GNUSYSTEM_AUX_DIR} $srcdir $srcdir/.. $srcdir/../..; do
   if test -f $ac_dir/install-sh; then
     ac_aux_dir=$ac_dir
     ac_install_sh="$ac_aux_dir/install-sh -c"
@@ -1465,91 +1465,15 @@ fi
 done
 
 
-cat > conftest.$ac_ext <<EOF
-#line 1470 "configure"
-#include "confdefs.h"
-#include <sys/cdefs.h>
-#ifdef __RCSID
-  yes
-#endif
-
-EOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
-  egrep "yes" >/dev/null 2>&1; then
-  :
-else
-  rm -rf conftest*
-  CPPFLAGS="$CPPFLAGS '-D__RCSID(x)='"
-fi
-rm -f conftest*
-
-
-cat > conftest.$ac_ext <<EOF
-#line 1489 "configure"
-#include "confdefs.h"
-#include <sys/cdefs.h>
-#ifdef __COPYRIGHT
-  yes
-#endif
-
-EOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
-  egrep "yes" >/dev/null 2>&1; then
-  :
-else
-  rm -rf conftest*
-  CPPFLAGS="$CPPFLAGS '-D__COPYRIGHT(x)='"
-fi
-rm -f conftest*
-
-
-cat > conftest.$ac_ext <<EOF
-#line 1508 "configure"
-#include "confdefs.h"
-#include <sys/cdefs.h>
-#ifdef __RENAME
-  yes
-#endif
-
-EOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
-  egrep "yes" >/dev/null 2>&1; then
-  :
-else
-  rm -rf conftest*
-  CPPFLAGS="$CPPFLAGS '-D__RENAME(x)='"
-fi
-rm -f conftest*
-
-
-cat > conftest.$ac_ext <<EOF
-#line 1527 "configure"
-#include "confdefs.h"
-#include <sys/cdefs.h>
-#ifdef _DIAGASSERT
-  yes
-#endif
-
-EOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
-  egrep "yes" >/dev/null 2>&1; then
-  :
-else
-  rm -rf conftest*
-  CPPFLAGS="$CPPFLAGS '-D_DIAGASSERT(x)='"
-fi
-rm -f conftest*
-
-
 for ac_func in issetugid
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:1548: checking for $ac_func" >&5
+echo "configure:1472: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1553 "configure"
+#line 1477 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -1572,7 +1496,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:1576: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1500: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -1599,12 +1523,12 @@ done
 for ac_func in strlcat
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:1603: checking for $ac_func" >&5
+echo "configure:1527: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1608 "configure"
+#line 1532 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -1627,7 +1551,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:1631: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1555: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -1655,12 +1579,12 @@ done
 for ac_func in strlcpy
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:1659: checking for $ac_func" >&5
+echo "configure:1583: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1664 "configure"
+#line 1588 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -1683,7 +1607,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:1687: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1611: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -1711,12 +1635,12 @@ done
 for ac_func in fgetln
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:1715: checking for $ac_func" >&5
+echo "configure:1639: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1720 "configure"
+#line 1644 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -1739,7 +1663,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:1743: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1667: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -1767,12 +1691,12 @@ done
 for ac_func in strvis
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:1771: checking for $ac_func" >&5
+echo "configure:1695: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1776 "configure"
+#line 1700 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -1795,7 +1719,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:1799: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1723: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -1823,12 +1747,12 @@ done
 for ac_func in strunvis
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:1827: checking for $ac_func" >&5
+echo "configure:1751: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1832 "configure"
+#line 1756 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -1851,7 +1775,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:1855: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1779: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -1877,6 +1801,82 @@ fi
 done
 
 
+cat > conftest.$ac_ext <<EOF
+#line 1806 "configure"
+#include "confdefs.h"
+#include <sys/cdefs.h>
+#ifdef __RCSID
+  yes
+#endif
+
+EOF
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
+  egrep "yes" >/dev/null 2>&1; then
+  :
+else
+  rm -rf conftest*
+  CPPFLAGS="$CPPFLAGS '-D__RCSID(x)='"
+fi
+rm -f conftest*
+
+
+cat > conftest.$ac_ext <<EOF
+#line 1825 "configure"
+#include "confdefs.h"
+#include <sys/cdefs.h>
+#ifdef __COPYRIGHT
+  yes
+#endif
+
+EOF
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
+  egrep "yes" >/dev/null 2>&1; then
+  :
+else
+  rm -rf conftest*
+  CPPFLAGS="$CPPFLAGS '-D__COPYRIGHT(x)='"
+fi
+rm -f conftest*
+
+
+cat > conftest.$ac_ext <<EOF
+#line 1844 "configure"
+#include "confdefs.h"
+#include <sys/cdefs.h>
+#ifdef __RENAME
+  yes
+#endif
+
+EOF
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
+  egrep "yes" >/dev/null 2>&1; then
+  :
+else
+  rm -rf conftest*
+  CPPFLAGS="$CPPFLAGS '-D__RENAME(x)='"
+fi
+rm -f conftest*
+
+
+cat > conftest.$ac_ext <<EOF
+#line 1863 "configure"
+#include "confdefs.h"
+#include <sys/cdefs.h>
+#ifdef _DIAGASSERT
+  yes
+#endif
+
+EOF
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
+  egrep "yes" >/dev/null 2>&1; then
+  :
+else
+  rm -rf conftest*
+  CPPFLAGS="$CPPFLAGS '-D_DIAGASSERT(x)='"
+fi
+rm -f conftest*
+
+
 # Check whether --enable-readline or --disable-readline was given.
 if test "${enable_readline+set}" = set; then
   enableval="$enable_readline"
diff --git a/editline/configure.in b/editline/configure.in
index efa5e23e0c469ea58ada2f4e6da5152086895f61..72ae9180bc06f542ac151bb4955c81bbcb099ca9 100755
--- a/editline/configure.in
+++ b/editline/configure.in
@@ -76,6 +76,13 @@ AC_CHECK_HEADERS(termcap.h, , \
 
 AC_CHECK_HEADERS(sys/cdefs.h vis.h)
 
+AC_CHECK_FUNCS(issetugid)
+AC_CHECK_FUNCS(strlcat, , CCSRCS="$CCSRCS np/strlcat.c")
+AC_CHECK_FUNCS(strlcpy, , CCSRCS="$CCSRCS np/strlcpy.c")
+AC_CHECK_FUNCS(fgetln, , CCSRCS="$CCSRCS np/fgetln.c")
+AC_CHECK_FUNCS(strvis, , CCSRCS="$CCSRCS np/vis.c")
+AC_CHECK_FUNCS(strunvis, , CCSRCS="$CCSRCS np/unvis.c")
+
 AC_EGREP_CPP(yes,
 [#include <sys/cdefs.h>
 #ifdef __RCSID
@@ -104,13 +111,6 @@ AC_EGREP_CPP(yes,
 #endif
 ], , [CPPFLAGS="$CPPFLAGS '-D_DIAGASSERT(x)='"])
 
-AC_CHECK_FUNCS(issetugid)
-AC_CHECK_FUNCS(strlcat, , CCSRCS="$CCSRCS np/strlcat.c")
-AC_CHECK_FUNCS(strlcpy, , CCSRCS="$CCSRCS np/strlcpy.c")
-AC_CHECK_FUNCS(fgetln, , CCSRCS="$CCSRCS np/fgetln.c")
-AC_CHECK_FUNCS(strvis, , CCSRCS="$CCSRCS np/vis.c")
-AC_CHECK_FUNCS(strunvis, , CCSRCS="$CCSRCS np/unvis.c")
-
 dnl Enable readline compatibility by default.
 AC_ARG_ENABLE(readline, [  --disable-readline      Disable readline compatibility],
 if test "x$enable_readline" != "xyes" ; then
diff --git a/formats/Makefile b/formats/Makefile
index 17a919542232cf22020a957b8acb6164daf88b06..0cc2cb41f5b4e8f93b9eb6ef830ff1951e355e66 100755
--- a/formats/Makefile
+++ b/formats/Makefile
@@ -17,7 +17,7 @@ FORMAT_LIBS+=format_jpeg.so
 
 GSMLIB=../codecs/gsm/lib/libgsm.a
 
-CFLAGS+=
+CFLAGS+=-fPIC
 
 all: $(FORMAT_LIBS)
 
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index da849c8997feb46313fade0b4b9be3059d550869..16bb8bb5ec8f1a48f2690bae0dae2955955b1bd2 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -669,6 +669,45 @@ int ast_autoservice_start(struct ast_channel *chan);
 /*! Stop servicing a channel for us...  Returns -1 on error or if channel has been hungup */
 int ast_autoservice_stop(struct ast_channel *chan);
 
+/* Misc. functions below */
+
+//! Waits for activity on a group of channels
+/*! 
+ * \param nfds the maximum number of file descriptors in the sets
+ * \param rfds file descriptors to check for read availability
+ * \param wfds file descriptors to check for write availability
+ * \param efds file descriptors to check for exceptions (OOB data)
+ * \param tvp timeout while waiting for events
+ * This is the same as a standard select(), except it guarantees the
+ * behaviour where the passed struct timeval is updated with how much
+ * time was not slept while waiting for the specified events
+ */
+static inline int ast_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tvp)
+{
+#ifdef __linux__
+	return select(nfds, rfds, wfds, efds, tvp);
+#else
+	if (tvp) {
+		struct timeval tv, tvstart, tvend, tvlen;
+		int res;
+
+		tv = *tvp;
+		gettimeofday(&tvstart, NULL);
+		res = select(nfds, rfds, wfds, efds, tvp);
+		gettimeofday(&tvend, NULL);
+		timersub(&tvend, &tvstart, &tvlen);
+		timersub(&tv, &tvlen, tvp);
+		if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
+			tvp->tv_sec = 0;
+			tvp->tv_usec = 0;
+		}
+		return res;
+	}
+	else
+		return(nfds, rfds, wfds, efds, NULL);
+#endif
+}
+
 #ifdef DO_CRASH
 #define CRASH do { fprintf(stderr, "!! Forcing immediate crash a-la abort !!\n"); *((int *)0) = 0; } while(0)
 #else
diff --git a/manager.c b/manager.c
index 5195beeb3fffa9a768e452405425555858b0e7a4..3a2754d3825ed378e4b0494799dcca033c6ea573 100755
--- a/manager.c
+++ b/manager.c
@@ -539,7 +539,7 @@ static int get_input(struct mansession *s, char *output)
 	}
 	FD_ZERO(&fds);
 	FD_SET(s->fd, &fds);
-	res = select(s->fd + 1, &fds, NULL, NULL, NULL);
+	res = ast_select(s->fd + 1, &fds, NULL, NULL, NULL);
 	if (res < 0) {
 		ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
 	} else if (res > 0) {
diff --git a/pbx/Makefile b/pbx/Makefile
index ee9cc01df8f18ebbcd95ece7bbe646facd602ef2..27d3554195b94cda597a188b3b360dd41607bc6c 100755
--- a/pbx/Makefile
+++ b/pbx/Makefile
@@ -27,7 +27,7 @@ GTK_LIBS=`gtk-config --libs gthread`
 MOC=$(QTDIR)/bin/moc
 KDE_FLAGS=-I$(KDEDIR)/include -I$(KDEDIR)/include/kde -I$(QTDIR)/include
 KDE_LIBS=-L$(KDEDIR)/lib -L$(QTDIR)/lib -lqt -lkdecore -lkdeui
-CFLAGS+=
+CFLAGS+=-fPIC
 
 KDE_CONSOLE_OBJS=pbx_kdeconsole_main.o pbx_kdeconsole.o 
 
diff --git a/res/Makefile b/res/Makefile
index b0967f828089028eed0ed5fdee7fc9695fe9fb79..e5f6d7c37c28a2e9ca0b2a6b8649269de547e57e 100755
--- a/res/Makefile
+++ b/res/Makefile
@@ -20,7 +20,7 @@ CFLAGS+=$(shell [ -f /usr/include/linux/zaptel.h ] && echo " -DZAPATA_MOH")
 #
 # Work around buggy RedHat 9.0
 #
-CFLAGS+=-DOPENSSL_NO_KRB5
+CFLAGS+=-DOPENSSL_NO_KRB5 -fPIC
 
 all: $(MODS)
 
diff --git a/res/res_parking.c b/res/res_parking.c
index dc13af024699b91a461e45d6ed187b1b1343da48..4ffbc5166b47b9c4036e255c1c52cedbca62725a 100755
--- a/res/res_parking.c
+++ b/res/res_parking.c
@@ -443,7 +443,7 @@ std:					for (x=0;x<AST_MAX_FDS;x++) {
 		tv.tv_sec = ms / 1000;
 		tv.tv_usec = (ms % 1000) * 1000;
 		/* Wait for something to happen */
-		select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
+		ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
 		pthread_testcancel();
 	}
 	return NULL;	/* Never reached */