Original patch by Marc Abramowitz: http://marc-abramowitz.com/archives/2006/01/29/building-tsocks-on-mac-os-x/

Modified by Adam Backstrom <adam@sixohthree.com>

 - fix running tsocks with no arguments
 - include DYLD_FORCE_FLAT_NAMESPACE

diff -urN tsocks-1.8/tsocks.c tsocks-1.8-macosx/tsocks.c
--- tsocks-1.8/tsocks.c	2002-07-15 18:50:52.000000000 -0400
+++ tsocks-1.8-macosx/tsocks.c	2009-09-12 20:54:25.000000000 -0400
@@ -99,6 +99,7 @@
 static int read_socksv4_req(struct connreq *conn);
 static int read_socksv5_connect(struct connreq *conn);
 static int read_socksv5_auth(struct connreq *conn);
+void _init(void) __attribute__ ((constructor));
 
 void _init(void) {
 #ifdef USE_OLD_DLSYM
@@ -191,9 +192,10 @@
 	struct sockaddr_in *connaddr;
 	struct sockaddr_in peer_address;
 	struct sockaddr_in server_address;
-   int gotvalidserver = 0, rc, namelen = sizeof(peer_address);
+   int gotvalidserver = 0, rc;
 	int sock_type = -1;
-	int sock_type_len = sizeof(sock_type);
+	socklen_t sock_type_len = sizeof(sock_type);
+	socklen_t namelen = sizeof(peer_address);
 	unsigned int res = -1;
 	struct serverent *path;
    struct connreq *newconn;
diff -urN tsocks-1.8/Makefile.in tsocks-1.8-macosx/Makefile.in
--- tsocks-1.8/Makefile.in	2002-03-16 05:12:40.000000000 -0500
+++ tsocks-1.8-macosx/Makefile.in	2009-09-12 20:54:25.000000000 -0400
@@ -1,6 +1,7 @@
 # Makefile used by configure to create real Makefile
 
 CC=@CC@
+SHLIB_EXT=@SHLIB_EXT@
 prefix=@prefix@
 exec_prefix = @exec_prefix@
 libexecdir = @libexecdir@
@@ -23,11 +24,12 @@
 SCRIPT = tsocks
 SHLIB_MAJOR = 1
 SHLIB_MINOR = 8
-SHLIB = ${LIB_NAME}.so.${SHLIB_MAJOR}.${SHLIB_MINOR}
+SHLIB = ${LIB_NAME}.${SHLIB_EXT}.${SHLIB_MAJOR}.${SHLIB_MINOR}
 
 INSTALL = @INSTALL@
 INSTALL_DATA = @INSTALL_DATA@
 CFLAGS = @CFLAGS@
+LDFLAGS = @LDFLAGS@
 INCLUDES = -I.
 LIBS = @LIBS@
 SPECIALLIBS = @SPECIALLIBS@
@@ -47,11 +49,11 @@
 	${SHCC} ${CFLAGS} ${INCLUDES} -o ${INSPECT} ${INSPECT}.c ${COMMON}.o ${LIBS} 
 
 ${SAVE}: ${SAVE}.c
-	${SHCC} ${CFLAGS} ${INCLUDES} -static -o ${SAVE} ${SAVE}.c
+	${SHCC} ${CFLAGS} ${INCLUDES} -o ${SAVE} ${SAVE}.c
 
 ${SHLIB}: ${OBJS} ${COMMON}.o ${PARSER}.o
-	${SHCC} ${CFLAGS} ${INCLUDES} -nostdlib -shared -o ${SHLIB} ${OBJS} ${COMMON}.o ${PARSER}.o ${DYNLIB_FLAGS} ${SPECIALLIBS} ${LIBS}
-	ln -sf ${SHLIB} ${LIB_NAME}.so
+	${SHCC} ${CFLAGS} ${INCLUDES} -nostdlib ${LDFLAGS} -o ${SHLIB} ${OBJS} ${COMMON}.o ${PARSER}.o ${DYNLIB_FLAGS} ${SPECIALLIBS} ${LIBS}
+	ln -sf ${SHLIB} ${LIB_NAME}.${SHLIB_EXT}
 
 %.so: %.c
 	${SHCC} ${CFLAGS} ${INCLUDES} -c ${CC_SWITCHES} $< -o $@
@@ -68,8 +70,8 @@
 installlib:
 	${MKINSTALLDIRS} "${DESTDIR}${libdir}"
 	${INSTALL} ${SHLIB} ${DESTDIR}${libdir}
-	ln -sf ${SHLIB} ${DESTDIR}${libdir}/${LIB_NAME}.so.${SHLIB_MAJOR}
-	ln -sf ${LIB_NAME}.so.${SHLIB_MAJOR} ${DESTDIR}${libdir}/${LIB_NAME}.so
+	ln -sf ${SHLIB} ${DESTDIR}${libdir}/${LIB_NAME}.${SHLIB_EXT}.${SHLIB_MAJOR}
+	ln -sf ${LIB_NAME}.${SHLIB_EXT}.${SHLIB_MAJOR} ${DESTDIR}${libdir}/${LIB_NAME}.${SHLIB_EXT}
 
 installman:
 	${MKINSTALLDIRS} "${DESTDIR}${mandir}/man1"
@@ -80,7 +82,7 @@
 	${INSTALL_DATA} tsocks.conf.5 ${DESTDIR}${mandir}/man5/
 	
 clean:
-	-rm -f *.so *.so.* *.o *~ ${TARGETS}
+	-rm -f *.so *.so.* *.dylib *.dylib.* *.o *~ ${TARGETS}
 
 distclean: clean
 	-rm -f config.cache config.log config.h Makefile
diff -urN tsocks-1.8/configure.in tsocks-1.8-macosx/configure.in
--- tsocks-1.8/configure.in	2002-07-15 18:51:03.000000000 -0400
+++ tsocks-1.8-macosx/configure.in	2009-09-12 20:54:25.000000000 -0400
@@ -33,6 +33,12 @@
   AC_DEFINE_UNQUOTED(CONF_FILE, "/etc/tsocks.conf")
 ])
 
