Tizen 2.0 Release
[external/tizen-coreutils.git] / src / chroot.c
1 /* chroot -- run command or shell with special root directory
2    Copyright (C) 95, 96, 1997, 1999-2004 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Written by Roland McGrath.  */
19
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24
25 #include "system.h"
26 #include "error.h"
27 #include "long-options.h"
28 #include "quote.h"
29
30 /* The official name of this program (e.g., no `g' prefix).  */
31 #define PROGRAM_NAME "chroot"
32
33 #define AUTHORS "Roland McGrath"
34
35 /* The name this program was run with, for error messages. */
36 char *program_name;
37
38 void
39 usage (int status)
40 {
41   if (status != EXIT_SUCCESS)
42     fprintf (stderr, _("Try `%s --help' for more information.\n"),
43              program_name);
44   else
45     {
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);
61     }
62   exit (status);
63 }
64
65 int
66 main (int argc, char **argv)
67 {
68   initialize_main (&argc, &argv);
69   program_name = argv[0];
70   setlocale (LC_ALL, "");
71   bindtextdomain (PACKAGE, LOCALEDIR);
72   textdomain (PACKAGE);
73
74   initialize_exit_failure (EXIT_FAIL);
75   atexit (close_stdout);
76
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);
81
82   if (argc <= optind)
83     {
84       error (0, 0, _("missing operand"));
85       usage (EXIT_FAIL);
86     }
87
88   if (chroot (argv[optind]) != 0)
89     error (EXIT_FAIL, errno, _("cannot change root directory to %s"), argv[1]);
90
91   if (chdir ("/"))
92     error (EXIT_FAIL, errno, _("cannot chdir to root directory"));
93
94   if (argc == optind + 1)
95     {
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;
103     }
104   else
105     {
106       /* The following arguments give the command.  */
107       argv += optind + 1;
108     }
109
110   /* Execute the given command.  */
111   execvp (argv[0], argv);
112
113   {
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);
117   }
118 }