I have to create a program that will create new one or overwrite existing(outfile) file, with the content of an already existing file(infile). For me, this is easy, I've already done this. But my problem is, that outfile needs to have same permission as infile and I have no idea how to do this.
programName infile outfile
My code looks like this, I think that I need to use stat() or fstat() but I'm not really sure how.
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
int main(int argc, char** argv) {
char* infile = argv[1];
char* outfile = argv[2];
int in = open(infile, O_RDONLY, S_IRUSR);
int out = open(outfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
long len = lseek(in, 0L, SEEK_END);
char buf[len];
lseek(in, 0L, SEEK_SET);
read(in, &buf, len);
write(out, &buf, len);
close(in);
close(out);
return 0;
}
I know that right now I've set permission manually.