[Toybox] [PATCH] toybox compiled on OS X 10.6

Georgi Chorbadzhiyski gf at unixsol.org
Tue Feb 28 04:09:48 PST 2012


Apparently I have too much free time on my hands, so with the attached patches
I'm able to compile toybox with allyesconfig on OS X 10.6.

What is needed:
  - First install gsed (port install gsed), the sed that comes with OS X can
    not generate needed header files. It spews lots errors:

> sed: 2: "p\n": undefined label 'got;b;:got'
> sed: 2: "p\n": undefined label 'got;b;:got'
> Extract help text from Config.in.
> Make generated/config.h from .config.
> sed: illegal option -- r
> usage: sed script [-Ealn] [-i extension] [file ...]
>        sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

What is disabled:
  - The following toys: dmesg, free, insmod, mdev, rmmod, swapoff, swapon, uptime
  - getmountlist() is disabled and this breaks df. Probably can be fixed.
  - dmesg, free and uptime can be ported to OS X without much trouble IMHO.

What is broken:
  - toysh and xargs.
OS X's libc do not support getline and getdelim, so I just ifdef'ed them in xargs
and toysh.

What is useful:
  - Patch 0001 that allows sed command to be changed.
  - Patch 0002 that replaces dprintf(...) with fprintf(stderr, ...).
    Possible incompatabilities:
       yesno() in lib/lib.c
       Something in patch.c:
        -  fdprintf(TT.state == 2 ? 2 : TT.fileout,
        +  fprintf(TT.state == 2 ? stderr : stdout,

What I'm going to do with this patches? Depends on what Rob thinks. Currently they
are just a quick hack. I'm certainly not any kind of OS X (or BSD) expert or have
much interest in these OSes. I can probably clean the patches some more and if they
are applied do some kind of testing from time to time, compiling toybox on OS X.

-- 
Georgi Chorbadzhiyski
http://georgi.unixsol.org/
-------------- next part --------------
From b9713ee0280989ad15f06544abd4da7011992c4a Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 28 Feb 2012 12:38:21 +0200
Subject: [PATCH 1/6] Allow SED to be overwritten in configure.

---
 configure       |    1 +
 scripts/make.sh |   16 ++++++++--------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 9b7bb36..bb855fe 100644
--- a/configure
+++ b/configure
@@ -12,3 +12,4 @@ CFLAGS="$CFLAGS -funsigned-char"
 CC="${CROSS_COMPILE}${CC}"
 [ -z "$STRIP" ] && STRIP="${CROSS_COMPILE}strip"
 [ -z "$HOSTCC" ] && HOSTCC=gcc
+[ -z "$SED" ] && SED=sed
diff --git a/scripts/make.sh b/scripts/make.sh
index e2236ea..0558466 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -18,12 +18,12 @@ function newtoys()
 {
   for i in toys/*.c
   do
-    sed -n -e '1,/^config [A-Z]/s/^USE_/&/p' $i || exit 1
+    $SED -n -e '1,/^config [A-Z]/s/^USE_/&/p' $i || exit 1
   done
 }
 echo "NEWTOY(toybox, NULL, 0)" > generated/newtoys.h
-newtoys | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
-	| sed 's/[^ ]* //'  >> generated/newtoys.h
+newtoys | $SED 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
+	| $SED 's/[^ ]* //'  >> generated/newtoys.h
 
 # Extract global structure definitions from toys/*.c
 
@@ -31,10 +31,10 @@ function getglobals()
 {
   for i in toys/*.c
   do
-    NAME="$(echo $i | sed 's at toys/\(.*\)\.c@\1@')"
+    NAME="$(echo $i | $SED 's at toys/\(.*\)\.c@\1@')"
 
     echo -e "// $i\n"
-    sed -n -e '/^DEFINE_GLOBALS(/,/^)/b got;b;:got' \
+    $SED -n -e '/^DEFINE_GLOBALS(/,/^)/b got;b;:got' \
         -e 's/^DEFINE_GLOBALS(/struct '"$NAME"'_data {/' \
         -e 's/^)/};/' -e 'p' $i
   done
@@ -45,7 +45,7 @@ GLOBSTRUCT="$(getglobals)"
   echo "$GLOBSTRUCT"
   echo
   echo "extern union global_union {"
-  echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/	struct \1_data \1;/p'
+  echo "$GLOBSTRUCT" | $SED -n 's/struct \(.*\)_data {/	struct \1_data \1;/p'
   echo "} this;"
 ) > generated/globals.h
 
@@ -62,7 +62,7 @@ echo "Make generated/config.h from .config."
 # New ones have '\n' so can replace one line with two without all the branches
 # and tedious mucking about with hold space.
 
-sed -n \
+$SED -n \
   -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
   -e 't notset' \
   -e 's/^CONFIG_\(.*\)=y.*/\1/' \
@@ -92,7 +92,7 @@ sed -n \
 # 4) Remove toybox itself from the list (as that indicates global symbols).
 # 5) Add "toys/" prefix and ".c" suffix.
 
-TOYFILES=$(cat .config | sed -nre 's/^CONFIG_(.*)=y/\1/;t skip;b;:skip;s/_.*//;p' | sort -u | tr A-Z a-z | grep -v '^toybox$' | sed 's@\(.*\)@toys/\1.c@' )
+TOYFILES=$(cat .config | $SED -nre 's/^CONFIG_(.*)=y/\1/;t skip;b;:skip;s/_.*//;p' | sort -u | tr A-Z a-z | grep -v '^toybox$' | $SED 's@\(.*\)@toys/\1.c@' )
 
 echo "Compile toybox..."
 
-- 
1.7.5.1

-------------- next part --------------
From 4af9eb799de7b1383c04ef2228e5eecd9eaca1e5 Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 28 Feb 2012 13:37:00 +0200
Subject: [PATCH 2/6] Stop using dprintf.

---
 lib/lib.c         |    2 +-
 lib/portability.h |    3 ---
 toys/cmp.c        |    2 +-
 toys/count.c      |    4 ++--
 toys/patch.c      |   22 +++++++++++-----------
 toys/sed.c        |    2 +-
 6 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index cc441f9..e4485ba 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -800,7 +800,7 @@ int yesno(char *prompt, int def)
 	for (i=0; i<3 && !isatty(i); i++);
 	if (i == 3) return 1;
 
-	fdprintf(i, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
+	fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
 	while (read(i, &buf, 1)) {
 		if (isspace(buf)) break;
 		if (tolower(buf) == 'y') return 1;
diff --git a/lib/portability.h b/lib/portability.h
index d4edfb6..55389c4 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -7,10 +7,7 @@
 
 #undef _FORTIFY_SOURCE
 
-// Humor glibc to get dprintf, then #define it to something more portable.
 #define _GNU_SOURCE
-#include <stdio.h>
-#define fdprintf(...) dprintf(__VA_ARGS__)
 
 #ifdef __GNUC__
 #define noreturn	__attribute__((noreturn))
diff --git a/toys/cmp.c b/toys/cmp.c
index 471952b..f156ce0 100644
--- a/toys/cmp.c
+++ b/toys/cmp.c
@@ -73,7 +73,7 @@ void do_cmp(int fd, char *name)
 		}
 		if (len1 != len2) {
 			if (!(toys.optflags & FLAG_s)) {
-				fdprintf(2, "cmp: EOF on %s\n",
+				fprintf(stderr, "cmp: EOF on %s\n",
 					len1 < len2 ? TT.name : name);
 			}
 			toys.exitval = 1;
diff --git a/toys/count.c b/toys/count.c
index acc0c69..287d3fb 100644
--- a/toys/count.c
+++ b/toys/count.c
@@ -29,7 +29,7 @@ void count_main(void)
 		if (!len) break;
 		size += len;
 		xwrite(1, toybuf, len);
-		fdprintf(2, "%"PRIu64" bytes\r", size);
+		fprintf(stderr, "%"PRIu64" bytes\r", size);
 	}
-	fdprintf(2,"\n");
+	fprintf(stderr, "\n");
 }
diff --git a/toys/patch.c b/toys/patch.c
index 38c7194..5449fea 100644
--- a/toys/patch.c
+++ b/toys/patch.c
@@ -76,10 +76,10 @@ static void do_line(void *data)
 	struct double_list *dlist = (struct double_list *)data;
 
 	if (TT.state>1 && *dlist->data != TT.state)
-		fdprintf(TT.state == 2 ? 2 : TT.fileout,
+		fprintf(TT.state == 2 ? stderr : stdout,
 			"%s\n", dlist->data+(TT.state>3 ? 1 : 0));
 
-	if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
+	if (PATCH_DEBUG) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
 
 	free(dlist->data);
 	free(data);
@@ -96,7 +96,7 @@ static void fail_hunk(void)
 	if (!TT.current_hunk) return;
 	TT.current_hunk->prev->next = 0;
 
-	fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline);
+	fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline);
 	toys.exitval = 1;
 
 	// If we got to this point, we've seeked to the end.  Discard changes to
@@ -128,11 +128,11 @@ static int apply_one_hunk(void)
 	for (plist = TT.current_hunk; plist; plist = plist->next) {
 		if (plist->data[0]==' ') matcheof++;
 		else matcheof = 0;
-		if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data);
+		if (PATCH_DEBUG) fprintf(stderr, "HUNK:%s\n", plist->data);
 	}
 	matcheof = matcheof < TT.context;
 
-	if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
+	if (PATCH_DEBUG) fprintf(stderr, "MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
 
 	// Loop through input data searching for this hunk.  Match all context
 	// lines and all lines to be removed until we've found the end of a
@@ -155,19 +155,19 @@ static int apply_one_hunk(void)
 
 		// Is this EOF?
 		if (!data) {
-			if (PATCH_DEBUG) fdprintf(2, "INEOF\n");
+			if (PATCH_DEBUG) fprintf(stderr, "INEOF\n");
 
 			// Does this hunk need to match EOF?
 			if (!plist && matcheof) break;
 
 			if (backwarn)
-				fdprintf(2,"Possibly reversed hunk %d at %ld\n",
+				fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
 						TT.hunknum, TT.linenum);
 
 			// File ended before we found a place for this hunk.
 			fail_hunk();
 			goto done;
-		} else if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data);
+		} else if (PATCH_DEBUG) fprintf(stderr, "IN: %s\n", data);
 		check = dlist_add(&buf, data);
 
 		// Compare this line with next expected line of hunk.
@@ -186,7 +186,7 @@ static int apply_one_hunk(void)
 				// recheck remaining buffered data for a new match.
 	
 				if (PATCH_DEBUG)
-					fdprintf(2, "NOT: %s\n", plist->data);
+					fprintf(stderr, "NOT: %s\n", plist->data);
 
 				TT.state = 3;
 				check = llist_pop(&buf);
@@ -204,7 +204,7 @@ static int apply_one_hunk(void)
 				check = buf;
 			} else {
 				if (PATCH_DEBUG)
-					fdprintf(2, "MAYBE: %s\n", plist->data);
+					fprintf(stderr, "MAYBE: %s\n", plist->data);
 				// This line matches.  Advance plist, detect successful match.
 				plist = plist->next;
 				if (!plist && !matcheof) goto out;
@@ -257,7 +257,7 @@ void patch_main(void)
 		if (strip || !patchlinenum++) {
 			int len = strlen(patchline);
 			if (patchline[len-1] == '\r') {
-				if (!strip) fdprintf(2, "Removing DOS newlines\n");
+				if (!strip) fprintf(stderr, "Removing DOS newlines\n");
 				strip = 1;
 				patchline[len-1]=0;
 			}
diff --git a/toys/sed.c b/toys/sed.c
index c4eb1a4..55eeaed 100644
--- a/toys/sed.c
+++ b/toys/sed.c
@@ -57,7 +57,7 @@ void sed_main(void)
 	struct arg_list *test;
 
 	for (test = TT.commands; test; test = test->next)
-		dprintf(2,"command=%s\n",test->arg);
+		fprintf(stderr, "command=%s\n", test->arg);
 
 	printf("Hello world\n");
 }
-- 
1.7.5.1

-------------- next part --------------
From 108e1b842b185ae7ec976a6d51c4d3b10674f859 Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 28 Feb 2012 12:54:02 +0200
Subject: [PATCH 3/6] OS X 10.6 ships with gcc 4.2.1 which do not support
 --gc-sections and --as-needed linker options.

---
 configure       |    6 +++++-
 scripts/make.sh |    2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index bb855fe..d68764e 100644
--- a/configure
+++ b/configure
@@ -7,7 +7,11 @@
 
 [ -z "$CFLAGS" ] && CFLAGS="-Wall -Wundef -Wno-char-subscripts"
 CFLAGS="$CFLAGS -funsigned-char"
-[ -z "$OPTIMIZE" ] && OPTIMIZE="-Os -ffunction-sections -fdata-sections -Wl,--gc-sections"
+if [ "$(uname -s)" = "Linux" ]
+then
+	[ -z "$LDFLAGS" ] && LDFLAGS="-Wl,--as-needed,-lutil,--no-as-needed -Wl,--gc-sections"
+fi
+[ -z "$OPTIMIZE" ] && OPTIMIZE="-Os -ffunction-sections -fdata-sections"
 [ -z "$CC" ] && CC="cc"
 CC="${CROSS_COMPILE}${CC}"
 [ -z "$STRIP" ] && STRIP="${CROSS_COMPILE}strip"
diff --git a/scripts/make.sh b/scripts/make.sh
index 0558466..daeafb8 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -103,7 +103,7 @@ do_loudly()
 }
 
 do_loudly $CC $CFLAGS -I . -o toybox_unstripped $OPTIMIZE main.c lib/*.c \
-  $TOYFILES -Wl,--as-needed,-lutil,--no-as-needed || exit 1
+  $TOYFILES $LDFLAGS || exit 1
 do_loudly $STRIP toybox_unstripped -o toybox || exit 1
 # gcc 4.4's strip command is buggy, and doesn't set the executable bit on
 # its output the way SUSv4 suggests it do so.
-- 
1.7.5.1

-------------- next part --------------
From c97b024a9225156709b9ca525bcda2a6b1b4868d Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 28 Feb 2012 13:24:04 +0200
Subject: [PATCH 4/6] Build some toys only on Linux.

free, dmesg and uptime can be ported to OS X instead of disabling them.
---
 lib/getmountlist.c |   10 ++++++++++
 toys/dmesg.c       |    7 +++++++
 toys/free.c        |    5 +++++
 toys/insmod.c      |    2 ++
 toys/mdev.c        |    5 +++++
 toys/rmmod.c       |    5 +++++
 toys/swapoff.c     |    2 ++
 toys/swapon.c      |    2 ++
 toys/uptime.c      |    2 ++
 9 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/lib/getmountlist.c b/lib/getmountlist.c
index 1b23544..e3743f0 100644
--- a/lib/getmountlist.c
+++ b/lib/getmountlist.c
@@ -6,6 +6,14 @@
 
 #include "toys.h"
 
+#ifndef __linux__
+struct mtab_list *getmountlist(int die)
+{
+	if (die) error_exit("%s is unsupported.", __func__);
+	return NULL;
+}
+#else
+
 #include <mntent.h>
 
 char *path_mounts = "/proc/mounts";
@@ -41,3 +49,5 @@ struct mtab_list *getmountlist(int die)
 	}
 	return mtlist;
 }
+
+#endif
diff --git a/toys/dmesg.c b/toys/dmesg.c
index 95b023d..6f9d39a 100644
--- a/toys/dmesg.c
+++ b/toys/dmesg.c
@@ -22,6 +22,11 @@ config DMESG
 */
 
 #include "toys.h"
