am 3fd82b8: adb: osx: Match devices based on interface subclass and prot

Merge commit '3fd82b8861aa410fab7785074941b459d92220c1'

* commit '3fd82b8861aa410fab7785074941b459d92220c1':
  adb: osx: Match devices based on interface subclass and protocol
diff --git a/adb/commandline.c b/adb/commandline.c
index 7410dce..41b340a 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -96,7 +96,8 @@
         " -e                            - directs command to the only running emulator.\n"
         "                                 returns an error if more than one emulator is running.\n"
         " -s <serial number>            - directs command to the USB device or emulator with\n"
-        "                                 the given serial number\n"
+        "                                 the given serial number. Overrides ANDROID_SERIAL\n"
+        "                                 envivornment variable.\n"
         " -p <product name or path>     - simple product name like 'sooner', or\n"
         "                                 a relative/absolute path to a product\n"
         "                                 out directory like 'out/target/product/sooner'.\n"
@@ -766,6 +767,8 @@
     }
     // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
 
+    serial = getenv("ANDROID_SERIAL");
+
         /* modifiers and flags */
     while(argc > 0) {
         if(!strcmp(argv[0],"nodaemon")) {
diff --git a/include/arch/freebsd-x86/AndroidConfig.h b/include/arch/freebsd-x86/AndroidConfig.h
new file mode 100644
index 0000000..cc118f4
--- /dev/null
+++ b/include/arch/freebsd-x86/AndroidConfig.h
@@ -0,0 +1,317 @@
+/*
+ * Copyright (C) 2005 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Android config -- "FreeBSD".  Used for desktop x86 FreeBSD.
+ */
+#ifndef _ANDROID_CONFIG_H
+#define _ANDROID_CONFIG_H
+
+/*
+ * make sure we are building for FreeBSD
+ */
+#ifndef OS_FREEBSD
+#define OS_FREEBSD
+#endif
+/*
+ * ===========================================================================
+ *                              !!! IMPORTANT !!!
+ * ===========================================================================
+ *
+ * This file is included by ALL C/C++ source files.  Don't put anything in
+ * here unless you are absolutely certain it can't go anywhere else.
+ *
+ * Any C++ stuff must be wrapped with "#ifdef __cplusplus".  Do not use "//"
+ * comments.
+ */
+
+/*
+ * Threading model.  Choose one:
+ *
+ * HAVE_PTHREADS - use the pthreads library.
+ * HAVE_WIN32_THREADS - use Win32 thread primitives.
+ *  -- combine HAVE_CREATETHREAD, HAVE_CREATEMUTEX, and HAVE__BEGINTHREADEX
+ */
+#define HAVE_PTHREADS
+
+/*
+ * Do we have the futex syscall?
+ */
+/* #define HAVE_FUTEX */
+
+/*
+ * Process creation model.  Choose one:
+ *
+ * HAVE_FORKEXEC - use fork() and exec()
+ * HAVE_WIN32_PROC - use CreateProcess()
+ */
+#define HAVE_FORKEXEC
+
+/*
+ * Process out-of-memory adjustment.  Set if running on Linux,
+ * where we can write to /proc/<pid>/oom_adj to modify the out-of-memory
+ * badness adjustment.
+ */
+/* #define HAVE_OOM_ADJ */
+
+/*
+ * IPC model.  Choose one:
+ *
+ * HAVE_SYSV_IPC - use the classic SysV IPC mechanisms (semget, shmget).
+ * HAVE_MACOSX_IPC - use Macintosh IPC mechanisms (sem_open, mmap).
+ * HAVE_WIN32_IPC - use Win32 IPC (CreateSemaphore, CreateFileMapping).
+ * HAVE_ANDROID_IPC - use Android versions (?, mmap).
+ */
+#define HAVE_SYSV_IPC
+
+/*
+ * Memory-mapping model. Choose one:
+ *
+ * HAVE_POSIX_FILEMAP - use the Posix sys/mmap.h
+ * HAVE_WIN32_FILEMAP - use Win32 filemaps
+ */
+#define  HAVE_POSIX_FILEMAP
+
+/*
+ * Define this if you have <termio.h>
+ */
+/* #define  HAVE_TERMIO_H */
+
+/*
+ * Define this if you build against MSVCRT.DLL
+ */
+/* #define HAVE_MS_C_RUNTIME */
+
+/*
+ * Define this if you have sys/uio.h
+ */
+#define  HAVE_SYS_UIO_H
+
+/*
+ * Define this if your platforms implements symbolic links
+ * in its filesystems
+ */
+#define HAVE_SYMLINKS
+
+/*
+ * Define this if we have localtime_r().
+ */
+#define HAVE_LOCALTIME_R
+
+/*
+ * Define this if we have gethostbyname_r().
+ */
+/* #define HAVE_GETHOSTBYNAME_R */
+
+/*
+ * Define this if we have ioctl().
+ */
+#define HAVE_IOCTL
+
+/*
+ * Define this if we want to use WinSock.
+ */
+/* #define HAVE_WINSOCK */
+
+/*
+ * Define this if have clock_gettime() and friends
+ *
+ * Desktop Linux has this in librt, but it's broken in goobuntu, yielding
+ * mildly or wildly inaccurate results.
+ */
+#define HAVE_POSIX_CLOCKS
+
+/*
+ * Define this if we have pthread_cond_timedwait_monotonic() and
+ * clock_gettime(CLOCK_MONOTONIC).
+ */
+/* #define HAVE_TIMEDWAIT_MONOTONIC */
+
+/*
+ * Define this if we have linux style epoll()
+ */
+/* #define HAVE_EPOLL */
+
+/*
+ * Endianness of the target machine.  Choose one:
+ *
+ * HAVE_ENDIAN_H -- have endian.h header we can include.
+ * HAVE_LITTLE_ENDIAN -- we are little endian.
+ * HAVE_BIG_ENDIAN -- we are big endian.
+ */
+/* #define HAVE_ENDIAN_H */
+#define HAVE_LITTLE_ENDIAN
+
+/*
+ * Define this if you have sys/endian.h
+ * NOTE: mutually exclusive with HAVE_ENDIAN_H
+ */
+#define HAVE_SYS_ENDIAN_H
+
+/*
+ * We need to choose between 32-bit and 64-bit off_t.  All of our code should
+ * agree on the same size.  For desktop systems, use 64-bit values,
+ * because some of our libraries (e.g. wxWidgets) expect to be built that way.
+ */
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE_SOURCE 1
+
+/*
+ * Defined if we have the backtrace() call for retrieving a stack trace.
+ * Needed for CallStack to operate; if not defined, CallStack is
+ * non-functional.
+ */
+#define HAVE_BACKTRACE 0
+
+/*
+ * Defined if we have the dladdr() call for retrieving the symbol associated
+ * with a memory address.  If not defined, stack crawls will not have symbolic
+ * information.
+ */
+#define HAVE_DLADDR 1
+
+/*
+ * Defined if we have the cxxabi.h header for demangling C++ symbols.  If
+ * not defined, stack crawls will be displayed with raw mangled symbols
+ */
+#define HAVE_CXXABI 0
+
+/*
+ * Defined if we have the gettid() system call.
+ */
+/* #define HAVE_GETTID */
+
+/* 
+ * Defined if we have the sched_setscheduler() call
+ */
+#define HAVE_SCHED_SETSCHEDULER
+
+/*
+ * Add any extra platform-specific defines here.
+ */
+
+/*
+ * Define if we have <malloc.h> header
+ */
+#define HAVE_MALLOC_H
+
+/*
+ * Define if we have Linux-style non-filesystem Unix Domain Sockets
+ */
+
+/*
+ * What CPU architecture does this platform use?
+ */
+#define ARCH_X86
+
+
+/*
+ * Define if we have Linux's inotify in <sys/inotify.h>.
+ */
+/*#define HAVE_INOTIFY 1*/
+
+/*
+ * Define if we have madvise() in <sys/mman.h>
+ */
+#define HAVE_MADVISE 1
+
+/*
+ * Define if tm struct has tm_gmtoff field
+ */
+#define HAVE_TM_GMTOFF 1
+
+/*
+ * Define if dirent struct has d_type field
+ */
+#define HAVE_DIRENT_D_TYPE 1
+
+/*
+ * Define if libc includes Android system properties implementation.
+ */
+/* #define HAVE_LIBC_SYSTEM_PROPERTIES */
+
+/*
+ * Define if system provides a system property server (should be
+ * mutually exclusive with HAVE_LIBC_SYSTEM_PROPERTIES).
+ */
+#define HAVE_SYSTEM_PROPERTY_SERVER
+
+/*
+ * sprintf() format string for shared library naming.
+ */
+#define OS_SHARED_LIB_FORMAT_STR    "lib%s.so"
+
+/*
+ * type for the third argument to mincore().
+ */
+#define MINCORE_POINTER_TYPE char *
+
+/*
+ * Do we have the sigaction flag SA_NOCLDWAIT?
+ */
+#define HAVE_SA_NOCLDWAIT
+
+/*
+ * Define if we include <sys/mount.h> for statfs()
+ */
+#define INCLUDE_SYS_MOUNT_FOR_STATFS 1
+  
+/*
+ * The default path separator for the platform
+ */
+#define OS_PATH_SEPARATOR '/'
+
+/*
+ * Is the filesystem case sensitive?
+ */
+#define OS_CASE_SENSITIVE
+
+/*
+ * Define if <sys/socket.h> exists.
+ */
+#define HAVE_SYS_SOCKET_H 1
+
+/*
+ * Define if the strlcpy() function exists on the system.
+ */
+#define HAVE_STRLCPY 1
+
+/*
+ * Define if prctl() exists
+ */
+/* #define HAVE_PRCTL 1 */
+
+/*
+ * Define if writev() exists
+ */
+#define HAVE_WRITEV 1
+
+/*
+ * Define if <alloca.h> does not exist
+ * NOTE: <alloca.h> defines alloca() which
+ *   on FreeBSD is defined in <stdlib.h>
+ */
+#define HAVE_NO_ALLOCA_H
+
+/*
+ * Defines CLOCK_PROCESS_CPUTIME_ID for clock_gettime()
+ * XXX: CLOCK_PROF seems to be commonly used replacement
+ */
+#ifndef  CLOCK_PROCESS_CPUTIME_ID
+#define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF
+#endif
+
+#endif /*_ANDROID_CONFIG_H*/
diff --git a/include/cutils/tztime.h b/include/cutils/tztime.h
index 9b3ece8..4af2ce4 100644
--- a/include/cutils/tztime.h
+++ b/include/cutils/tztime.h
@@ -17,6 +17,8 @@
 #ifndef _CUTILS_TZTIME_H
 #define _CUTILS_TZTIME_H
 
+#include <time.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -24,6 +26,9 @@
 time_t mktime_tz(struct tm * const tmp, char const * tz);
 void localtime_tz(const time_t * const timep, struct tm * tmp, const char* tz);
 
+#ifndef HAVE_ANDROID_OS
+/* the following is defined in <time.h> in Bionic */
+
 struct strftime_locale {
     const char *mon[12];    /* short names */
     const char *month[12];  /* long names */
@@ -39,6 +44,8 @@
 
 size_t strftime_tz(char *s, size_t max, const char *format, const struct tm *tm, const struct strftime_locale *locale);
 
+#endif /* !HAVE_ANDROID_OS */
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/init/builtins.c b/init/builtins.c
index 95fb223..bcdfee1 100644
--- a/init/builtins.c
+++ b/init/builtins.c
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <sys/mount.h>
 #include <sys/resource.h>
+#include <linux/loop.h>
 
 #include "init.h"
 #include "keywords.h"
@@ -257,7 +258,7 @@
 int do_mount(int nargs, char **args)
 {
     char tmp[64];
-    char *source;
+    char *source, *target, *system;
     char *options = NULL;
     unsigned flags = 0;
     int n, i;
@@ -275,15 +276,70 @@
             options = args[n];
     }
 
+    system = args[1];
     source = args[2];
+    target = args[3];
+
     if (!strncmp(source, "mtd@", 4)) {
         n = mtd_name_to_number(source + 4);
-        if (n >= 0) {
-            sprintf(tmp, "/dev/block/mtdblock%d", n);
-            source = tmp;
+        if (n < 0) {
+            return -1;
         }
+
+        sprintf(tmp, "/dev/block/mtdblock%d", n);
+
+        if (mount(tmp, target, system, flags, options) < 0) {
+            return -1;
+        }
+
+        return 0;
+    } else if (!strncmp(source, "loop@", 5)) {
+        int mode, loop, fd;
+        struct loop_info info;
+
+        mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
+        fd = open(source + 5, mode);
+        if (fd < 0) {
+            return -1;
+        }
+
+        for (n = 0; ; n++) {
+            sprintf(tmp, "/dev/block/loop%d", n);
+            loop = open(tmp, mode);
+            if (loop < 0) {
+                return -1;
+            }
+
+            /* if it is a blank loop device */
+            if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
+                /* if it becomes our loop device */
+                if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
+                    close(fd);
+
+                    if (mount(tmp, target, system, flags, options) < 0) {
+                        ioctl(loop, LOOP_CLR_FD, 0);
+                        close(loop);
+                        return -1;
+                    }
+
+                    close(loop);
+                    return 0;
+                }
+            }
+
+            close(loop);
+        }
+
+        close(fd);
+        ERROR("out of loopback devices");
+        return -1;
+    } else {
+        if (mount(source, target, system, flags, options) < 0) {
+            return -1;
+        }
+
+        return 0;
     }
-    return mount(source, args[3], args[1], flags, options);
 }
 
 int do_setkey(int nargs, char **args)
diff --git a/init/devices.c b/init/devices.c
index b914c53..8aea772 100644
--- a/init/devices.c
+++ b/init/devices.c
@@ -131,6 +131,9 @@
     { "/dev/qmi0",          0640,   AID_RADIO,      AID_RADIO,      0 },
     { "/dev/qmi1",          0640,   AID_RADIO,      AID_RADIO,      0 },
     { "/dev/qmi2",          0640,   AID_RADIO,      AID_RADIO,      0 },
+
+        /* CDMA radio interface MUX */
+    { "/dev/ts0710mux",     0640,   AID_RADIO,      AID_RADIO,      1 },
     { NULL, 0, 0, 0, 0 },
 };
 
