ANSI C
[unix-history] / usr / src / usr.sbin / chroot / chroot.c
CommitLineData
94a602c5
KB
1/*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
32ce521f 5 * %sccs.include.redist.c%
94a602c5
KB
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1988 The Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
32ce521f 15static char sccsid[] = "@(#)chroot.c 5.8 (Berkeley) %G%";
94a602c5
KB
16#endif /* not lint */
17
18#include <stdio.h>
7abf8d65 19#include <paths.h>
94a602c5
KB
20
21main(argc, argv)
22 int argc;
23 char **argv;
397de7ad 24{
d4f5e936
KB
25 extern int errno;
26 char *shell, *getenv(), *strerror();
94a602c5 27
d4f5e936 28 if (argc < 2) {
f72ac62a 29 (void)fprintf(stderr, "usage: chroot newroot [command]\n");
94a602c5
KB
30 exit(1);
31 }
1ff41525 32 if (chdir(argv[1]) || chroot("."))
d4f5e936 33 fatal(argv[1]);
d4f5e936
KB
34 if (argv[2]) {
35 execvp(argv[2], &argv[2]);
36 fatal(argv[2]);
37 } else {
38 if (!(shell = getenv("SHELL")))
cac8e61f 39 shell = _PATH_BSHELL;
d4f5e936
KB
40 execlp(shell, shell, "-i", (char *)NULL);
41 fatal(shell);
42 }
43 /* NOTREACHED */
44}
45
46fatal(msg)
47 char *msg;
48{
49 extern int errno;
50
f72ac62a 51 (void)fprintf(stderr, "chroot: %s: %s\n", msg, strerror(errno));
94a602c5 52 exit(1);
397de7ad 53}