[Toybox] Status update

Georgi Chorbadzhiyski gf at unixsol.org
Thu Mar 15 05:39:31 PDT 2012


I merged added this new commits to my dev branch at https://github.com/gfto/toybox

commit 02a58feb4529d564e10b9b790bfc604aaf1820dd
    lib/lib.h: Add dprintf() for OSX and Android.

commit 9476083e573ae3c2a132b3020ae78429ac0dcbc7
    Ignore LINUX, ANDROID and OSX config symbols.

commit 122cb23062800b62f408f4545530115e6a0d6552
    Allow SED to be overwritten in configure.

commit 2d9efb3d80e93052c93c4278e1444c202316565b
    scripts/genconfig: Define LINUX, ANDROID and OSX symbols in Config.probed
    This would allow disabling of toys using kconfig dependencies.

along with:

commit 98730b291429979aed6c87de0916231ac79579f6
    Add compat swapon(), swapoff() and sethostname() for Android.

commit c4911629398eaf3fe202188934ffc93f61ecd3ae
    Add getdelim() and getline() portability functions.

allowed me to rebase OS X and android "ports" which shrunk considerably and
are looking almost manageable.

gf at gf:~/git/toybox$ git diff --stat dev..osx
 configure          |   10 +++++++++-
 lib/getmountlist.c |   10 ++++++++++
 lib/portability.h  |    2 ++
 scripts/make.sh    |    2 +-
 toys.h             |   24 ++++++++++++++++++++----
 toys/dmesg.c       |    1 +
 toys/free.c        |    1 +
 toys/insmod.c      |    1 +
 toys/mdev.c        |    1 +
 toys/rmmod.c       |    1 +
 toys/swapoff.c     |    1 +
 toys/swapon.c      |    1 +
 toys/uptime.c      |    1 +
 13 files changed, 50 insertions(+), 6 deletions(-)

gf at gf:~/git/toybox$ git diff --stat dev..android
 configure          |    8 ++++++++
 lib/getmountlist.c |   32 ++++++++++++++++++++++++++++++++
 lib/lib.c          |    5 ++++-
 lib/portability.h  |    4 ++++
 scripts/make.sh    |    2 +-
 toys.h             |   15 ++++++++++++---
 toys/netcat.c      |    2 ++
 toys/who.c         |    1 +
 8 files changed, 64 insertions(+), 5 deletions(-)

-- 
Georgi Chorbadzhiyski
http://georgi.unixsol.org/
-------------- next part --------------
diff --git a/configure b/configure
index f31f761..291b4b2 100644
--- a/configure
+++ b/configure
@@ -7,8 +7,16 @@
 
 [ -z "$CFLAGS" ] && CFLAGS="-Wall -Wundef -Wno-char-subscripts"
 CFLAGS="$CFLAGS -funsigned-char"
-[ -z "$OPTIMIZE" ] && OPTIMIZE="-Os -ffunction-sections -fdata-sections -Wl,--gc-sections"
 [ -z "$CC" ] && CC=cc
 [ -z "$STRIP" ] && STRIP=strip
+if [ "$(uname -s)" = "Linux" ]
+then
+	[ -z "$LDFLAGS" ] && LDFLAGS="-Wl,--as-needed,-lutil,--no-as-needed -Wl,--gc-sections"
+fi
+if [ "$(uname -s)" = "Darwin" ]
+then
+	[ -z "$SED" ] && SED=gsed
+fi
+[ -z "$OPTIMIZE" ] && OPTIMIZE="-Os -ffunction-sections -fdata-sections"
 [ -z "$HOSTCC" ] && HOSTCC=gcc
 [ -z "$SED" ] && SED=sed
diff --git a/lib/getmountlist.c b/lib/getmountlist.c
index 1b23544..cc83b02 100644
--- a/lib/getmountlist.c
+++ b/lib/getmountlist.c
@@ -6,6 +6,14 @@
 
 #include "toys.h"
 
+#ifdef __APPLE__
+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/lib/portability.h b/lib/portability.h
index b1b669b..1310629 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -9,8 +9,10 @@
 
 #define _FILE_OFFSET_BITS 64
 