@@ -387,6 +390,9 @@
         } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
             base = "/dev/mtd/";
             mkdir(base, 0755);
+        } else if(!strncmp(uevent->subsystem, "sound", 5)) {
+            base = "/dev/snd/";
+            mkdir(base, 0755);
         } else if(!strncmp(uevent->subsystem, "misc", 4) &&
                     !strncmp(name, "log_", 4)) {
             base = "/dev/log/";
diff --git a/init/init.c b/init/init.c
index a748ec3..283608c 100644
--- a/init/init.c
+++ b/init/init.c
@@ -248,7 +248,9 @@
             setuid(svc->uid);
         }
 
-        execve(svc->args[0], (char**) svc->args, (char**) ENV);
+        if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
+            ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
+        }
         _exit(127);
     }
 
diff --git a/libacc/acc.c b/libacc/acc.c
new file mode 100644
index 0000000..a8505cd
--- /dev/null
+++ b/libacc/acc.c
@@ -0,0 +1,525 @@
+/*
+ **
+ ** Copyright 2009, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License");
+ ** you may not use this file except in compliance with the License.
+ ** You may obtain a copy of the License at
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0
+ **
+ ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS,
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ** See the License for the specific language governing permissions and
+ ** limitations under the License.
+ */
+
+/* Based upon the freeware version of the Obfuscated Tiny C Compiler
+ * by Francis Bellard. <francis@bellard.org>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define TOKEN_OPERATOR 1
+#define TOKEN_NUMBER 2
+
+#define TOKEN_SYMBOL_BASE     256
+#define TOKEN_INT             256
+#define TOKEN_IF              288
+#define TOKEN_ELSE            312
+#define TOKEN_WHILE           352
+#define TOKEN_BREAK           400
+#define TOKEN_RETURN          448
+#define TOKEN_FOR             504
+#define TOKEN_DEFINE          536
+
+static int currentToken;
+static int currentTokenData;
+static int gCurrentTokenOperatorLevel;
+static int currentChar;
+static int gEndOfFunctionTarget;
+static int gProgramCounter;
+static int gFunctionStackSize;
+static int savedChar;
+static char* pInProgressMacro;
+static char* P;
+static char* ac;
+static char* gStringTable;
+static char* pSymbolTable;
+static char* M;
+static char* R;
+static FILE* pInput;
+
+static void parseDeclarations (int isLocal);
+static void parseExpression();
+
+static void addToSymbolTable(char e) {
+    *pSymbolTable++ = e;
+}
+
+static void nextChar() {
+    if (pInProgressMacro) {
+        currentChar = *(char*) pInProgressMacro++;
+        if (currentChar == 2) {
+            pInProgressMacro = NULL;
+            currentChar = savedChar;
+        }
+    } else
+        currentChar = fgetc(pInput);
+}
+
+static int isSymbolChar() {
+    return isalnum(currentChar) || currentChar == '_';
+}
+
+static void unescapeCurrentChar() {
+    if (currentChar == '\\') {
+        nextChar();
+        if (currentChar == 'n')
+            currentChar = '\n';
+    }
+}
+
+static void nextToken() {
+    int j, m;
+    while (isspace(currentChar) || currentChar == '#') {
+        if (currentChar == '#') {
+            nextChar();
+            nextToken();
+            if (currentToken == TOKEN_DEFINE) {
+                nextToken();
+                addToSymbolTable(' ');
+                *(int*) currentToken = 1;
+                *(int*) (currentToken + 4) = (int) pSymbolTable;
+            }
+            while (currentChar != '\n') {
+                addToSymbolTable(currentChar);
+                nextChar();
+            }
+            addToSymbolTable(currentChar);
+            addToSymbolTable(2);
+        }
+        nextChar();
+    }
+    gCurrentTokenOperatorLevel = 0;
+    currentToken = currentChar;
+    if (isSymbolChar()) {
+        addToSymbolTable(' ');
+        M = pSymbolTable;
+        while (isSymbolChar()) {
+            addToSymbolTable(currentChar);
+            nextChar();
+        }
+        if (isdigit(currentToken)) {
+            currentTokenData = strtol(M, 0, 0);
+            currentToken = TOKEN_NUMBER;
+        } else {
+            *(char*) pSymbolTable = ' ';
+            currentToken = strstr(R, M - 1) - R;
+            *(char*) pSymbolTable = 0;
+            currentToken = currentToken * 8 + TOKEN_SYMBOL_BASE;
+            if (currentToken > TOKEN_DEFINE) {
+                currentToken = ((int) P) + currentToken;
+                if (*(int*) currentToken == 1) {
+                    pInProgressMacro = (char*) (*(int*) (currentToken + 4));
+                    savedChar = currentChar;
+                    nextChar();
+                    nextToken();
+                }
+            }
+        }
+    } else {
+        nextChar();
+        if (currentToken == '\'') {
+            currentToken = TOKEN_NUMBER;
+            unescapeCurrentChar();
+            currentTokenData = currentChar;
+            nextChar();
+            nextChar();
+        } else if (currentToken == '/' & currentChar == '*') {
+            nextChar();
+            while (currentChar) {
+                while (currentChar != '*')
+                    nextChar();
+                nextChar();
+                if (currentChar == '/')
+                    currentChar = 0;
+            }
+            nextChar();
+            nextToken();
+        } else {
+            char* e = "++#m--%am*@R<^1c/@%[_[H3c%@%[_[H3c+@.B#d-@%:_^BKd<<Z/03e>>`/03e<=0f>=/f<@.f>@1f==&g!='g&&k||#l&@.BCh^@.BSi|@.B+j~@/%Yd!@&d*@b";
+            while (j = *(char*) e++) {
+                m = *(char*) e++;
+                currentTokenData = 0;
+                while ((gCurrentTokenOperatorLevel = *(char*) e++ - 98) < 0)
+                    currentTokenData = currentTokenData * 64 + gCurrentTokenOperatorLevel + 64;
+                if (j == currentToken && (m == currentChar || m == 64)) {
+                    if (m == currentChar) {
+                        nextChar();
+                        currentToken = TOKEN_OPERATOR;
+                    }
+                    break;
+                }
+            }
+        }
+    }
+}
+
+/*
+ * Emit 1 to 4 bytes of code. Little-endian, doesn't emit high bytes that
+ * are 0x00 or 0xff
+ */
+static void emitCode(int g) {
+    while( g && g != -1) {
+        *(char*) gProgramCounter++=g;
+        g=g>>8;
+    }
+}
+
+static void fixupAddress(e) {
+    int g;
+    while( e) {
+        g=*(int*) e;
+        *(int*) e=gProgramCounter-e-4;
+        e=g;
+    }
+}
+
+static int emitCodeWithImmediate( g, e) {
+    emitCode(g);
+    *(int*) gProgramCounter = e;
+    e = gProgramCounter;
+    gProgramCounter = gProgramCounter + 4;
+    return e;
+}
+
+static int emitLoadAccumulatorImmediate(e) {
+    emitCodeWithImmediate(0xb8,e); /* Move immediate a, e */
+}
+
+static int emitBranch(e) {
+    return emitCodeWithImmediate(0xe9,e); /* Jump relative */
+}
+
+static int emitTest( j, e) {
+    emitCode(0x0FC085); /* 85 C0 FC TEST */
+    return emitCodeWithImmediate(0x84 + j, e); /* TEST */
+}
+
+static void emitSetCC(int condition) {
+    emitCode( 0xC139); /* 39 C1 CMP */
+    emitLoadAccumulatorImmediate(0);
+    emitCode( 0x0F); /* Two byte opcode prefix */
+    emitCode( condition+0x90); /* Set byte on condition (controlled by e) */
+    emitCode( 0xC0); /* I think this is part of the SETcc instruction */
+}
+
+static void emitNumericOp( int op, int e) {
+    emitCode(op + 0x83);
+    emitCodeWithImmediate((e < 512) << 7 | 5, e);
+}
+
+static void parseTerminal (int level) {
+    int g,e,m,aa;
+    g=1;
+    if( currentToken == '"') {
+        emitLoadAccumulatorImmediate(gStringTable);
+        while( currentChar != '"') {
+            unescapeCurrentChar ();
+            *(char*) gStringTable++=currentChar;
+            nextChar ();
+        }
+        *(char*) gStringTable=0;
+        gStringTable= (char*) (((int)gStringTable) +4&-4);
+        nextChar();
+        nextToken();
+    }
+    else {
+        aa=gCurrentTokenOperatorLevel;
+        m= currentTokenData;
+        e=currentToken;
+        nextToken();
+        if( e == TOKEN_NUMBER) {
+            emitLoadAccumulatorImmediate(m);
+        }
+        else if( aa == 2) {
+            parseTerminal(0);
+            emitCodeWithImmediate(0xB9,0); /* MOV r1, immediate */
+            if( e == '!')emitSetCC(m);
+            else emitCode( m);
+        }
+        else if( e == '(') {
+            parseExpression ();
+            nextToken();
+        }
+        else if( e == '*') {
+            nextToken();
+            e=currentToken;
+            nextToken();
+            nextToken();
+            if( currentToken == '*') {
+                nextToken();
+                nextToken();
+                nextToken();
+                nextToken();
+                e=0;
+            }
+            nextToken();
+            parseTerminal(0);
+            if( currentToken == '=') {
+                nextToken();
+                emitCode( 0x50); /* PUSH r0 */
+                parseExpression ();
+                emitCode( 0x59); /* POP r1 */
+                emitCode( 0x188 + (e == TOKEN_INT)); /* 88 01 MOV */
+            }
+            else if( e) {
+                if( e == TOKEN_INT)emitCode( 0x8B); /* MOV */
+                else emitCode( 0xBE0F); /* 0F BE MOVSX move with sign extension */
+                gProgramCounter++;
+            }
+        }
+        else if( e == '&') {
+            emitNumericOp(10,*(int*) currentToken); /* 8D LEA */
+            nextToken();
+        }
+        else {
+            g=*(int*) e;
+            if(!g)g=dlsym(0,M);
+            if( currentToken == '=' & level) {
+                nextToken();
+                parseExpression ();
+                emitNumericOp(6,g); /* 89 MOV */
+            }
+            else if( currentToken!= '(') {
+                emitNumericOp(8,g); /* 8B MOV sreg */
+                if( gCurrentTokenOperatorLevel == 11) {
+                    emitNumericOp(0,g); /* 83 ADD */
+                    emitCode( currentTokenData);
+                    nextToken();
+                }
+            }
+        }
+    }
+    if( currentToken == '(') {
+        if( g == 1)emitCode( 0x50); /* push */
+        m= emitCodeWithImmediate(0xEC81,0); /* 81 EC Cmp ?? */
+        nextToken();
+        level=0;
+        while( currentToken!= ')') {
+            parseExpression ();
+            emitCodeWithImmediate(0x248489,level); /* 89 84 24 MOV sp + level*/
+            if( currentToken == ',')nextToken();
+            level=level +4;
+        }
+        *(int*) m= level;
+        nextToken();
+        if(!g) {
+            e=e +4;
+            *(int*) e=emitCodeWithImmediate(0xE8,*(int*) e); /* Call */
+        }
+        else if( g == 1) {
+            emitCodeWithImmediate(0x2494FF,level); /* FF 94 24 */
+            level=level +4;
+        }
+        else {
+            emitCodeWithImmediate(0xE8,g-gProgramCounter-5); /* CALL */
+        }
+        if( level)emitCodeWithImmediate(0xC481,level); /* 81 C4 adjust stack pointer */
+    }
+}
+
+static void parseBinaryOp (int level) {
+    int e,g,m;
+    if( level--== 1)parseTerminal(1);
+    else {
+        parseBinaryOp (level);
+        m= 0;
+        while( level == gCurrentTokenOperatorLevel) {
+            g=currentToken;
+            e=currentTokenData;
+            nextToken();
+            if( level>8) {
+                m= emitTest(e,m);
+                parseBinaryOp (level);
+            }
+            else {
+                emitCode( 0x50);
+                parseBinaryOp (level);
+                emitCode( 0x59);
+                if( level == 4 | level == 5) {
+                    emitSetCC(e);
+                }
+                else {
+                    emitCode( e);
+                    if( g == '%')emitCode( 0x92); /* XCHG */
+                }
+            }
+        }
+        if( m&&level>8) {
+            m= emitTest(e,m);
+            emitLoadAccumulatorImmediate(e^1);
+            emitBranch(5); /* Jump relative +5 */
+            fixupAddress(m);
+            emitLoadAccumulatorImmediate(e);
+        }
+    }
+}
+
+static void parseExpression() {
+    parseBinaryOp(11);
+}
+
+static int parseExpressionEmitTest() {
+    parseExpression();
+    return emitTest(0, 0);
+}
+
+static void parseStatement (int* pBreakTarget) {
+    int m,g,e;
+    if( currentToken == TOKEN_IF) {
+        nextToken();
+        nextToken();
+        m= parseExpressionEmitTest ();
+        nextToken();
+        parseStatement (pBreakTarget);
+        if( currentToken == TOKEN_ELSE) {
+            nextToken();
+            g=emitBranch(0);
+            fixupAddress(m);
+            parseStatement (pBreakTarget);
+            fixupAddress(g);
+        }
+        else {
+            fixupAddress(m);
+        }
+    }
+    else if ( currentToken == TOKEN_WHILE || currentToken == TOKEN_FOR) {
+        e = currentToken;
+        nextToken();
+        nextToken();
+        if( e == TOKEN_WHILE) {
+            g=gProgramCounter;
+            m= parseExpressionEmitTest ();
+        }
+        else {
+            if( currentToken != ';')parseExpression ();
+            nextToken();
+            g=gProgramCounter;
+            m= 0;
+            if( currentToken != ';')m= parseExpressionEmitTest ();
+            nextToken();
+            if( currentToken!= ')') {
+                e=emitBranch(0);
+                parseExpression ();
+                emitBranch(g-gProgramCounter-5);
+                fixupAddress(e);
+                g=e +4;
+            }
+        }
+        nextToken();
+        parseStatement(&m);
+        emitBranch(g-gProgramCounter-5);
+        fixupAddress(m);
+    }
+    else if( currentToken == '{') {
+        nextToken();
+        parseDeclarations(1);
+        while( currentToken != '}') parseStatement(pBreakTarget);
+        nextToken();
+    }
+    else {
+        if( currentToken == TOKEN_RETURN) {
+            nextToken();
+            if( currentToken != ';') parseExpression();
+            gEndOfFunctionTarget=emitBranch(gEndOfFunctionTarget);
+        }
+        else if( currentToken == TOKEN_BREAK) {
+            nextToken();
+            *pBreakTarget = emitBranch(*pBreakTarget);
+        }
+        else if( currentToken != ';') parseExpression();
+        nextToken();
+    }
+}
+
+static void parseDeclarations (int isLocal) {
+    int m;
+    while( currentToken == TOKEN_INT | currentToken != -1 & !isLocal ) {
+        if( currentToken == TOKEN_INT) {
+            nextToken();
+            while( currentToken != ';') {
+                if( isLocal ) {
+                    gFunctionStackSize=gFunctionStackSize +4;
+                    *(int*) currentToken=-gFunctionStackSize;
+                }
+                else {
+                    *(char**) currentToken = gStringTable;
+                    gStringTable=gStringTable +4;
+                }
+                nextToken();
+                if( currentToken == ',')nextToken();
+            }
+            nextToken();
+        }
+        else {
+            fixupAddress(*(int*)(currentToken + 4));
+            *(int*) currentToken=gProgramCounter;
+            nextToken();
+            nextToken();
+            m= 8;
+            while( currentToken != ')') {
+                *(int*) currentToken=m;
+                m= m +4;
+                nextToken();
+                if( currentToken == ',')nextToken();
+            }
+            nextToken();
+            gEndOfFunctionTarget=gFunctionStackSize=0;
+            emitCode( 0xE58955); /* 55 89 E5 PUSH */
+            m= emitCodeWithImmediate(0xEC81,0); /* 81 EC */
+            parseStatement(0);
+            fixupAddress(gEndOfFunctionTarget);
+            emitCode( 0xC3C9); /* C9 C3 LEAVE */
+            *(int*) m= gFunctionStackSize;
+        }
+    }
+}
+
+int main( int argc, char** argv) {
+    pInput = stdin;
+    if (argc-- > 1) {
+        char* file = argv[1];
+        argv += 1;
+        pInput = fopen(file, "r");
+        if (pInput == NULL) {
+            fprintf(stderr, "Could not open file \"%s\"\n", file);
+            return -1;
+        }
+    }
+    pSymbolTable = strcpy(R = calloc(1, 99999),
+            " int if else while break return for define main ") + 48;
+    gStringTable = calloc(1, 99999);
+    ac = calloc(1, 99999);
+    gProgramCounter = (int) ac;
+    P = calloc(1, 99999);
+    nextChar();
+    nextToken();
+    parseDeclarations(0);
+#if 1
+    fwrite(R, 1, 99999, stdout);
+    fwrite(ac, 1, 99999, stdout);
+    fwrite(P, 1, 99999, stdout);
+    return 0;
+#else
+    /* Look up the address of "main" in the symbol table and call it.
+     * We put main in at a known offset, so we know the address.
+     */
+    return (*(int(*)()) *(int*) (P + 592))(argc, argv);
+#endif
+}
+
diff --git a/libacc/test b/libacc/test
new file mode 100755
index 0000000..2f999c1
--- /dev/null
+++ b/libacc/test
@@ -0,0 +1,2 @@
+#!/bin/sh
+gcc acc.c -ldl -o tests/acc && tests/acc tests/otcc.c >tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig
diff --git a/libacc/tests/.gitignore b/libacc/tests/.gitignore
new file mode 100644
index 0000000..9974532
--- /dev/null
+++ b/libacc/tests/.gitignore
@@ -0,0 +1,2 @@
+acc
+*.out
diff --git a/libacc/tests/hello.c b/libacc/tests/hello.c
new file mode 100644
index 0000000..585ce6c
--- /dev/null
+++ b/libacc/tests/hello.c
@@ -0,0 +1,3 @@
+main(a,b) {
+    printf("Hello, world\n");
+}
diff --git a/libacc/tests/hello.out-orig b/libacc/tests/hello.out-orig
new file mode 100644
index 0000000..b71181a
--- /dev/null
+++ b/libacc/tests/hello.out-orig
Binary files differ
diff --git a/libacc/tests/otcc.c b/libacc/tests/otcc.c
new file mode 100644
index 0000000..577fcf3
--- /dev/null
+++ b/libacc/tests/otcc.c
@@ -0,0 +1,446 @@
+#include <stdio.h>
+#define k *(int*)
+#define a if(
+#define c ad()
+#define i else
+#define p while(
+#define x *(char*)
+#define b ==
+#define V =calloc(1,99999)
+#define f ()
+#define J return
+#define l ae(
+#define n e)
+#define u d!=
+#define F int 
+#define y (j)
+#define r m=
+#define t +4
+F d,z,C,h,P,K,ac,q,G,v,Q,R,D,L,W,M;
+E(n{
+x D++=e;
+}
+o f{
+a L){
+h=x L++;
+a h b 2){
+L=0;
+h=W;
+}
+}
+i h=fgetc(Q);
+}
+X f{
+J isalnum(h)|h b 95;
+}
+Y f{
+a h b 92){
+o f;
+a h b 110)h=10;
+}
+}
+c{
+F e,j,m;
+p isspace(h)|h b 35){
+a h b 35){
+o f;
+c;
+a d b 536){
+c;
+E(32);
+k d=1;
+k(d t)=D;
+}
+p h!=10){
+E(h);
+o f;
+}
+E(h);
+E(2);
+}
+o f;
+}
+C=0;
+d=h;
+a X f){
+E(32);
+M=D;
+p X f){
+E(h);
+o f;
+}
+a isdigit(d)){
+z=strtol(M,0,0);
+d=2;
+}
+i{
+x D=32;
+d=strstr(R,M-1)-R;
+x D=0;
+d=d*8+256;
+a d>536){
+d=P+d;
+a k d b 1){
+L=k(d t);
+W=h;
+o f;
+c;
+}
+}
+}
+}
+i{
+o f;
+a d b 39){
+d=2;
+Y f;
+z=h;
+o f;
+o f;
+}
+i a d b 47&h b 42){
+o f;
+p h){
+p h!=42)o f;
+o f;
+a h b 47)h=0;
+}
+o f;
+c;
+}
+i{
+e="++#m--%am*@R<^1c/@%[_[H3c%@%[_[H3c+@.B#d-@%:_^BKd<<Z/03e>>`/03e<=0f>=/f<@.f>@1f==&g!='g&&k||#l&@.BCh^@.BSi|@.B+j~@/%Yd!@&d*@b";
+p j=x e++){
+r x e++;
+z=0;
+p(C=x e++-98)<0)z=z*64+C+64;
+a j b d&(m b h|m b 64)){
+a m b h){
+o f;
+d=1;
+}
+break;
+}
+}
+}
+}
+}
+l g){
+p g&&g!=-1){
+x q++=g;
+g=g>>8;
+}
+}
+A(n{
+F g;
+p n{
+g=k e;
+k e=q-e-4;
+e=g;
+}
+}
+s(g,n{
+l g);
+k q=e;
+e=q;
+q=q t;
+J e;
+}
+H(n{
+s(184,n;
+}
+B(n{
+J s(233,n;
+}
+S(j,n{
+l 1032325);
+J s(132+j,n;
+}
+Z(n{
+l 49465);
+H(0);
+l 15);
+l e+144);
+l 192);
+}
+N(j,n{
+l j+131);
+s((e<512)<<7|5,n;
+}
+T y{
+F g,e,m,aa;
+g=1;
+a d b 34){
+H(v);
+p h!=34){
+Y f;
+x v++=h;
+o f;
+}
+x v=0;
+v=v t&-4;
+o f;
+c;
+}
+i{
+aa=C;
+r z;
+e=d;
+c;
+a e b 2){
+H(m);
+}
+i a aa b 2){
+T(0);
+s(185,0);
+a e b 33)Z(m);
+i l m);
+}
+i a e b 40){
+w f;
+c;
+}
+i a e b 42){
+c;
+e=d;
+c;
+c;
+a d b 42){
+c;
+c;
+c;
+c;
+e=0;
+}
+c;
+T(0);
+a d b 61){
+c;
+l 80);
+w f;
+l 89);
+l 392+(e b 256));
+}
+i a n{
+a e b 256)l 139);
+i l 48655);
+q++;
+}
+}
+i a e b 38){
+N(10,k d);
+c;
+}
+i{
+g=k e;
+a!g)g=dlsym(0,M);
+a d b 61&j){
+c;
+w f;
+N(6,g);
+}
+i a u 40){
+N(8,g);
+a C b 11){
+N(0,g);
+l z);
+c;
+}
+}
+}
+}
+a d b 40){
+a g b 1)l 80);
+r s(60545,0);
+c;
+j=0;
+p u 41){
+w f;
+s(2393225,j);
+a d b 44)c;
+j=j t;
+}
+k r j;
+c;
+a!g){
+e=e t;
+k e=s(232,k n;
+}
+i a g b 1){
+s(2397439,j);
+j=j t;
+}
+i{
+s(232,g-q-5);
+}
+a j)s(50305,j);
+}
+}
+O y{
+F e,g,m;
+a j--b 1)T(1);
+i{
+O y;
+r 0;
+p j b C){
+g=d;
+e=z;
+c;
+a j>8){
+r S(e,m);
+O y;
+}
+i{
+l 80);
+O y;
+l 89);
+a j b 4|j b 5){
+Z(n;
+}
+i{
+l n;
+a g b 37)l 146);
+}
+}
+}
+a m&&j>8){
+r S(e,m);
+H(e^1);
+B(5);
+A(m);
+H(n;
+}
+}
+}
+w f{
+O(11);
+}
+U f{
+w f;
+J S(0,0);
+}
+I y{
+F m,g,e;
+a d b 288){
+c;
+c;
+r U f;
+c;
+I y;
+a d b 312){
+c;
+g=B(0);
+A(m);
+I y;
+A(g);
+}
+i{
+A(m);
+}
+}
+i a d b 352|d b 504){
+e=d;
+c;
+c;
+a e b 352){
+g=q;
+r U f;
+}
+i{
+a u 59)w f;
+c;
+g=q;
+r 0;
+a u 59)r U f;
+c;
+a u 41){
+e=B(0);
+w f;
+B(g-q-5);
+A(n;
+g=e t;
+}
+}
+c;
+I(&m);
+B(g-q-5);
+A(m);
+}
+i a d b 123){
+c;
+ab(1);
+p u 125)I y;
+c;
+}
+i{
+a d b 448){
+c;
+a u 59)w f;
+K=B(K);
+}
+i a d b 400){
+c;
+k j=B(k j);
+}
+i a u 59)w f;
+c;
+}
+}
+ab y{
+F m;
+p d b 256|u-1&!j){
+a d b 256){
+c;
+p u 59){
+a j){
+G=G t;
+k d=-G;
+}
+i{
+k d=v;
+v=v t;
+}
+c;
+a d b 44)c;
+}
+c;
+}
+i{
+A(k(d t));
+k d=q;
+c;
+c;
+r 8;
+p u 41){
+k d=m;
+r m t;
+c;
+a d b 44)c;
+}
+c;
+K=G=0;
+l 15042901);
+r s(60545,0);
+I(0);
+A(K);
+l 50121);
+k r G;
+}
+}
+}
+main(g,n{
+Q=stdin;
+a g-->1){
+e=e t;
+Q=fopen(k e,"r");
+}
+D=strcpy(R V," int if else while break return for define main ")+48;
+v V;
+q=ac V;
+P V;
+o f;
+c;
+ab(0);
+J(*(int(*)f)k(P+592))(g,n;
+}
+
diff --git a/libacc/tests/otcc.out-orig b/libacc/tests/otcc.out-orig
new file mode 100644
index 0000000..db2ac83
--- /dev/null
+++ b/libacc/tests/otcc.out-orig
Binary files differ
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index a43f7e3..b6d806e 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -37,6 +37,9 @@
 	properties.c \
 	threads.c
 
+commonHostSources := \
+        ashmem-host.c
+
 # some files must not be compiled when building against Mingw
 # they correspond to features not used by our host development tools
 # which are also hard or even impossible to port to native Win32
@@ -60,16 +63,18 @@
         selector.c \
         fdevent.c \
         tztime.c \
-        tzstrftime.c \
         adb_networking.c \
-	zygote.c
+        zygote.c
+
+    commonHostSources += \
+        tzstrftime.c
 endif
 
 
 # Static library for host
 # ========================================================
 LOCAL_MODULE := libcutils
-LOCAL_SRC_FILES := $(commonSources) ashmem-host.c
+LOCAL_SRC_FILES := $(commonSources) $(commonHostSources)
 LOCAL_LDLIBS := -lpthread
 LOCAL_STATIC_LIBRARIES := liblog
 include $(BUILD_HOST_STATIC_LIBRARY)
@@ -81,7 +86,7 @@
 # ========================================================
 include $(CLEAR_VARS)
 LOCAL_MODULE := libcutils
-LOCAL_SRC_FILES := $(commonSources) memory.c dlmalloc_stubs.c ashmem-host.c
+LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) memory.c dlmalloc_stubs.c
 LOCAL_LDLIBS := -lpthread
 LOCAL_SHARED_LIBRARIES := liblog
 include $(BUILD_SHARED_LIBRARY)
