bug fixes for LEASES from Rick Macklem
[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
53a143fe 15static char sccsid[] = "@(#)chroot.c 5.9 (Berkeley) %G%";
94a602c5
KB
16#endif /* not lint */
17
53a143fe
KB
18#include <sys/types.h>
19
20#include <errno.h>
7abf8d65 21#include <paths.h>
53a143fe
KB
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
94a602c5 26
53a143fe
KB
27void fatal __P((char *));
28void usage __P((void));
29
30int
94a602c5
KB
31main(argc, argv)
32 int argc;
53a143fe 33 char *argv[];
397de7ad 34{
53a143fe
KB
35 int ch;
36 char *shell;
37
38 while ((ch = getopt(argc, argv, "")) != EOF)
39 switch(ch) {
40 case '?':
41 default:
42 usage();
43 }
44 argc -= optind;
45 argv += optind;
46
47 if (argc < 2)
48 usage();
94a602c5 49
1ff41525 50 if (chdir(argv[1]) || chroot("."))
d4f5e936 51 fatal(argv[1]);
d4f5e936
KB
52 if (argv[2]) {
53 execvp(argv[2], &argv[2]);
54 fatal(argv[2]);
55 } else {
56 if (!(shell = getenv("SHELL")))
cac8e61f 57 shell = _PATH_BSHELL;
d4f5e936
KB
58 execlp(shell, shell, "-i", (char *)NULL);
59 fatal(shell);
60 }
61 /* NOTREACHED */
62}
63
53a143fe 64void
d4f5e936
KB
65fatal(msg)
66 char *msg;
67{
f72ac62a 68 (void)fprintf(stderr, "chroot: %s: %s\n", msg, strerror(errno));
94a602c5 69 exit(1);
397de7ad 70}
53a143fe
KB
71
72void
73usage()
74{
75 (void)fprintf(stderr, "usage: chroot newroot [command]\n");
76 exit(1);
77}