Code drop from //branches/cupcake/...@124589
diff --git a/libc/unistd/ftime.c b/libc/unistd/ftime.c
new file mode 100644
index 0000000..6513593
--- /dev/null
+++ b/libc/unistd/ftime.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/timeb.h>
+
+int ftime(struct timeb *tb)
+{
+    struct timeval  tv;
+    struct timezone tz;
+
+    if (gettimeofday (&tv, &tz) < 0)
+        return -1;
+
+    tb->time    = tv.tv_sec;
+    tb->millitm = (tv.tv_usec + 500) / 1000;
+
+    if (tb->millitm == 1000) {
+        ++tb->time;
+        tb->millitm = 0;
+    }
+    tb->timezone = tz.tz_minuteswest;
+    tb->dstflag  = tz.tz_dsttime;
+
+    return 0;
+}
diff --git a/libc/unistd/initgroups.c b/libc/unistd/initgroups.c
new file mode 100644
index 0000000..dea6d96
--- /dev/null
+++ b/libc/unistd/initgroups.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <grp.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#define  INIT_GROUPS  2
+
+int
+initgroups (const char *user, gid_t group)
+{
+    gid_t   groups0[ INIT_GROUPS ];
+    gid_t*  groups    = groups0;
+    int     ret       = -1;
+    int     numgroups = INIT_GROUPS;
+
+    if (getgrouplist(user, group, groups, &numgroups) < 0) {
+        groups = malloc(numgroups*sizeof(groups[0]));
+        if (groups == NULL)
+            return -1;
+        if (getgrouplist(user,group,groups,&numgroups) < 0) {
+            goto EXIT;
+        }
+    }
+
+    ret = setgroups(numgroups, groups);
+
+EXIT:
+    if (groups != groups0)
+        free(groups);
+
+    return ret;
+}
diff --git a/libc/unistd/opendir.c b/libc/unistd/opendir.c
index 1b06057..afa3ea0 100644
--- a/libc/unistd/opendir.c
+++ b/libc/unistd/opendir.c
@@ -203,3 +203,65 @@
     pthread_mutex_unlock( &dir->_DIR_lock );
 }
 
+
+int alphasort(const void *a, const void *b)
+{
+        struct dirent **d1, **d2;
+
+        d1 = (struct dirent **) a;
+        d2 = (struct dirent **) b;
+        return strcmp((*d1)->d_name, (*d2)->d_name);
+}
+
+
+int scandir(const char *dir, struct dirent ***namelist,
+            int(*filter)(const struct dirent *),
+            int(*compar)(const struct dirent **, const struct dirent **))
+{
+    DIR *d;
+    int n_elem = 0;
+    struct dirent *this_de, *de;
+    struct dirent **de_list = NULL;
+    int de_list_size = 0;
+
+    d = opendir(dir);
+    if (d == NULL) {
+        return -1;
+    }
+
+    while ((this_de = readdir(d)) != NULL) {
+        if (filter && (*filter)(this_de) == 0) {
+            continue;
+        }
+        if (n_elem == 0) {
+            de_list_size = 4;
+            de_list = (struct dirent **) 
+                    malloc(sizeof(struct dirent *)*de_list_size);
+            if (de_list == NULL) {
+                return -1;
+            }
+        }
+        else if (n_elem == de_list_size) {
+            struct dirent **de_list_new;
+
+            de_list_size += 10;
+            de_list_new = (struct dirent **) 
+                    realloc(de_list, sizeof(struct dirent *)*de_list_size);
+            if (de_list_new == NULL) {
+                free(de_list);
+                return -1;
+            }
+            de_list = de_list_new;
+        }
+        de = (struct dirent *) malloc(sizeof(struct dirent));
+        *de = *this_de;
+        de_list[n_elem++] = de;
+    }
+    closedir(d);
+    if (n_elem && compar) {
+        qsort(de_list, n_elem, sizeof(struct dirent *), 
+              (int (*)(const void *, const void *)) compar);
+    }
+    *namelist = de_list;
+    return n_elem;
+}
diff --git a/libc/unistd/pread.c b/libc/unistd/pread.c
index 7bfaeaa..d2f71f7 100644
--- a/libc/unistd/pread.c
+++ b/libc/unistd/pread.c
@@ -11,39 +11,24 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of Google Inc. nor the names of its contributors may
+ *       be used to endorse or promote products derived from this software
+ *       without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
+*/
 #include <sys/types.h>
 #include <unistd.h>
-#include <errno.h>
 