+
+#ifndef __linux__
+void dmesg_main(void) { }
+#else
+
 #include <sys/klog.h>
 
 DEFINE_GLOBALS(
@@ -57,3 +62,5 @@ void dmesg_main(void)
 		if (CFG_TOYBOX_FREE) free(data);
 	}
 }
+
+#endif
diff --git a/toys/free.c b/toys/free.c
index 229e5e9..c356465 100644
--- a/toys/free.c
+++ b/toys/free.c
@@ -21,6 +21,10 @@ config FREE
 
 #include "toys.h"
 
+#ifndef __linux__
+void free_main(void) { }
+#else
+
 static unsigned long long convert(unsigned long d, unsigned int iscale,
 				unsigned int oscale)
 {
@@ -57,3 +61,4 @@ void free_main(void)
 		convert(info.totalswap - info.freeswap, iscale, oscale),
 		convert(info.freeswap, iscale, oscale));
 }
+#endif
diff --git a/toys/insmod.c b/toys/insmod.c
index e794828..f9816a2 100644
--- a/toys/insmod.c
+++ b/toys/insmod.c
@@ -24,6 +24,7 @@ config INSMOD
 
 void insmod_main(void)
 {
+#ifdef __linux__
 	char * buf = NULL;
 	int len, res, i;
 	int fd = xopen(toys.optargs[0], O_RDONLY);
@@ -44,4 +45,5 @@ void insmod_main(void)
 
 	if (res)
 		perror_exit("failed to load %s", toys.optargs[0]);
+#endif
 }
