diff options
author | Jim Meyering <jim@meyering.net> | 1996-03-28 05:42:56 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1996-03-28 05:42:56 +0000 |
commit | 0271d972595e38fb289ca9cf8021ae017ece54df (patch) | |
tree | 9314537d2d82bdf9781d0689ac43b076bc3fbe3e /src | |
parent | 9cb86170787764bc54c2e9b234d618e91e7db7e7 (diff) | |
download | coreutils-0271d972595e38fb289ca9cf8021ae017ece54df.tar.xz |
from Roland
Diffstat (limited to 'src')
-rw-r--r-- | src/chroot.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/chroot.c b/src/chroot.c new file mode 100644 index 000000000..31fa70fc3 --- /dev/null +++ b/src/chroot.c @@ -0,0 +1,89 @@ +/* chroot -- run command or shell with special root directory + Copyright (C) 1995 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#include <config.h> +#include <stdio.h> + +#include "system.h" +#include "long-options.h" +#include "version.h" + +void error (); + +static void usage (); + +/* The name this program was run with, for error messages. */ +char *program_name; + +int +main (argc, argv) + int argc; + char **argv; +{ + int c; + + program_name = argv[0]; + + parse_long_options (argc, argv, "chroot", version_string, usage); + + if (argc == 1) + usage (1); + + if (chroot (argv[1])) + error (1, errno, "cannot change root directory to %s", argv[1]); + + if (argc == 2) + { + /* No command. Run an interactive shell. */ + char *shell = getenv ("SHELL"); + if (shell == NULL) + shell = "/bin/sh"; + argv[0] = shell; + argv[1] = "-i"; + } + else + /* The following arguments give the command. */ + argv += 2; + + /* Execute the given command. */ + execvp (argv[0], argv); + error (1, errno, "cannot execute %s", argv[0]); + + exit (1); + return 1; +} + +static void +usage (status) + int status; +{ + if (status != 0) + fprintf (stderr, "Try `%s --help' for more information.\n", + program_name); + else + { + printf ("Usage: %s [OPTION] NEWROOT [COMMAND...]\n", program_name); + printf ("\ +\n\ + --help display this help and exit\n\ + --version output version information and exit\n\ +\n\ +If no command is given, runs ``${SHELL} -i'' (default: /bin/sh).\n\ +"); + } + exit (status); +} |