-/*
- * Non-atomic emulation of the pread system call.
- */
-ssize_t pread(int fd, void *buf, size_t count, off_t offset)
+extern int __pread64(int fd, void *buf, size_t nbytes, off_t lo, off_t hi);
+
+ssize_t pread(int fd, void *buf, size_t nbytes, off_t offset)
 {
-    int save_errno;
-    ssize_t nread;
-    off_t save_offset = lseek(fd, offset, SEEK_SET);
-    if (save_offset < 0) {
-        return -1;
-    }
-    nread = read(fd, buf, count);
-    save_errno = errno;
-    /* Even if the read failed, try to restore the seek pointer */
-    lseek(fd, save_offset, SEEK_SET);
-    errno = save_errno;
-    return nread;
+    return __pread64(fd, buf, nbytes, offset, 0);
 }
+
diff --git a/libc/unistd/pwrite.c b/libc/unistd/pwrite.c
index 7f5e071..5adf40a 100644
--- a/libc/unistd/pwrite.c
+++ b/libc/unistd/pwrite.c
@@ -27,23 +27,11 @@
  */
 #include <sys/types.h>
 #include <unistd.h>
-#include <errno.h>
 
-/*
- * Non-atomic emulation of the pwrite system call.
- */
-ssize_t pwrite(int fd, void *buf, size_t count, off_t offset)
+extern int __pwrite64(int fd, void *buf, size_t nbytes, off_t lo, off_t hi);
+
+ssize_t pwrite(int fd, void *buf, size_t nbytes, off_t offset)
 {
-    int save_errno;
-    ssize_t nwrite;
-    off_t save_offset = lseek(fd, offset, SEEK_SET);
-    if (save_offset < 0) {
-        return -1;
-    }
-    nwrite = write(fd, buf, count);
-    save_errno = errno;
-    /* Even if the write failed, try to restore the seek pointer */
-    lseek(fd, save_offset, SEEK_SET);
-    errno = save_errno;
-    return nwrite;
+    return __pwrite64(fd, buf, nbytes, offset, 0);
 }
+
diff --git a/libc/unistd/siginterrupt.c b/libc/unistd/siginterrupt.c
new file mode 100644
index 0000000..4e91edb
--- /dev/null
+++ b/libc/unistd/siginterrupt.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <signal.h>
+
+/* this is only useful for legacy programs */
+int siginterrupt(int sig, int flag) 
+{
+    struct sigaction act;
+
+    (void) sigaction(sig, NULL, &act);
+
+    if (flag)
+        act.sa_flags &= ~SA_RESTART;
+    else
+        act.sa_flags |= SA_RESTART;
+
+    return sigaction(sig, &act, NULL);
+}
+
diff --git a/libc/unistd/tcgetpgrp.c b/libc/unistd/tcgetpgrp.c
new file mode 100644
index 0000000..4355014
--- /dev/null
+++ b/libc/unistd/tcgetpgrp.c
@@ -0,0 +1,34 @@
+/* bionic/unistd/tcgetpgrp.c
+**
+** Copyright 2006, The Android Open Source Project
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+**     * Redistributions of source code must retain the above copyright
+**       notice, this list of conditions and the following disclaimer.
+**     * Redistributions in binary form must reproduce the above copyright
+**       notice, this list of conditions and the following disclaimer in the
+**       documentation and/or other materials provided with the distribution.
+**     * Neither the name of Google Inc. nor the names of its contributors may
+**       be used to endorse or promote products derived from this software
+**       without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
+** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#include <unistd.h>
+#include <termios.h>
+
+pid_t tcgetpgrp(int fd)
+{
+    pid_t _pid;
+    return ioctl(fd, TIOCGPGRP, &_pid) ? (pid_t)-1 : _pid;
+}
diff --git a/libc/unistd/tcsetpgrp.c b/libc/unistd/tcsetpgrp.c
new file mode 100644
index 0000000..b83b997
--- /dev/null
+++ b/libc/unistd/tcsetpgrp.c
@@ -0,0 +1,33 @@
+/* bionic/unistd/tcsetpgrp.c
+**
+** Copyright 2006, The Android Open Source Project
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+**     * Redistributions of source code must retain the above copyright
+**       notice, this list of conditions and the following disclaimer.
+**     * Redistributions in binary form must reproduce the above copyright
+**       notice, this list of conditions and the following disclaimer in the
+**       documentation and/or other materials provided with the distribution.
+**     * Neither the name of Google Inc. nor the names of its contributors may
+**       be used to endorse or promote products derived from this software
+**       without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
+** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#include <unistd.h>
+#include <termios.h>
+
+int tcsetpgrp(int fd, pid_t _pid)
+{
+    return ioctl(fd, TIOCSPGRP, &_pid);
+}