diff --git a/libcutils/strdup16to8.c b/libcutils/strdup16to8.c
index fadaabe..1a8ba86 100644
--- a/libcutils/strdup16to8.c
+++ b/libcutils/strdup16to8.c
@@ -15,6 +15,8 @@
 ** limitations under the License.
 */
 
+#include <limits.h>  /* for SIZE_MAX */
+
 #include <cutils/jstring.h>
 #include <assert.h>
 #include <stdlib.h>
@@ -26,19 +28,67 @@
  */
 extern size_t strnlen16to8(const char16_t* utf16Str, size_t len)
 {
-   size_t utf8Len = 0;
+    size_t utf8Len = 0;
 
-   while (len--) {
-       unsigned int uic = *utf16Str++;
+    /* A small note on integer overflow. The result can
+     * potentially be as big as 3*len, which will overflow
+     * for len > SIZE_MAX/3.
+     *
+     * Moreover, the result of a strnlen16to8 is typically used
+     * to allocate a destination buffer to strncpy16to8 which
+     * requires one more byte to terminate the UTF-8 copy, and
+     * this is generally done by careless users by incrementing
+     * the result without checking for integer overflows, e.g.:
+     *
+     *   dst = malloc(strnlen16to8(utf16,len)+1)
+     *
+     * Due to this, the following code will try to detect
+     * overflows, and never return more than (SIZE_MAX-1)
+     * when it detects one. A careless user will try to malloc
+     * SIZE_MAX bytes, which will return NULL which can at least
+     * be detected appropriately.
+     *
+     * As far as I know, this function is only used by strndup16(),
+     * but better be safe than sorry.
+     */
 
-       if (uic > 0x07ff)
-           utf8Len += 3;
-       else if (uic > 0x7f || uic == 0)
-           utf8Len += 2;
-       else
-           utf8Len++;
-   }
-   return utf8Len;
+    /* Fast path for the usual case where 3*len is < SIZE_MAX-1.
+     */
+    if (len < (SIZE_MAX-1)/3) {
+        while (len--) {
+            unsigned int uic = *utf16Str++;
+
+            if (uic > 0x07ff)
+                utf8Len += 3;
+            else if (uic > 0x7f || uic == 0)
+                utf8Len += 2;
+            else
+                utf8Len++;
+        }
+        return utf8Len;
+    }
+
+    /* The slower but paranoid version */
+    while (len--) {
+        unsigned int  uic     = *utf16Str++;
+        size_t        utf8Cur = utf8Len;
+
+        if (uic > 0x07ff)
+            utf8Len += 3;
+        else if (uic > 0x7f || uic == 0)
+            utf8Len += 2;
+        else
+            utf8Len++;
+
+        if (utf8Len < utf8Cur) /* overflow detected */
+            return SIZE_MAX-1;
+    }
+
+    /* don't return SIZE_MAX to avoid common user bug */
+    if (utf8Len == SIZE_MAX)
+        utf8Len = SIZE_MAX-1;
+
+    return utf8Len;
 }
 
 
