micro-utils/src/chroot.c

34 lines
605 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>
2024-07-03 14:22:50 +00:00
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "")) != -1 || argc == 1) {
2024-07-02 19:48:16 +00:00
puts("chroot [dir] [command] [arg arg2...]");
2024-07-01 10:23:00 +00:00
return 0;
}
2024-07-03 14:22:50 +00:00
argv += optind;
argc -= optind;
2024-07-01 10:23:00 +00:00
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;
}
2024-07-03 14:22:50 +00:00
if (execvp(argv[0], argv) < 0) {
2024-07-01 10:23:00 +00:00
fprintf(stderr, "chroot: %s\n", strerror(errno));
return 1;
}
return 0;
}