+#ifndef __APPLE__
 #define _POSIX_C_SOURCE 200809L
 #define _XOPEN_SOURCE 600
+#endif
 #define _BSD_SOURCE
 #define _SVID_SOURCE
 
diff --git a/scripts/make.sh b/scripts/make.sh
index 6597c35..2df9a4c 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -103,7 +103,7 @@ do_loudly()
 }
 
 do_loudly ${CROSS_COMPILE}${CC} $CFLAGS -I . -o toybox_unstripped $OPTIMIZE \
-  main.c lib/*.c $TOYFILES -Wl,--as-needed,-lutil,--no-as-needed || exit 1
+  main.c lib/*.c $TOYFILES $LDFLAGS || exit 1
 do_loudly ${CROSS_COMPILE}${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.
diff --git a/toys.h b/toys.h
index aa4ccfa..6be5f27 100644
--- a/toys.h
+++ b/toys.h
@@ -17,7 +17,6 @@
 #include <limits.h>
 #include <libgen.h>
 #include <math.h>
-#include <pty.h>
 #include <pwd.h>
 #include <sched.h>
 #include <setjmp.h>
@@ -29,12 +28,9 @@
 #include <strings.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
-#include <sys/mount.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/statvfs.h>
-#include <sys/sysinfo.h>
-#include <sys/swap.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
 #include <sys/wait.h>
@@ -42,6 +38,26 @@
 #include <utime.h>
 #include <utmpx.h>
 
+#ifndef __APPLE__
+#include <pty.h>
+#include <sys/swap.h>
+#include <sys/sysinfo.h>
+#include <sys/mount.h>
+#endif
+
+#ifdef __APPLE__
+#include <libgen.h> // for basename
+#include <signal.h> // for kill
+#include <util.h> // for forkpty
+#include <sys/disk.h>
+#define BLKGETSIZE DKIOCGETBLOCKSIZE
+#define RB_POWER_OFF RB_AUTOBOOT
+
+static inline unsigned short bswap_16(unsigned short x) {
+	return (x>>8) | (x<<8);
+}
+#endif
+
 #undef _XOPEN_SOURCE
 #define _XOPEN_SOURCE 600
 #include <time.h>
diff --git a/toys/dmesg.c b/toys/dmesg.c
index 95b023d..53a25cb 100644
--- a/toys/dmesg.c
+++ b/toys/dmesg.c
@@ -10,6 +10,7 @@ USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN))
 
 config DMESG
 	bool "dmesg"
+	depends on !OSX
 	default y
 	help
 	  usage: dmesg [-n level] [-s bufsize] | -c
diff --git a/toys/free.c b/toys/free.c
index 99dca86..d789ab9 100644
--- a/toys/free.c
+++ b/toys/free.c
@@ -10,6 +10,7 @@ USE_FREE(NEWTOY(free, "gmkb", TOYFLAG_USR|TOYFLAG_BIN))
 
 config FREE
 	bool "free"
+	depends on LINUX
 	default y
 	help
 	  usage: free [-bkmg]
diff --git a/toys/insmod.c b/toys/insmod.c
index e794828..fc8ac0a 100644
--- a/toys/insmod.c
+++ b/toys/insmod.c
@@ -10,6 +10,7 @@ USE_INSMOD(NEWTOY(insmod, "<1", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
 
 config INSMOD
 	bool "insmod"
+	depends on LINUX
 	default y
 	help
 	  usage: insmod MODULE [MODULE_OPTIONS]
diff --git a/toys/mdev.c b/toys/mdev.c
index 846d34e..0241b48 100644
--- a/toys/mdev.c
+++ b/toys/mdev.c
@@ -11,6 +11,7 @@ USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
 
 config MDEV
 	bool "mdev"
+	depends on LINUX
 	default n
 	help
 	  usage: mdev [-s]
diff --git a/toys/rmmod.c b/toys/rmmod.c
index d730b45..f6366ed 100644
--- a/toys/rmmod.c
+++ b/toys/rmmod.c
@@ -10,6 +10,7 @@ USE_RMMOD(NEWTOY(rmmod, "<1wf", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
 
 config RMMOD
 	bool "rmmod"
+	depends on LINUX
 	default y
 	help
 	  usage: rmmod [-wf] [MODULE]
diff --git a/toys/swapoff.c b/toys/swapoff.c
index fba0de8..33905c9 100644
--- a/toys/swapoff.c
+++ b/toys/swapoff.c
@@ -10,6 +10,7 @@ USE_SWAPOFF(NEWTOY(swapoff, "<1>1", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
 
 config SWAPOFF
 	bool "swapoff"
+	depends on !OSX
 	default y
 	help
 	  usage: swapoff swapregion
diff --git a/toys/swapon.c b/toys/swapon.c
index 16ce8d1..4b750b2 100644
--- a/toys/swapon.c
+++ b/toys/swapon.c
@@ -10,6 +10,7 @@ USE_SWAPON(NEWTOY(swapon, "<1>1p#<0>32767", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
 
 config SWAPON
 	bool "swapon"
+	depends on !OSX
 	default y
 	help
 	  usage: swapon [-p priority] filename
diff --git a/toys/uptime.c b/toys/uptime.c
index 83fb6b1..744a9bb 100644
--- a/toys/uptime.c
+++ b/toys/uptime.c
@@ -10,6 +10,7 @@ USE_UPTIME(NEWTOY(uptime, NULL, TOYFLAG_USR|TOYFLAG_BIN))
 
 config UPTIME
 	bool "uptime"
+	depends on !OSX
 	default y
 	help
 	  usage: uptime
-------------- next part --------------
diff --git a/configure b/configure
index f31f761..d8a4327 100644
--- a/configure
+++ b/configure
@@ -12,3 +12,11 @@ CFLAGS="$CFLAGS -funsigned-char"
 [ -z "$STRIP" ] && STRIP=strip
 [ -z "$HOSTCC" ] && HOSTCC=gcc
 [ -z "$SED" ] && SED=sed
+
+export NDK_ROOT="/home/gf/android-ndk-r7b"
+export CC=gcc
+export CROSS_COMPILE="$NDK_ROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-"
+export BASE="$NDK_ROOT/platforms/android-9/arch-arm"
+cp -v $BASE/usr/lib/crtbegin_dynamic.o .
+cp -v $BASE/usr/lib/crtend_android.o .
+export CFLAGS="-Wl,-rpath-link=$BASE/usr/lib/,-dynamic-linker=/system/bin/linker -L$BASE/usr/lib/ -I$BASE/usr/include -lc"
diff --git a/lib/getmountlist.c b/lib/getmountlist.c
index 1b23544..2cfe23c 100644
--- a/lib/getmountlist.c
+++ b/lib/getmountlist.c
@@ -14,6 +14,36 @@ char *path_mounts = "/proc/mounts";
 // statvfs() information.  This returns a reversed list, which is good for
 // finding overmounts and such.
 
+#ifdef __ANDROID__
+struct mtab_list *getmountlist(int die)
+{
+	FILE *fp;
+	struct mtab_list *mtlist, *mt;
+	struct mntent *me;
+	char evilbuf[2*PATH_MAX];
+
+	mtlist = 0;
+	fp = fopen(path_mounts, "r");
+	while ( (me = getmntent(fp)) ) {
+		mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
+			strlen(me->mnt_dir) + strlen(me->mnt_type) + 3);
+		mt->next = mtlist;
+		// Get information about this filesystem.  Yes, we need both.
+		stat(me->mnt_dir, &(mt->stat));
+		statvfs(me->mnt_dir, &(mt->statvfs));
+		// Remember information from /proc/mounts
+		strcpy(mt->type, me->mnt_type);
+		strcpy(mt->dir, me->mnt_dir);
+		mt->dir = mt->type + strlen(mt->type) + 1;
+		mt->device = mt->dir + strlen(mt->dir) + 1;
+		strcpy(mt->device, me->mnt_fsname);
+		mtlist = mt;
+	}
+
+	return mtlist;
+}
+#else
+
 struct mtab_list *getmountlist(int die)
 {
 	FILE *fp;
@@ -41,3 +71,5 @@ struct mtab_list *getmountlist(int die)
 	}
 	return mtlist;
 }
+
+#endif
diff --git a/lib/lib.c b/lib/lib.c
index fb379e2..4dd3e86 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -768,15 +768,18 @@ void crc_init(unsigned int *crc_table, int little_endian)
 
 void terminal_size(unsigned *x, unsigned *y)
 {
-	struct winsize ws;
 	int i;
 
 	//memset(&ws, 0, sizeof(ws));
+#ifndef __ANDROID__
+	struct winsize ws;
 	for (i=0; i<3; i++) {
 		if (ioctl(i, TIOCGWINSZ, &ws)) continue;
 		if (x) *x = ws.ws_col;
 		if (y) *y = ws.ws_row;
 	}
+#endif
+
 	if (x) {
 		char *s = getenv("COLUMNS");
 
diff --git a/lib/portability.h b/lib/portability.h
index b1b669b..3f629dc 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -17,6 +17,10 @@
 #include <stdio.h>
 #define fdprintf(...) dprintf(__VA_ARGS__)
 
+#ifdef __ANDROID__
+#define statvfs statfs
+#endif
+
 #ifdef __GNUC__
 #define noreturn	__attribute__((noreturn))
 #else
diff --git a/scripts/make.sh b/scripts/make.sh
index 6597c35..49872ba 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -103,7 +103,7 @@ do_loudly()
 }
 
 do_loudly ${CROSS_COMPILE}${CC} $CFLAGS -I . -o toybox_unstripped $OPTIMIZE \
-  main.c lib/*.c $TOYFILES -Wl,--as-needed,-lutil,--no-as-needed || exit 1
+  main.c lib/*.c $TOYFILES -Wl,--as-needed,--no-as-needed || exit 1
 do_loudly ${CROSS_COMPILE}${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.
diff --git a/toys.h b/toys.h
index aa4ccfa..5ff9d5d 100644
--- a/toys.h
+++ b/toys.h
@@ -17,7 +17,6 @@
 #include <limits.h>
 #include <libgen.h>
 #include <math.h>
-#include <pty.h>
 #include <pwd.h>
 #include <sched.h>
 #include <setjmp.h>
@@ -32,15 +31,25 @@
 #include <sys/mount.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
-#include <sys/statvfs.h>
 #include <sys/sysinfo.h>
-#include <sys/swap.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <utime.h>
+
+#if defined(__linux__) && !defined(__ANDROID__)
+#include <pty.h>
+#include <sys/statvfs.h>
+#include <sys/swap.h>
 #include <utmpx.h>
+#endif
+
+#ifdef __ANDROID__
+#include <sys/statfs.h>
+#define SWAP_FLAG_PREFER 0x8000
+#define SWAP_FLAG_PRIO_SHIFT 0
+#endif
 
 #undef _XOPEN_SOURCE
 #define _XOPEN_SOURCE 600
diff --git a/toys/netcat.c b/toys/netcat.c
index 3da1912..c1f6c48 100644
--- a/toys/netcat.c
+++ b/toys/netcat.c
@@ -167,8 +167,10 @@ void netcat_main(void)
 
 				// Do we need a tty?
 
+#ifndef __ANDROID__
 				if (toys.optflags&FLAG_t)
 					child = forkpty(&(pollfds[1].fd), NULL, NULL, NULL);
+#endif
 
 				// Do we need to fork and/or redirect for exec?
 
diff --git a/toys/who.c b/toys/who.c
index d407c6b..ff2da9a 100644
--- a/toys/who.c
+++ b/toys/who.c
@@ -12,6 +12,7 @@ USE_WHO(NEWTOY(who, NULL, TOYFLAG_BIN))
 
 config WHO
 	bool "who"
+	depends on !ANDROID
 	default n
 	help
 	  usage: who


More information about the Toybox mailing list