* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / chroot.c
blobd2ae0e5802cb63ac31fb5380ae9760e73edd12a9
1 /* chroot -- run command or shell with special root directory
2 Copyright (C) 95, 96, 1997, 1999-2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Roland McGrath. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "error.h"
27 #include "long-options.h"
28 #include "quote.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "chroot"
33 #define AUTHORS "Roland McGrath"
35 /* The name this program was run with, for error messages. */
36 char *program_name;
38 void
39 usage (int status)
41 if (status != EXIT_SUCCESS)
42 fprintf (stderr, _("Try `%s --help' for more information.\n"),
43 program_name);
44 else
46 printf (_("\
47 Usage: %s NEWROOT [COMMAND...]\n\
48 or: %s OPTION\n\
49 "), program_name, program_name);
50 fputs (_("\
51 Run COMMAND with root directory set to NEWROOT.\n\
52 \n\
53 "), stdout);
54 fputs (HELP_OPTION_DESCRIPTION, stdout);
55 fputs (VERSION_OPTION_DESCRIPTION, stdout);
56 fputs (_("\
57 \n\
58 If no command is given, run ``${SHELL} -i'' (default: /bin/sh).\n\
59 "), stdout);
60 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
62 exit (status);
65 int
66 main (int argc, char **argv)
68 initialize_main (&argc, &argv);
69 program_name = argv[0];
70 setlocale (LC_ALL, "");
71 bindtextdomain (PACKAGE, LOCALEDIR);
72 textdomain (PACKAGE);
74 initialize_exit_failure (EXIT_FAIL);
75 atexit (close_stdout);
77 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
78 usage, AUTHORS, (char const *) NULL);
79 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
80 usage (EXIT_FAIL);
82 if (argc <= optind)
84 error (0, 0, _("missing operand"));
85 usage (EXIT_FAIL);
88 if (chroot (argv[optind]) != 0)
89 error (EXIT_FAIL, errno, _("cannot change root directory to %s"), argv[1]);
91 if (chdir ("/"))
92 error (EXIT_FAIL, errno, _("cannot chdir to root directory"));
94 if (argc == optind + 1)
96 /* No command. Run an interactive shell. */
97 char *shell = getenv ("SHELL");
98 if (shell == NULL)
99 shell = "/bin/sh";
100 argv[0] = shell;
101 argv[1] = "-i";
102 argv[2] = NULL;
104 else
106 /* The following arguments give the command. */
107 argv += optind + 1;
110 /* Execute the given command. */
111 execvp (argv[0], argv);
114 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
115 error (0, errno, _("cannot run command %s"), quote (argv[0]));
116 exit (exit_status);