diff --git a/toys/mdev.c b/toys/mdev.c
index b644408..e2e0b16 100644
--- a/toys/mdev.c
+++ b/toys/mdev.c
@@ -36,6 +36,10 @@ config MDEV_CONF
 #include "toys.h"
 #include "lib/xregcomp.h"
 
+#ifndef __linux__
+void mdev_main(void) { }
+#else
+
 // mknod in /dev based on a path like "/sys/block/hda/hda1"
 static void make_device(char *path)
 {
@@ -213,3 +217,4 @@ void mdev_main(void)
 
 	// hotplug support goes here
 }
+#endif
diff --git a/toys/rmmod.c b/toys/rmmod.c
index d730b45..ea310c1 100644
--- a/toys/rmmod.c
+++ b/toys/rmmod.c
@@ -22,6 +22,10 @@ config RMMOD
 
 #include "toys.h"
 
+#ifndef __linux__
+void rmmod_main(void) { }
+#else
+
 #include <sys/syscall.h>
 #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
 
@@ -49,3 +53,4 @@ void rmmod_main(void)
 	if (delete_module(mod_name, flags))
 		perror_exit("failed to unload %s", mod_name);
 }
+#endif
diff --git a/toys/swapoff.c b/toys/swapoff.c
index fba0de8..d0a3538 100644
--- a/toys/swapoff.c
+++ b/toys/swapoff.c
@@ -21,6 +21,8 @@ config SWAPOFF
 
 void swapoff_main(void)
 {
+#ifdef __linux__
 	if (swapoff(toys.optargs[0]))
 		perror_exit("failed to remove swaparea");
+#endif
 }