+dnl -----------------------------------
+dnl On Mac OS X, gcc-4.0 leads to link errors; must use gcc-3.3
+dnl -----------------------------------
+if test -x "/usr/bin/gcc-3.3"; then
+    CC=gcc-3.3
+fi
 
 dnl -----------------------------------
 dnl Get hostname and other information.
@@ -312,7 +318,7 @@
 dnl Find the correct poll prototype on this machine 
 AC_MSG_CHECKING(for correct poll prototype)
 PROTO=
-for testproto in 'struct pollfd *ufds, unsigned long nfds, int timeout' 
+for testproto in 'struct pollfd *ufds, nfds_t nfds, int timeout'
 do
   if test "${PROTO}" = ""; then
     AC_TRY_COMPILE([
@@ -332,4 +338,42 @@
 AC_SUBST(SPECIALLIBS)
 LIBS=${SIMPLELIBS}
 
-AC_OUTPUT(Makefile)
+dnl Check if the linker accepts -dynamiclib; necessary on Mac OS X
+AC_MSG_CHECKING(if the linker accepts -dynamiclib)
+OLDLDFLAGS="$LDFLAGS"
+LDFLAGS="$LDFLAGS -dynamiclib"
+AC_TRY_COMPILE(,,AC_MSG_RESULT(yes),[
+   LDFLAGS="$OLDLDFLAGS"
+   AC_MSG_RESULT(no)])
+
+dnl Check if the linker accepts -multiply_defined suppress; necessary on Mac OS X
+AC_MSG_CHECKING(if the linker accepts -multiply_defined suppress)
+OLDLDFLAGS="$LDFLAGS"
+LDFLAGS="$LDFLAGS -multiply_defined suppress"
+AC_TRY_COMPILE(,,AC_MSG_RESULT(yes),[
+   LDFLAGS="$OLDLDFLAGS"
+   AC_MSG_RESULT(no)])
+
+dnl Check if the linker accepts -single_module; necessary on Mac OS X
+AC_MSG_CHECKING(if the linker accepts -single_module)
+OLDLDFLAGS="$LDFLAGS"
+SHLIB_EXT="so"
+LD_PRELOAD="LD_PRELOAD"
+LDFLAGS="$LDFLAGS -single_module"
+AC_TRY_COMPILE(,,
+   [
+      SHLIB_EXT="dylib" 
+      LD_PRELOAD="DYLD_INSERT_LIBRARIES"
+      AC_MSG_RESULT(yes)
+   ], [
+      LDFLAGS="$OLDLDFLAGS"
+      AC_MSG_RESULT(no)
+   ]
+)
+
+AC_SUBST(SHLIB_EXT)
+AC_SUBST(LD_PRELOAD)
+AC_CONFIG_FILES([Makefile])
+AC_CONFIG_FILES([tsocks], [chmod +x tsocks])
+AC_OUTPUT
+
diff -urN tsocks-1.8/tsocks.in tsocks-1.8-macosx/tsocks.in
--- tsocks-1.8/tsocks.in	1969-12-31 19:00:00.000000000 -0500
+++ tsocks-1.8-macosx/tsocks.in	2009-09-12 21:03:34.000000000 -0400
@@ -0,0 +1,106 @@
+#!/bin/sh
+# Wrapper script for use of the tsocks(8) transparent socksification library
+#
+# There are three forms of usage for this script:
+#
+# /usr/bin/tsocks program [program arguments...]
+#
+# This form sets the users @LD_PRELOAD@ environment variable so that tsocks(8) 
+# will be loaded to socksify the application then executes the specified 
+# program (with the provided arguments). The following simple example might 
+# be used to telnet to www.foo.org via a tsocks.conf(5) configured socks server:
+#
+# /usr/bin/tsocks telnet www.foo.org
+#
+# The second form allows for tsocks(8) to be switched on and off for a 
+# session (that is, it adds and removes tsocks from the @LD_PRELOAD@ environment
+# variable). This form must be _sourced_ into the user's existing session
+# (and will only work with bourne shell users):
+#
+# . /usr/bin/tsocks on
+# telnet www.foo.org 
+# . /usr/bin/tsocks off
+# 
+# Or
+# 
+# source /usr/bin/tsocks on
+# telnet www.foo.org
+# source /usr/bin/tsocks off
+#
+# The third form creates a new shell with @LD_PRELOAD@ set and is achieved
+# simply by running the script with no arguments 
+# 
+# /usr/bin/tsocks
+#
+# When finished the user can simply terminate the shell with 'exit'
+# 
+# This script is originally from the debian tsocks package by 
+# Tamas Szerb <toma@rulez.org>
+
+LIBDIR="@libdir@"
+LIB_NAME="libtsocks"
+SHLIB_EXT="@SHLIB_EXT@"
+SHLIB="${LIBDIR}/${LIB_NAME}.${SHLIB_EXT}"
+
+# mac os x specific
+export DYLD_FORCE_FLAT_NAMESPACE=1
+
+if [ $# = 0 ] ; then
+    @LD_PRELOAD@="${SHLIB}" exec ${SHELL:-/bin/sh}
+fi
+
+function show() {
+    echo "@LD_PRELOAD@         = \"$@LD_PRELOAD@\""
+}
+        
+case "$1" in
+	on)
+		if [ -z "$@LD_PRELOAD@" ]
+			then
+                echo "Setting @LD_PRELOAD@ ..."
+				export @LD_PRELOAD@="${SHLIB}"
+			else
+                echo "Appending to @LD_PRELOAD@ ..."
+				echo $@LD_PRELOAD@ | grep -q "${SHLIB}" || \
+				export @LD_PRELOAD@="${SHLIB} $@LD_PRELOAD@"
+		fi
+        show
+	;;
+	off)
+        echo "Removing ${SHLIB} from @LD_PRELOAD@ ..."
+		export @LD_PRELOAD@=`echo -n $@LD_PRELOAD@ | sed 's#@libdir@/libtsocks\.@SHLIB_EXT@ *##'`
+		if [ -z "$@LD_PRELOAD@" ]
+			then
+                unset DYLD_FORCE_FLAT_NAMESPACE
+				unset @LD_PRELOAD@
+		fi
+        show
+	;;
+	show|sh)
+		show
+	;;
+	-h|-?)
+      echo "$0: Please see tsocks(1) or read comment at top of $0"
+   ;;
+	*)
+		if [ -z "$@LD_PRELOAD@" ]
+		then
+			export @LD_PRELOAD@="${SHLIB}"
+		else
+			echo $@LD_PRELOAD@ | grep -q "${SHLIB}" || \
+			export @LD_PRELOAD@="${SHLIB} $@LD_PRELOAD@"
+		fi
+
+		if [ $# = 0 ]
+		then
+			${SHELL:-/bin/sh}
+		fi
+
+		if [ $# -gt 0 ]
+		then
+			exec "$@"
+		fi
+	;;
+esac
+
+#EOF

