This commit is contained in:
Your Name 2023-10-18 21:44:56 +03:00
parent 31133309c2
commit b701e39fbb
2 changed files with 15 additions and 4 deletions

View File

@ -5,7 +5,7 @@
#include <errno.h> #include <errno.h>
int main(const int argc, const char **argv) { int main(const int argc, const char **argv) {
if (argc <= 2) { if (argc < 3) {
printf("chroot [dir] [\"command arg\"]\n"); printf("chroot [dir] [\"command arg\"]\n");
return 0; return 0;
} }
@ -16,6 +16,8 @@ int main(const int argc, const char **argv) {
} }
chdir("/"); chdir("/");
for (int i = 2; i < argc; i++) for (int i = 1; i < argc; i++)
system(argv[i]); system(argv[i]);
return 0;
} }

View File

@ -21,11 +21,20 @@ char *make_path(const char *src, const char *dst) {
return full_path; return full_path;
} }
int write_buffer(int ifd, int ofd) { int write_buffer(int ifd, int ofd, const char *dst) {
off_t len = lseek(ifd, 0, SEEK_END); off_t len = lseek(ifd, 0, SEEK_END);
if (len < 0) if (len < 0)
return 1; return 1;
else if (len == 0) {
int fd = open(dst, O_CREAT | O_RDONLY, 0666);
if (fd < 0)
return 1;
close(fd);
return 0;
}
if (ftruncate(ofd, len) < 0) if (ftruncate(ofd, len) < 0)
return 1; return 1;
@ -57,7 +66,7 @@ int copy(const char *src, const char *dst) {
return 1; return 1;
} }
if (write_buffer(ifd, ofd)) if (write_buffer(ifd, ofd, dst))
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno)); fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
close(ifd); close(ifd);