diff --git a/toys/swapon.c b/toys/swapon.c
index 16ce8d1..d1e7128 100644
--- a/toys/swapon.c
+++ b/toys/swapon.c
@@ -27,6 +27,7 @@ DEFINE_GLOBALS(
 
 void swapon_main(void)
 {
+#ifdef __linux__
 	int flags = 0;
 
 	if (toys.optflags & 1)
@@ -34,4 +35,5 @@ void swapon_main(void)
 
 	if (swapon(*toys.optargs, flags))
 		perror_exit("Couldn't swapon '%s'", *toys.optargs);
+#endif
 }
diff --git a/toys/uptime.c b/toys/uptime.c
index 83fb6b1..0750822 100644
--- a/toys/uptime.c
+++ b/toys/uptime.c
@@ -22,6 +22,7 @@ config UPTIME
 
 void uptime_main(void)
 {
+#ifdef __linux__
 	struct sysinfo info;
 	time_t tmptime;
 	struct tm * now;
@@ -49,4 +50,5 @@ void uptime_main(void)
 	printf(" load average: %.02f %.02f %.02f\n", info.loads[0]/65536.0,
 		info.loads[1]/65536.0, info.loads[2]/65536.0);
 
+#endif
 }
-- 
1.7.5.1

-------------- next part --------------
From 9290871852fd2b1e823a414cf6e4c07ae8613673 Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 28 Feb 2012 13:29:04 +0200
Subject: [PATCH 5/6] Port some toys to OS X.

---
 lib/lib.c     |    8 ++++++++
 toys.h        |   11 ++++++++---
 toys/env.c    |    4 ++++
 toys/netcat.c |   10 ++++++++++
 toys/oneit.c  |    3 +++
 5 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index e4485ba..a9827dc 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -497,6 +497,10 @@ long atolx(char *numstr)
 	return val;
 }
 
