Always have ps output to stdout at least one line.

There is at least one app that assumes that you will always get at least
one line of output in stdout from ps.

To fix this, move error output to stdout, and move the check of whether
/proc can be opened until after the ps header is printed.

Bug: 26554285
Change-Id: I6d9342aafd5c6f728735507cdd87a48a8e0373ac
diff --git a/toolbox/ps.c b/toolbox/ps.c
index ecc1c9f..7e70c71 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -264,9 +264,6 @@
     int pidfilter = 0;
     int threads = 0;
 
-    d = opendir("/proc");
-    if(d == 0) return -1;
-
     while(argc > 1){
         if(!strcmp(argv[1],"-t")) {
             threads = 1;
@@ -287,7 +284,10 @@
         } else if(!strcmp(argv[1],"--ppid")) {
             ppid_filter = atoi(argv[2]);
             if (ppid_filter == 0) {
-                fprintf(stderr, "bad ppid '%s'\n", argv[2]);
+                /* Bug 26554285: Use printf because some apps require at least
+                 * one line of output to stdout even for errors.
+                 */
+                printf("bad ppid '%s'\n", argv[2]);
                 return 1;
             }
             argc--;
@@ -295,7 +295,10 @@
         } else {
             pidfilter = atoi(argv[1]);
             if (pidfilter == 0) {
-                fprintf(stderr, "bad pid '%s'\n", argv[1]);
+                /* Bug 26554285: Use printf because some apps require at least
+                 * one line of output to stdout even for errors.
+                 */
+                printf("bad pid '%s'\n", argv[1]);
                 return 1;
             }
         }
@@ -313,6 +316,9 @@
            (int) PC_WIDTH, "PC",
            (display_flags&SHOW_ABI)?"ABI " : "");
 
+    d = opendir("/proc");
+    if(d == 0) return -1;
+
     while((de = readdir(d)) != 0){
         if(isdigit(de->d_name[0])){
             int pid = atoi(de->d_name);