eclair snapshot
diff --git a/toolbox/ls.c b/toolbox/ls.c
index f609df21..b221074 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -15,9 +15,11 @@
 #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)
+#define LIST_SIZE           (1 << 4)
 
 // fwd
 static int listpath(const char *name, int flags);
@@ -84,7 +86,19 @@
     }
 }
 
-static int listfile(const char *path, int flags)
+static int listfile_size(const char *path, int flags)
+{
+    struct stat s;
+
+    if (lstat(path, &s) < 0)
+        return -1;
+
+    /* blocks are 512 bytes, we want output to be KB */
+    printf("%lld %s\n", s.st_blocks / 2, path);
+    return 0;
+}
+
+static int listfile_long(const char *path, int flags)
 {
     struct stat s;
     char date[32];
@@ -155,6 +169,30 @@
     return 0;
 }
 
+static int listfile(const char *dirname, const char *filename, int flags)
+{
+    if ((flags & (LIST_LONG | LIST_SIZE)) == 0) {
+        printf("%s\n", filename);
+        return 0;
+    }
+
+    char tmp[4096];
+    const char* pathname = filename;
+
+    if (dirname != NULL) {
+        snprintf(tmp, sizeof(tmp), "%s/%s", dirname, filename);
+        pathname = tmp;
+    } else {
+        pathname = filename;
+    }
+
+    if ((flags & LIST_LONG) != 0) {
+        return listfile_long(pathname, flags);
+    } else /*((flags & LIST_SIZE) != 0)*/ {
+        return listfile_size(pathname, flags);
+    }
+}
+
 static int listdir(const char *name, int flags)
 {
     char tmp[4096];
@@ -170,12 +208,8 @@
     while((de = readdir(d)) != 0){
         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
         if(de->d_name[0] == '.' && (flags & LIST_ALL) == 0) continue;
-        if ((flags & LIST_LONG) != 0) {
-            sprintf(tmp, "%s/%s", name, de->d_name);
-            listfile(tmp, flags);
-        } else {
-            printf("%s\n", de->d_name);
-        }
+
+        listfile(name, de->d_name, flags);
     }
 
     if (flags & LIST_RECURSIVE) {
@@ -190,8 +224,10 @@
             if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0)
                 continue;
 
-            if (!strcmp(name, "/")) sprintf(tmp, "/%s", de->d_name);
-            else sprintf(tmp, "%s/%s", name, de->d_name);
+            if (!strcmp(name, "/"))
+                snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
+            else
+                snprintf(tmp, sizeof(tmp), "%s/%s", name, de->d_name);
 
             /*
              * If the name ends in a '/', use stat() so we treat it like a
@@ -238,18 +274,13 @@
         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);
     } else {
-        if ((flags & LIST_LONG) != 0) {
-            /* yeah this calls stat() again*/
-            return listfile(name, flags);
-        } else {
-            printf("%s\n", name);
-            return 0;
-        }
+        /* yeah this calls stat() again*/
+        return listfile(NULL, name, flags);
     }
 }
 