+#ifdef __APPLE__
+#include <sys/disk.h>
+#endif
+
 // Return how long the file at fd is, if there's any way to determine it.
 off_t fdlength(int fd)
 {
@@ -505,7 +509,11 @@ off_t fdlength(int fd)
 
 	// If the ioctl works for this, return it.
 
+#ifdef __APPLE__
+	if (ioctl(fd, DKIOCGETBLOCKSIZE, &size) >= 0) return size*512L;
+#else
 	if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L;
+#endif
 
 	// If not, do a binary search for the last location we can read.  (Some
 	// block devices don't do BLKGETSIZE right.)  This should probably have
diff --git a/toys.h b/toys.h
index 504e0dc..2a251be 100644
--- a/toys.h
+++ b/toys.h
@@ -15,7 +15,6 @@
 #include <grp.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <pty.h>
 #include <pwd.h>
 #include <setjmp.h>
 #include <stdarg.h>
@@ -23,18 +22,24 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <libgen.h> // for basename
+#include <signal.h> // for kill
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/statvfs.h>
-#include <sys/sysinfo.h>
-#include <sys/swap.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <utime.h>
 
+#ifdef __linux__
+#include <pty.h>
+#include <sys/swap.h>
+#include <sys/sysinfo.h>
+#endif
+
 #undef _XOPEN_SOURCE
 #define _XOPEN_SOURCE 600
 #include <time.h>
diff --git a/toys/env.c b/toys/env.c
index b23d98a..7d8616a 100644
--- a/toys/env.c
+++ b/toys/env.c
@@ -22,7 +22,11 @@ void env_main(void)
     char **command = NULL;
     char *del = "=";
     
+#ifdef __linux__
     if (toys.optflags & 1) clearenv();
+#else
+    if (toys.optflags & 1) *environ = NULL;
+#endif
     
     for (ev = toys.optargs; *ev != NULL; ev++) {
         char *env, *val = NULL;
diff --git a/toys/netcat.c b/toys/netcat.c
index 1b51337..c023981 100644
--- a/toys/netcat.c
+++ b/toys/netcat.c
@@ -45,6 +45,10 @@ config NETCAT_LISTEN
 #include "toys.h"
 #include "toynet.h"
 
+#ifdef __APPLE__
+#include <util.h> // for forkpty
+#endif
+
 DEFINE_GLOBALS(
 	char *filename;        // -f read from filename instead of network
 	long quit_delay;       // -q Exit after EOF from stdin after # seconds.
@@ -82,6 +86,12 @@ static void lookup_name(char *name, uint32_t *result)
 	*result = *(uint32_t *)*hostbyname->h_addr_list;
 }
 
+#ifdef __APPLE__
+static inline unsigned short bswap_16(unsigned short x) {
+	return (x>>8) | (x<<8);
+}
+#endif
+
 // Worry about a fancy lookup later.
 static void lookup_port(char *str, uint16_t *port)
 {
diff --git a/toys/oneit.c b/toys/oneit.c
index 4593591..d88a0ed 100644
--- a/toys/oneit.c
+++ b/toys/oneit.c
@@ -59,6 +59,9 @@ void oneit_main(void)
     while (pid!=wait(&i));
     sync();
 
+#ifndef RB_POWER_OFF
+#define RB_POWER_OFF RB_AUTOBOOT
+#endif
 	// PID 1 can't call reboot() because it kills the task that calls it,
 	// which causes the kernel to panic before the actual reboot happens.
 	if (!vfork()) reboot((toys.optflags&1) ? RB_POWER_OFF : RB_AUTOBOOT);
-- 
1.7.5.1

-------------- next part --------------
From f3ce2ac6bd295ccb459a9c9370b8e74c5dbb419c Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 28 Feb 2012 13:37:23 +0200
Subject: [PATCH 6/6] This commit breaks toysh and xargs because OS X's libc
 do not support getline and getdelim.

---
 toys/toysh.c |    2 ++
 toys/xargs.c |    2 ++
 2 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/toys/toysh.c b/toys/toysh.c
index 23b88c3..ec46a91 100644
--- a/toys/toysh.c
+++ b/toys/toysh.c
@@ -372,7 +372,9 @@ void toysh_main(void)
 		for (;;) {
 			char *command = 0;
 			if (!f) xputc('$');
+#ifdef __linux__
 			if (1 > getline(&command, &cmdlen, f ? : stdin)) break;
+#endif
 			handle(command);
 			free(command);
 		}
diff --git a/toys/xargs.c b/toys/xargs.c
index 830fdaf..b4a41cd 100644
--- a/toys/xargs.c
+++ b/toys/xargs.c
@@ -126,7 +126,9 @@ void xargs_main(void)
 			// Read line
 			if (!data) {
 				ssize_t l = 0;
+#ifdef __linux__
 				l = getdelim(&data, (size_t *)&l, TT.delim, stdin);
+#endif
 
 				if (l<0) {
 					data = 0;
-- 
1.7.5.1



More information about the Toybox mailing list