micro-utils/src/coreutils/chroot/chroot.c

30 lines
563 B
C
Raw Normal View History

2024-07-01 10:23:00 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(const int argc, char **argv) {
if (argc < 3 || !strcmp(argv[argc - 1], "--help")) {
2024-07-02 19:48:16 +00:00
puts("chroot [dir] [command] [arg arg2...]");
2024-07-01 10:23:00 +00:00
return 0;
}
if (chroot(argv[1]) < 0) {
fprintf(stderr, "chroot: %s\n", strerror(errno));
return 1;
}
if (chdir("/") < 0) {
fprintf(stderr, "chroot: %s\n", strerror(errno));
return 1;
}
if (execvp(argv[2], argv + 2) < 0) {
fprintf(stderr, "chroot: %s\n", strerror(errno));
return 1;
}
return 0;
}