@@ -263,12 +294,16 @@
         int err = 0;
 
         for (i = 1; i < argc; i++) {
-            if(!strcmp(argv[i], "-l")) {
+            if (!strcmp(argv[i], "-l")) {
                 flags |= LIST_LONG;
+            } else if (!strcmp(argv[i], "-s")) {
+                flags |= LIST_SIZE;
             } else if (!strcmp(argv[i], "-a")) {
                 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/mount.c b/toolbox/mount.c
index 86047a9..472c952 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/ps.c b/toolbox/ps.c
index 3b86fa2..bc50cfa 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -11,6 +11,8 @@
 
 #include <pwd.h>
 
+#include <cutils/sched_policy.h>
+
 
 static char *nexttoksep(char **strp, char *sep)
 {
@@ -24,6 +26,7 @@
 
 #define SHOW_PRIO 1
 #define SHOW_TIME 2
+#define SHOW_POLICY 4
 
 static int display_flags = 0;
 
@@ -138,12 +141,26 @@
     }
     
     if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {
-        printf("%-8s %-5d %-5d %-5d %-5d", user, pid, ppid, vss / 1024, rss * 4);
+        printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
         if(display_flags&SHOW_PRIO)
             printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
+        if (display_flags & SHOW_POLICY) {
+            SchedPolicy p;
+            if (get_sched_policy(pid, &p) < 0)
+                printf(" un ");
+            else {
+                if (p == SP_BACKGROUND)
+                    printf(" bg ");
+                else if (p == SP_FOREGROUND)
+                    printf(" fg ");
+                else
+                    printf(" er ");
+            }
+        }
         printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);
         if(display_flags&SHOW_TIME)
             printf(" (u:%d, s:%d)", utime, stime);
+
         printf("\n");
     }
     return 0;
@@ -186,6 +203,8 @@
             threads = 1;
         } else if(!strcmp(argv[1],"-x")) {
             display_flags |= SHOW_TIME;
+        } else if(!strcmp(argv[1],"-P")) {
+            display_flags |= SHOW_POLICY;
         } else if(!strcmp(argv[1],"-p")) {
             display_flags |= SHOW_PRIO;
         }  else if(isdigit(argv[1][0])){
@@ -197,8 +216,9 @@
         argv++;
     }
 
-    printf("USER     PID   PPID  VSIZE RSS   %sWCHAN    PC         NAME\n", 
-           (display_flags&SHOW_PRIO)?"PRIO  NICE  RTPRI SCHED ":"");
+    printf("USER     PID   PPID  VSIZE  RSS   %s %s WCHAN    PC         NAME\n", 
+           (display_flags&SHOW_PRIO)?"PRIO  NICE  RTPRI SCHED ":"",
+           (display_flags&SHOW_POLICY)?"PCY " : "");
     while((de = readdir(d)) != 0){
         if(isdigit(de->d_name[0])){
             int pid = atoi(de->d_name);
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/toolbox/top.c b/toolbox/top.c
index dcc0843..daade6d 100644
--- a/toolbox/top.c
+++ b/toolbox/top.c
@@ -39,6 +39,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <cutils/sched_policy.h>
+
 struct cpu_info {
     long unsigned utime, ntime, stime, itime;
     long unsigned iowtime, irqtime, sirqtime;
@@ -64,6 +66,7 @@
     long vss;
     long rss;
     int num_threads;
+    char policy[32];
 };
 
 struct proc_list {
@@ -88,6 +91,7 @@
 static void free_proc(struct proc_info *proc);
 static void read_procs(void);
 static int read_stat(char *filename, struct proc_info *proc);
+static void read_policy(int pid, struct proc_info *proc);
 static void add_proc(int proc_num, struct proc_info *proc);
 static int read_cmdline(char *filename, struct proc_info *proc);
 static int read_status(char *filename, struct proc_info *proc);
@@ -260,6 +264,8 @@
             sprintf(filename, "/proc/%d/status", pid);
             read_status(filename, proc);
 
+            read_policy(pid, proc);
+
             proc->num_threads = 0;
         } else {
             sprintf(filename, "/proc/%d/cmdline", pid);
@@ -289,6 +295,8 @@
                 sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);
                 read_stat(filename, proc);
 
+                read_policy(tid, proc);
+
                 strcpy(proc->name, cur_proc.name);
                 proc->uid = cur_proc.uid;
                 proc->gid = cur_proc.gid;
@@ -368,6 +376,20 @@
     return 0;
 }
 
+static void read_policy(int pid, struct proc_info *proc) {
+    SchedPolicy p;
+    if (get_sched_policy(pid, &p) < 0)
+        strcpy(proc->policy, "unk");
+    else {
+        if (p == SP_BACKGROUND)
+            strcpy(proc->policy, "bg");
+        else if (p == SP_FOREGROUND)
+            strcpy(proc->policy, "fg");
+        else
+            strcpy(proc->policy, "er");
+    }
+}
+
 static int read_status(char *filename, struct proc_info *proc) {
     FILE *file;
     char line[MAX_LINE];
@@ -432,9 +454,9 @@
             total_delta_time);
     printf("\n");
     if (!threads) 
-        printf("%5s %4s %1s %5s %7s %7s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "UID", "Name");
+        printf("%5s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name");
     else
-        printf("%5s %5s %4s %1s %7s %7s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "UID", "Thread", "Proc");
+        printf("%5s %5s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc");
 
     for (i = 0; i < num_new_procs; i++) {
         proc = new_procs[i];
@@ -456,11 +478,11 @@
             group_str = group_buf;
         }
         if (!threads) 
-            printf("%5d %3ld%% %c %5d %6ldK %6ldK %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
-                proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
+            printf("%5d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
+                proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
         else
-            printf("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state,
-                proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->tname, proc->name);
+            printf("%5d %5d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state,
+                proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name);
     }
 }