ls: make --color more compatible with coreutils
[oweals/busybox.git] / coreutils / chroot.c
index ca0bfcf3fbc6c58a3506b5c2f36b8049f1273b83..9b3d700444cd47749142029b0d5ad298b518e376 100644 (file)
@@ -1,32 +1,37 @@
-#include "internal.h"
-#include <stdio.h>
-#include <unistd.h>
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini chroot implementation for busybox
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
 
+/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
 
-const char chroot_usage[] = "chroot directory [command]\n"
-  "Run a command with special root directory.\n";
+#include "libbb.h"
 
-extern int
-chroot_main (struct FileInfo *i, int argc, char **argv)
+int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int chroot_main(int argc, char **argv)
 {
-  char *prog;
+       if (argc < 2) {
+               bb_show_usage();
+       }
 
-  if (chroot (argv[1]))
-    {
-      name_and_error ("cannot chroot to that directory");
-      return 1;
-    }
-  if (argc > 2)
-    {
-      execvp (argv[2], argv + 2);
-    }
-  else
-    {
-      prog = getenv ("SHELL");
-      if (!prog)
-       prog = "/bin/sh";
-      execlp (prog, prog, NULL);
-    }
-  name_and_error ("cannot exec");
-  return 1;
+       ++argv;
+       xchroot(*argv);
+       xchdir("/");
+
+       ++argv;
+       if (argc == 2) {
+               argv -= 2;
+               argv[0] = getenv("SHELL");
+               if (!argv[0]) {
+                       argv[0] = (char *) DEFAULT_SHELL;
+               }
+               argv[1] = (char *) "-i";
+       }
+
+       BB_EXECVP(*argv, argv);
+       bb_perror_msg_and_die("can't execute '%s'", *argv);
 }