@@ -50,7 +100,7 @@
  *
  * Make sure you allocate "utf8Str" with the result of strlen16to8() + 1,
  * not just "len".
- * 
+ *
  * Please note, a terminated \0 is always added, so your result will always
  * be "strlen16to8() + 1" bytes long.
  */
@@ -58,6 +108,10 @@
 {
     char* utf8cur = utf8Str;
 
+    /* Note on overflows: We assume the user did check the result of
+     * strnlen16to8() properly or at a minimum checked the result of
+     * its malloc(SIZE_MAX) in case of overflow.
+     */
     while (len--) {
         unsigned int uic = *utf16Str++;
 
@@ -73,8 +127,8 @@
 
             if (uic == 0) {
                 break;
-            }           
-        }       
+            }
+        }
     }
 
    *utf8cur = '\0';
@@ -85,20 +139,30 @@
 /**
  * Convert a UTF-16 string to UTF-8.
  *
- * Make sure you allocate "dest" with the result of strblen16to8(),
- * not just "strlen16()".
  */
 char * strndup16to8 (const char16_t* s, size_t n)
 {
-    char *ret;
+    char*   ret;
+    size_t  len;
 
     if (s == NULL) {
         return NULL;
     }
 
-    ret = malloc(strnlen16to8(s, n) + 1);
+    len = strnlen16to8(s, n);
+
+    /* We are paranoid, and we check for SIZE_MAX-1
+     * too since it is an overflow value for our
+     * strnlen16to8 implementation.
+     */
+    if (len >= SIZE_MAX-1)
+        return NULL;
+
+    ret = malloc(len + 1);
+    if (ret == NULL)
+        return NULL;
 
     strncpy16to8 (ret, s, n);
-    
-    return ret;    
+
+    return ret;
 }
