OSDN Git Service

Cosmetic tweak: no trailing spaces when ./toybox lists command names.
[android-x86/external-toybox.git] / main.c
diff --git a/main.c b/main.c
index fdb4749..ed6706a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -26,6 +26,8 @@ struct toy_list *toy_find(char *name)
 {
   int top, bottom, middle;
 
+  if (!CFG_TOYBOX) return 0;
+
   // If the name starts with "toybox" accept that as a match.  Otherwise
   // skip the first entry, which is out of order.
 
@@ -66,8 +68,10 @@ static void toy_singleinit(struct toy_list *which, char *argv[])
   toys.which = which;
   toys.argv = argv;
 
+  if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE));
+
   if (CFG_TOYBOX_HELP_DASHDASH && argv[1] && !strcmp(argv[1], "--help")) {
-    if (toys.which == toy_list && toys.argv[2])
+    if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
       if (!(toys.which = toy_find(toys.argv[2]))) return;
     show_help();
     xexit();
@@ -94,8 +98,10 @@ void toy_init(struct toy_list *which, char *argv[])
     uid_t uid = getuid(), euid = geteuid();
 
     if (!(which->flags & TOYFLAG_STAYROOT)) {
-      if (uid != euid)
-        if (!setuid(euid=uid)) perror_exit("setuid"); // drop root
+      if (uid != euid) {
+        if (!setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
+        else euid = uid;
+      }
     } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
       error_msg("Not installed suid root");
 
@@ -115,12 +121,18 @@ void toy_init(struct toy_list *which, char *argv[])
 }
 
 // Like exec() but runs an internal toybox command instead of another file.
-// Only returns if it can't find the command, otherwise exit() when done.
+// Only returns if it can't run command internally, otherwise exit() when done.
 void toy_exec(char *argv[])
 {
   struct toy_list *which;
 
+  // Return if we can't find it, or need to re-exec to acquire root,
+  // or if stack depth is getting silly.
   if (!(which = toy_find(argv[0]))) return;
+  if (toys.recursion && (which->flags & TOYFLAG_ROOTONLY) && getuid()) return;
+  if (toys.recursion++ > 5) return;
+
+  // Run command
   toy_init(which, argv);
   if (toys.which) toys.which->toy_main();
   if (fflush(NULL) || ferror(stdout)) perror_exit("write");
@@ -154,11 +166,9 @@ list:
         for (j=0; toy_paths[j]; j++)
           if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
       }
-      len += printf("%s ",toy_list[i].name);
-      if (len>65) {
-        xputc('\n');
-        len=0;
-      }
+      len += printf("%s",toy_list[i].name);
+      if (++len > 65) len = 0;
+      xputc(len ? ' ' : '\n');
     }
   }
   xputc('\n');
@@ -166,8 +176,6 @@ list:
 
 int main(int argc, char *argv[])
 {
-  if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "");
-
   if (CFG_TOYBOX) {
     // Trim path off of command name
     *argv = basename(*argv);