hostid: fix behavior on identifiers starting with zeros
[platform/upstream/busybox.git] / coreutils / chroot.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chroot implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
11
12 //usage:#define chroot_trivial_usage
13 //usage:       "NEWROOT [PROG ARGS]"
14 //usage:#define chroot_full_usage "\n\n"
15 //usage:       "Run PROG with root directory set to NEWROOT"
16 //usage:
17 //usage:#define chroot_example_usage
18 //usage:       "$ ls -l /bin/ls\n"
19 //usage:       "lrwxrwxrwx    1 root     root          12 Apr 13 00:46 /bin/ls -> /BusyBox\n"
20 //usage:       "# mount /dev/hdc1 /mnt -t minix\n"
21 //usage:       "# chroot /mnt\n"
22 //usage:       "# ls -l /bin/ls\n"
23 //usage:       "-rwxr-xr-x    1 root     root        40816 Feb  5 07:45 /bin/ls*\n"
24
25 #include "libbb.h"
26
27 int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
28 int chroot_main(int argc UNUSED_PARAM, char **argv)
29 {
30         ++argv;
31         if (!*argv)
32                 bb_show_usage();
33         xchroot(*argv);
34         xchdir("/");
35
36         ++argv;
37         if (!*argv) { /* no 2nd param (PROG), use shell */
38                 argv -= 2;
39                 argv[0] = (char *) get_shell_name();
40                 argv[1] = (char *) "-i"; /* GNU coreutils 8.4 compat */
41                 /*argv[2] = NULL; - already is */
42         }
43
44         BB_EXECVP_or_die(argv);
45 }