diff --git a/liblog/logprint.c b/liblog/logprint.c
index 2cf1254..080f9e3 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -23,7 +23,6 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
-#include <alloca.h>
 #include <assert.h>
 #include <arpa/inet.h>
 
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index 50eb5f5..0cc85d9 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -64,12 +64,14 @@
 LOCAL_MODULE:= libpixelflinger
 LOCAL_SRC_FILES := $(PIXELFLINGER_SRC_FILES)
 LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
+
 ifneq ($(BUILD_TINY_ANDROID),true)
 # Really this should go away entirely or at least not depend on
 # libhardware, but this at least gets us built.
 LOCAL_SHARED_LIBRARIES += libhardware_legacy
 LOCAL_CFLAGS += -DWITH_LIB_HARDWARE
 endif
+
 ifeq ($(TARGET_ARCH),arm)
 LOCAL_WHOLE_STATIC_LIBRARIES := libpixelflinger_armv6
 endif
diff --git a/toolbox/ls.c b/toolbox/ls.c
index f609df21..087e4d5 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -15,9 +15,10 @@
 #include <linux/kdev_t.h>
 
 // bits for flags argument
-#define LIST_LONG       (1 << 0)
-#define LIST_ALL        (1 << 1)
-#define LIST_RECURSIVE  (1 << 2)
+#define LIST_LONG           (1 << 0)
+#define LIST_ALL            (1 << 1)
+#define LIST_RECURSIVE      (1 << 2)
+#define LIST_DIRECTORIES    (1 << 3)
 
 // fwd
 static int listpath(const char *name, int flags);
@@ -238,7 +239,7 @@
         return -1;
     }
 
-    if (S_ISDIR(s.st_mode)) {
+    if ((flags & LIST_DIRECTORIES) == 0 && S_ISDIR(s.st_mode)) {
         if (flags & LIST_RECURSIVE)
             printf("\n%s:\n", name);
         return listdir(name, flags);
@@ -269,6 +270,8 @@
                 flags |= LIST_ALL;
             } else if (!strcmp(argv[i], "-R")) {
                 flags |= LIST_RECURSIVE;
+            } else if (!strcmp(argv[i], "-d")) {
+                flags |= LIST_DIRECTORIES;
             } else {
                 listed++;
                 if(listpath(argv[i], flags) != 0) {
diff --git a/toolbox/mkdosfs.c b/toolbox/mkdosfs.c
index 744aad1..66e720b 100644
--- a/toolbox/mkdosfs.c
+++ b/toolbox/mkdosfs.c
@@ -393,7 +393,7 @@
         bpb.bsec = length / bpb.bps;
         bpb.spt = bpb.bsec;
         // use FAT32 for 2 gig or greater 
-        if (length >= 2 *1024 *1024 *1024) {
+        if (length >= 2LL *1024 *1024 *1024) {
             fat = 32;
         } else {
             fat = 16;
diff --git a/toolbox/mount.c b/toolbox/mount.c
index ef13e1f..395c943 100644
--- a/toolbox/mount.c
+++ b/toolbox/mount.c
@@ -138,14 +138,17 @@
 
     if (loop) {
         int file_fd, device_fd;
+        int flags;
+
+        flags = (rwflag & MS_RDONLY) ? O_RDONLY : O_RDWR;
         
         // FIXME - only one loop mount supported at a time
-        file_fd = open(dev, O_RDWR);
+        file_fd = open(dev, flags);
         if (file_fd < -1) {
             perror("open backing file failed");
             return 1;
         }
-        device_fd = open(LOOP_DEVICE, O_RDWR);
+        device_fd = open(LOOP_DEVICE, flags);
         if (device_fd < -1) {
             perror("open loop device failed");
             close(file_fd);
diff --git a/toolbox/smd.c b/toolbox/smd.c
index 65ff994e..91e495c 100644
--- a/toolbox/smd.c
+++ b/toolbox/smd.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
diff --git a/vold/mmc.c b/vold/mmc.c
index 0f08964..fb61b2f 100644
--- a/vold/mmc.c
+++ b/vold/mmc.c
@@ -250,7 +250,7 @@
     char filename[255];
     char *uevent_buffer;
     ssize_t sz;
-    char *uevent_params[4];
+    char *uevent_params[5];
     char tmp[255];
     FILE *fp;
     char line[255];